5cdec8829971654582fab6a274dbd6fbdf0e06c4
Replace the bare SemanticallyEqual call in every frontend (CLI fmt, LSP
formatting + rangeFormatting) with format.VerifySafe, which runs four
checks before any formatted output is emitted:
- semantic equivalence - the code token stream is unchanged
- comment preservation - no -- or /* */ comment is dropped, merged,
split, reordered, or reworded (line endings / indentation normalised
away; recurses into dollar-quoted bodies)
- structural balance - the () [] and BEGIN/CASE/IF/LOOP...END nesting
profile matches, ignoring anything inside a comment or a string
- idempotence - a second format pass would not change it
On failure the CLI now prints the specific reason and keeps the original.
The comment check surfaced two real formatter bugs, both fixed in
formatBodyStatements:
- multi-line /* */ comments inside a PL/pgSQL body had their interior
lines re-split and reindented as if they were statements; they are
now tracked and carried verbatim with the opening line
- a column-0 -- line was glued onto the preceding line by the
split-line-join, which merged consecutive comment lines into one
Regenerate testdata/corpus/test_mm_proc.pgsql (was carrying the mangled
output). TestCorpusIdempotentAndSafe now runs the full VerifySafe bundle;
add safety_test.go with targeted cases.
PgTidy
A PostgreSQL linter and formatter. Enforces consistent SQL style, detects PG-specific issues, and formats migrations, schemas, functions, procedures, and triggers.
Ships as a CLI, LSP server, VSCode extension, and DataGrip plugin.
Install
go install git.warky.dev/wdevs/pgtidy/cmd/pgtidy@latest
Or download a pre-built binary from Releases.
CLI
pgtidy fmt [flags] [files...] Format SQL/PL-pgSQL (stdin if no files)
pgtidy lint [flags] [files...] Lint SQL
pgtidy config Print effective configuration
pgtidy lsp Start LSP server (stdio)
pgtidy version
fmt flags
| Flag | Effect |
|---|---|
-w, --write |
Rewrite files in place |
-l, --list |
List unformatted files |
-d, --diff |
Print unified diff |
--check |
Exit non-zero if not formatted (CI) |
Examples
pgtidy fmt -w schema.sql # format in place
pgtidy fmt --check migrations/ # CI check
cat query.sql | pgtidy fmt # stdin → stdout
pgtidy lint schema.sql # lint a file
Config — .pgtidy.yaml
Discovered by walking up from the target file. Defaults = house style.
style:
indent: 2
keyword_case: upper # upper | lower | preserve
identifier_case: lower
type_case: lower
leading_comma: true
pgtidy config # print resolved config
House Style (defaults)
- Keywords
UPPERCASE; data types and identifierslowercase - 2-space indent
- Leading-comma lists (SELECT columns, function params)
- Function params one-per-line;
LANGUAGE,SECURITY, volatility each on own line - PL/pgSQL:
DECLAREblock vars 2-space indented;BEGIN/ENDat body level - Spaces around binary operators (
=,<>,||,:=); no space before(or around::,->,->>
Build from Source
git clone https://git.warky.dev/wdevs/pgtidy
cd pgtidy
make build # → dist/pgtidy
make test # all tests + corpus harness
make snapshot # multi-platform binaries (requires goreleaser)
Editor Integration
VSCode — install the extension from the marketplace or build locally:
make vscode-package
DataGrip / JetBrains — install via the plugin marketplace (LSP4IJ-based).
Other LSP editors — run pgtidy lsp as a stdio LSP server.
