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

pgfmt is idempotent. Running it twice produces the same output.

Workflow

For package-local queries:

  1. Write or edit SQL in <pkg>/queries/*.sql.
  2. Run pgfmt on those files.
  3. Commit the formatted SQL with the Go call-site changes.

← All articles