pg_query.rs

This Rust library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.
It also allows you to normalize queries (replacing constant values with $1, etc.) and parse these normalized queries into a parse tree again.
When you build this library, it builds parts of the PostgreSQL server source (see libpg_query), and then statically links it into this library.
You can find further examples and a longer rationale for the original Ruby implementation here. The Rust version tries to have a very similar API.
Examples
Parsing a query
use NodeRef;
let result = parse;
assert!;
let result = result.unwrap;
assert_eq!;
assert!;
Normalizing a query
let result = normalize.unwrap;
assert_eq!;
Fingerprinting a query
let result = fingerprint.unwrap;
assert_eq!;
Fingerprinting allows you to identify similar queries that are different only
because of the specific object being queried (e.g. different object ids in the
WHERE clause) or because of formatting. It is intended to be a superset of
the Postgres queryid mechanism: constant values are ignored, so
... WHERE id = 123 and ... WHERE id = 456 get the same fingerprint.
Postgres 18 significantly changed how schema names and table aliases in FROM
clauses are handled for queryid, and libpg_query follows that by default:
- Postgres 17 and earlier: the schema name was significant, and table aliases were always ignored.
- Postgres 18 (default): schema names are ignored, and a table alias (if present) is used instead of the relation name.
// Postgres 18 behaviour (default): schema is ignored and the alias replaces the relation name
let a = fingerprint.unwrap;
let b = fingerprint.unwrap;
assert_eq!;
// Postgres 17 compatible fingerprints take the schema into account
let a = fingerprint.unwrap;
let b = fingerprint.unwrap;
assert_ne!;
Available fingerprint options (combinable, passed as the third fingerprint
argument to pg_query::fingerprint):
FingerprintOptions |
effect |
|---|---|
DEFAULT (0) |
Postgres 18 query ID behaviour |
RANGEVAR_IGNORE_ALIASES |
relation names are always fingerprinted; aliases are ignored |
RANGEVAR_INCLUDE_SCHEMA |
schema names are also fingerprinted in SELECT/DML statements |
RANGEVAR_PG17_COMPAT |
combination of the two above, matching Postgres 17 (and libpg_query 17) fingerprints |
FULL_RELNAME |
fingerprint the full relation name instead of ignoring 2+ consecutive digits (which by default groups date/number-suffixed partitions together) |
See https://github.com/pganalyze/libpg_query/wiki/Fingerprinting for the full fingerprinting rules.
Scanning a query's tokens without protobuf
let tokens = scan_tokens.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Detecting utility statements
let result = is_utility_stmt.unwrap;
assert_eq!;
Deparsing a query
use DeparseOptions;
let parsed = parse.unwrap;
let output = deparse.unwrap;
assert_eq!;
// With comments extracted and re-inserted, pretty-printed:
let comments = deparse_comments_for_query.unwrap;
let output = deparse
.unwrap;
assert!;
Truncating a query
let query = "INSERT INTO \"x\" (a, b, c, d, e, f) VALUES ($1)";
let result = parse.unwrap;
assert_eq!;
Credits
Thanks to Paul Mason for his work on pg_parse that this crate is based on.
After version 0.6.0, Paul donated the pg_query crate to the pganalyze team. pg_parse is a lighter alternative that focuses on query parsing, while pg_query aims for feature parity with the Ruby gem.
License
PostgreSQL server source code, used under the PostgreSQL license. Portions Copyright (c) 1996-2023, The PostgreSQL Global Development Group Portions Copyright (c) 1994, The Regents of the University of California
All other parts are licensed under the MIT license, see LICENSE file for details. Copyright (c) 2021 Paul Mason paul@form1.co.nz Copyright (c) 2021-2023, Duboce Labs, Inc. (pganalyze) team@pganalyze.com