Postgres SQL tools from Query Doctor: parse SQL into an AST and back, format it, fingerprint it, read SQLCommenter tags and quote identifiers. Each tool is its own import, so a bundle carries only the tools it uses.
npm install @query-doctor/sql-toolsRuns in Node 20 and later, as ES modules or CommonJS.
| Import | Exports | Loads | Browser bundle, gzipped |
|---|---|---|---|
@query-doctor/sql-tools/parse |
parse, fingerprint, normalize, scan, their …Sync variants, loadModule |
libpg_query as WebAssembly | 14.5 KB, plus the 1.6 MB .wasm (309 KB gzipped) |
@query-doctor/sql-tools/deparse |
deparse, deparseSync |
pgsql-deparser, no WebAssembly | 42.3 KB |
@query-doctor/sql-tools/fingerprint |
normalizedFingerprint, fingerprintAfter |
pgsql-deparser, no WebAssembly | 42.7 KB |
@query-doctor/sql-tools/format |
formatSql |
sql-formatter | 20.0 KB |
@query-doctor/sql-tools/sqlcommenter |
extractSqlcommenter |
nothing | 0.5 KB |
@query-doctor/sql-tools/ident |
PgIdentifier |
nothing | 1.0 KB |
import { deparse } from "@query-doctor/sql-tools/deparse";
import { parse, fingerprint, normalize } from "@query-doctor/sql-tools/parse";
const ast = await parse("select a, b from t where id in (1,2)");
await deparse(ast); // "SELECT a, b FROM t WHERE id IN (1, 2)"
await normalize("select * from t where id = 42"); // "select * from t where id = $1"
(await fingerprint("select * from t where id = 1")) ===
(await fingerprint("SELECT * FROM t WHERE id = 42")); // trueparse uses libpg_query, which is Postgres's own parser, so it accepts exactly what Postgres 17 accepts. SQL that only Postgres 18 accepts, such as a VIRTUAL generated column, is a syntax error. deparse drops comments, because the AST does not carry them.
import { normalizedFingerprint } from "@query-doctor/sql-tools/fingerprint";
import { parse, fingerprint } from "@query-doctor/sql-tools/parse";
const key = async (sql: string) =>
normalizedFingerprint(await parse(sql), fingerprint);
(await key("select id, name from users where id = $1")) ===
(await key("select email, id from users where id = $1")); // true
(await key("select id from users where id = $1")) ===
(await key("select id from users where email = $1")); // falsefingerprint groups queries the way pg_stat_statements does, so adding or reordering a column makes a new query. normalizedFingerprint drops the plain column references from every select list and sorts what is left, so the same query with different columns keeps one key. Anything else, such as an expression, the WHERE or the FROM, still changes it. fingerprintAfter fingerprints the tree after your own rewrite of each node.
import { formatSql } from "@query-doctor/sql-tools/format";
formatSql("select id from users where id = $1");
// "SELECT\n id\nFROM\n users\nWHERE\n id = $1\n"Line endings become \n and a leading byte-order mark is kept, so offsets into the output are stable. It throws on SQL it cannot read.
import { extractSqlcommenter } from "@query-doctor/sql-tools/sqlcommenter";
extractSqlcommenter("select 1 /*route='%2Forders',db_driver='drizzle'*/");
// {
// tags: [{ key: "route", value: "/orders" }, { key: "db_driver", value: "drizzle" }],
// queryWithoutTags: "select 1 ",
// }Only a comment at the end of the query is read as tags. The collapsed IN list that pg_stat_statements 18 writes stays in the query.
import { PgIdentifier } from "@query-doctor/sql-tools/ident";
PgIdentifier.fromString("select").toString(); // "\"select\""
PgIdentifier.fromParts("public", "my-table").toString(); // "public.\"my-table\""Quotes the way Postgres's quote_ident does: only names that need it.
parsememory in Node: once a process has parsed a few hundred queries, V8 recompiles libpg_query's WebAssembly with its optimizing compiler. Over 5,000 parses of a 0.5 KB query, that recompile raised peak RSS from 71 MB to 671 MB and used more CPU than it saved (476 ms against 328 ms). Starting Node with--liftoff-onlykeeps the baseline compiler and the lower numbers. The flag has to be on the command line: Node rejects it inNODE_OPTIONS, and setting it at runtime has no effect. V8 lists it as a testing flag, so check that Node still starts after an upgrade.parsein a browser: the page fetcheslibpg-query.wasmfrom its own directory, and bundlers do not emit that file. Copynode_modules/@libpg-query/parser/wasm/libpg-query.wasmto where your page is served from.formatSqlon long queries: one call on anINlist of 30 KB takes about 100 ms of CPU and 20 MB of memory; 234 KB takes 441 ms and 105 MB.
Apache-2.0