Skip to content

pg_genexec

Experimental verified IR and LLVM execution for selected aggregate queries

pg_genexec · Experimental verified IR and LLVM execution for selected aggregate queries

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
102465pg_genexecpg_genexec0.1FEATApache-2.0C

PostgreSQL · 16, 17

Usage

Sources:

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.

CREATE EXTENSION pg_genexec;
LOAD 'pg_genexec';
CREATE TABLE genexec_example (category integer NOT NULL, amount bigint NOT NULL);
SELECT genexec_export_context(
  'SELECT category, SUM(amount) FROM genexec_example GROUP BY category'
);

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.

SELECT plan_id, enabled, pg_baseline_ms, gen_best_ms
FROM pg_genexec_plans ORDER BY plan_id DESC;
SET pg_genexec.enabled = off;
SET pg_genexec.jit = off;

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.

Table reader