pg_genexec
pg_genexec · Experimental verified IR and LLVM execution for selected aggregate queries
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 102465 | pg_genexec | pg_genexec | 0.1 | FEAT | Apache-2.0 | C |
PostgreSQL · 16, 17
Usage
Sources:
- README.md
- extension/pg_genexec.control
- extension/pg_genexec–0.1.sql
- extension/Makefile
- extension/src/pg_genexec.c
- extension/src/sql_api.c
- extension/src/query.c
- extension/src/verifier.c
- docs/usage/configuration.md
- docs/usage/getting-started.md
pg_genexec 0.1 is an experimental C extension for verified, adaptive execution of selected single-table aggregate queries. It uses a constrained JSON intermediate representation, a bytecode executor and LLVM MCJIT kernels. The reviewed source documents PostgreSQL 16 and 17; the optional offline CLI requires Python 3.10 or later.
Load and Inspect a Query
A superuser installs the relocatable extension after the PostgreSQL and LLVM libraries are available. Installation executes a session-local library load. Load it in other query sessions too so its planner hooks are present; shared preloading and a server restart are not required for this session workflow.
genexec_export_context returns the query’s schema, types, parameters and guard context. The separate CLI can use an existing IR file or request generated IR; the PostgreSQL library itself makes no network requests. Review any external model’s data handling before sending schema or statistics to it.
Register, Verify and Enable
genexec_verify_and_register(sql_template, ir_json, guards_json, sample_params) validates the candidate and compares sampled results with PostgreSQL’s native executor. It returns ok, plan_id, error_msg, pg_ms and gen_ms. Successful plans enter pg_genexec_plans with enabled = false; enable only the returned, reviewed identifier using genexec_enable, or disable it with genexec_disable.
The two settings default to on and are session-settable. The first disables selection of registered plans; the second selects bytecode execution without LLVM kernels. Registration and enable/disable functions have public execution revoked. Appropriate table privileges are still required.
Supported Shape and Failure Boundaries
An enabled matching plan appears as Custom Scan (GenExec) in query plans. The implementation rejects unsupported query shapes, non-heap relations, inheritance children and row-security relations. It verifies schema/type/range guards and samples before registration; sample agreement is not a proof for all future inputs. Keep native execution available, compare representative workloads, and treat this newly published source as experimental rather than a general query optimizer.