Architecture
A post-parse hook between the parser and the optimizer.
HyperQuery/PG is a 100% PostgreSQL 15+ compatible extension. It registers a post-parse hook that intercepts the parser's output AST — the parse tree — and rewrites it before forwarding to the query optimizer. Your application, ORM, and driver see nothing different.
The hook
What the post-parse hook sees — and what it does.
PostgreSQL exposes a post_parse_analyze_hook that fires after the parser and analyzer have resolved all types and catalog references but before the planner builds a cost model. HyperQuery/PG owns this hook.
Fully typed AST, not raw SQL
The hook receives a resolved Query struct — all column types, operator OIDs, and catalog references already bound. Rules match on semantics, not text patterns.
In-process, zero-copy
No proxy, no sidecar, no network hop. The rewrite happens inside the same PostgreSQL backend process handling the connection — adding microseconds, not milliseconds.
Chains existing hooks
HyperQuery/PG saves and restores any previously-registered post-parse hook, so it cooperates correctly with other extensions using the same hook point.
Requires PostgreSQL 15+
The post_parse_analyze_hook signature stabilised in PG 15. HyperQuery/PG targets PostgreSQL 14 and later on self-managed instances.
Full pipeline
Where HyperQuery/PG fits in PostgreSQL's internals.
The diagram maps to a standard PostgreSQL query lifecycle. HyperQuery/PG occupies a single, well-defined slot — nothing else in the pipeline is modified.
Query struct.Query tree, matches anti-patterns, rewrites sub-trees in-place. Logs every rewrite. Passes the (possibly modified) tree forward.Foundations
Grounded in relational algebra and relational calculus.
HyperQuery/PG's rewrite rules are not heuristics or machine-learned patterns. Each one is derived from first principles in relational algebra — the same mathematical foundation that SQL itself is built on.
Relational equivalence, not empirical testing
A rule ships only when its rewrite can be shown — algebraically — to produce the identical relation: same tuples, same ordering. No edge case is left to empirical testing alone.
NULL semantics are part of the proof
SQL's three-valued logic (TRUE / FALSE / UNKNOWN) is one of the most common sources of subtle correctness bugs in query rewrites. Every rule handles NULL behaviour explicitly.
Order preservation
Rewrites that change join order or set operations are proven to preserve the output ORDER BY — including tie-breaking. The planner's freedom to reorder is preserved downstream.
Regression suite per rule
In addition to the algebraic proof, each rule ships with a schema-and-data regression suite that validates identical output across the full range of known edge cases.
Want to see the hook fire on your queries?
Share your Postgres version and a slow query — we'll walk through exactly which rule would intercept it.