cd /news/developer-tools/ann-pgfmt-format-your-hasql-th-block… · home › topics › developer-tools › article
[ARTICLE · art-176833] src=discourse.haskell.org ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

[ANN] pgfmt Format your hasql-th blocks (or any other SQL file)

Kronor released pgfmt, an open-source formatter for PostgreSQL SQL files and for SQL embedded in Haskell hasql-th quasiquotes, extracted from an in-house tool with the help of an LLM. Before rewriting any statement, pgfmt parses the output with libpg_query, PostgreSQL's own parser, and compares parse trees; if they differ, the statement is left unchanged and pgfmt exits non-zero. The tool, which the team says it has run over thousands of production migrations and hasql-th blocks, offers a --check flag for CI and pre-commit, --keyword-case lower|upper, and --quoter NAME for custom quoter aliases, and formats only the SQL inside quoters such as singletonStatement and vectorStatement while leaving the surrounding Haskell untouched so it works alongside fourmolu or ormolu.

key takeaway

pgfmt reformats SQL in .sql files and hasql-th quasiquotes only after re-parsing the result with libpg_query and confirming the parse tree is unchanged, exiting non-zero otherwise, and offers --check for CI plus --keyword-case and --quoter options.

read2 min views7 publishedNov 26, 2026

We have been using this SQL formatter at work that we built in house. With the help of an LLM we managed to extract it from our code base and make it a proper tool for general use.

pgfmt, a formatter for PostgreSQL SQL files and for the SQL inside Haskell hasql-th quasiquotes.

We’ve taken lots of care in making sure the formatter does not alter the meaning of the SQL it outputs. Before rewriting a statement, pgfmt parses the result with libpg_query (PostgreSQL’s own parser) and compares parse trees. If they differ, the statement is left exactly as it was and pgfmt exits non-zero. We’ve run it over thousands of production migrations and hasql-th blocks.

The style it follows is quite arbitrarily our own preference. We never did agree in the end on what we liked, but after a while we just accepted the benefit of having a single style and the arguments were over.

For .hs files it formats the SQL inside singletonStatement, vectorStatement and the other hasql-th quoters, and leaves the Haskell alone, so it works alongside fourmolu or ormolu:

userEmails :: Statement Int64 (Vector (Text, Maybe Text))
userEmails =
    [vectorStatement|
      select u.name :: text, e.address :: text? from users u left join emails e on e.user_id = u.id where u.org_id = $1 :: int8 order by u.name
    |]

becomes

userEmails :: Statement Int64 (Vector (Text, Maybe Text))
userEmails =
    [vectorStatement|
        select
            u.name::text
          , e.address::text?
        from users u
        left join emails e
          on e.user_id = u.id
        where u.org_id = $1::int8
        order by u.name
    |]

It skips comments and string literals the way GHC’s lexer does, so a quasiquote inside a string or a comment is never touched. Each quasiquote is only rewritten if it parses back to the same AST.

Other features:

--check for CI and pre-commit--keyword-case lower|upper``--quoter NAME for your own quoter aliases Try it:

nix run --accept-flake-config github:kronor-io/pgfmt -- --check src/**/*.hs

Feedback and issues are welcome: GitHub - kronor-io/pgfmt: Format PostgreSQL files, checked against PostgreSQL's own parser. Part of sqitchcraft: https://kronor-io.github.io/sqitchcraft/ · GitHub

── more in #developer-tools 4 stories · sorted by recency
── more on @pgfmt 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/ann-pgfmt-format-you…] indexed:0 read:2min 2026-11-26 · —