pg_normalize_query
pg_normalize_query · Normalize constants in SQL statements using PostgreSQL parser logic
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100129 | pg_normalize_query | pg_normalize_query | 1.0 | FUNC | BSD-3-Clause | C |
PostgreSQL · 10, 11, 12, 13
Usage
Sources:
pg_normalize_query replaces constants in a valid SQL statement with numbered parameters, producing a fingerprint-like text form similar to the normalization used by pg_stat_statements. It is useful for grouping statement shapes in logs or application telemetry.
Core Workflow
API
The extension exposes one strict, immutable function: pg_normalize_query(query text) RETURNS text. The implementation invokes PostgreSQL’s raw parser, walks constant locations in the parse tree, and rewrites those token spans as $1, $2, and so on. Existing external parameters are accounted for when numbering replacements.
Interpretation and Compatibility
Input must be syntactically valid for the PostgreSQL parser used to build the extension; parse errors are raised normally. The result preserves most original spelling and whitespace, so it is a normalization of constants rather than a canonical SQL formatter. The implementation also notes that equivalent queries can still yield different normalized representations.
The reviewed upstream snapshot targeted PostgreSQL 9.4 through pre-release 13. Because it copies parser-adjacent logic from a historical PostgreSQL/libpg_query implementation, verify both compilation and representative syntax on newer server majors before relying on stable fingerprints across upgrades.