go / pgfmt
pgfmt is a small SQL formatter for Postgres query files.
For package-local SQL query layout and embedding, see
go / sql queries.
I run it on */queries/*.sql so SQL style stays consistent
without review bikeshedding.
Why
When SQL lives in many package-local directories, style drifts fast. Diffs get noisy. Reviews spend time on formatting.
A formatter keeps diffs focused on behavior.
Command
Format in place:
go run ./cmd/pgfmt -w <pkg>/queries/*.sql
Format stdin to stdout:
cat query.sql | go run ./cmd/pgfmt
CI behavior
With CI=true, pgfmt exits non-zero if a file would change and
prints the exact file path to reformat.
This keeps enforcement deterministic in CI and cheap locally.
Style it enforces
- Uppercase SQL keywords.
- Lowercase function names.
- Two-space indentation.
- Vertical clause lists (
SELECT,FROM,WHERE,ORDER BY). - Stable wrapping for common constructs (
CASE,NOT EXISTS,jsonb_build_object, joins).
pgfmt is idempotent. Running it twice produces the same output.
Workflow
For package-local queries:
- Write or edit SQL in
<pkg>/queries/*.sql. - Run pgfmt on those files.
- Commit the formatted SQL with the Go call-site changes.