Skip to content

pg_plan_guard

Detect drift from captured PostgreSQL query plan baselines

pg_plan_guard · Detect drift from captured PostgreSQL query plan baselines

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
6100pg_plan_guardpg_plan_guard1.1.12STATApache-2.0PLpgSQL
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
----d--NoNoNoYesnono
Plan capture uses the PG19 pg_plan_advice library.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY1.1.1219 18 17 16 15pg_plan_guard-
RPMPIGSTY1.1.1219 18 17 16 15pg_plan_guard_$vpostgresql$v-contrib
DEBPIGSTY1.1.1219 18 17 16 15postgresql-$v-pg-plan-guard-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/AN/AN/AN/AN/A
el8.aarch64N/AN/AN/AN/AN/A
el9.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
el9.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
el10.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
el10.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
d12.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
d12.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
d13.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
d13.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
u22.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
u22.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
u24.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
u24.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
u26.x86_64PIGSTY 1.1.12N/AN/AN/AN/A
u26.aarch64PIGSTY 1.1.12N/AN/AN/AN/A
PackageVersionOSORGSIZEFile URL
pg_plan_guard_191.1.12el9.x86_64pigsty32.7 KiBpg_plan_guard_19-1.1.12-1PGSTY.el9.noarch.rpm
pg_plan_guard_191.1.12el9.aarch64pigsty32.7 KiBpg_plan_guard_19-1.1.12-1PGSTY.el9.noarch.rpm
pg_plan_guard_191.1.12el10.x86_64pigsty32.8 KiBpg_plan_guard_19-1.1.12-1PGSTY.el10.noarch.rpm
pg_plan_guard_191.1.12el10.aarch64pigsty32.8 KiBpg_plan_guard_19-1.1.12-1PGSTY.el10.noarch.rpm
postgresql-19-pg-plan-guard1.1.12d12.x86_64pigsty26.9 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~bookworm_all.deb
postgresql-19-pg-plan-guard1.1.12d12.aarch64pigsty26.9 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~bookworm_all.deb
postgresql-19-pg-plan-guard1.1.12d13.x86_64pigsty26.9 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~trixie_all.deb
postgresql-19-pg-plan-guard1.1.12d13.aarch64pigsty26.9 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~trixie_all.deb
postgresql-19-pg-plan-guard1.1.12u22.x86_64pigsty23.4 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~jammy_all.deb
postgresql-19-pg-plan-guard1.1.12u22.aarch64pigsty23.4 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~jammy_all.deb
postgresql-19-pg-plan-guard1.1.12u24.x86_64pigsty23.3 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~noble_all.deb
postgresql-19-pg-plan-guard1.1.12u24.aarch64pigsty23.3 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~noble_all.deb
postgresql-19-pg-plan-guard1.1.12u26.x86_64pigsty23.2 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~resolute_all.deb
postgresql-19-pg-plan-guard1.1.12u26.aarch64pigsty23.2 KiBpostgresql-19-pg-plan-guard_1.1.12-1PGSTY~resolute_all.deb

Source

github.com/Manuelreyesbravo/pg_plan_guard

pig build pkg pg_plan_guard;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_plan_guard;		# install by package name

pig install pg_plan_guard -v 19;   # install for PG 19

Create this extension with:

CREATE EXTENSION pg_plan_guard;

Usage

Sources:

pg_plan_guard 1.1.12 records approved PostgreSQL 19 plan-advice text and detects changes when stored queries are replanned. Plan capture loads the PostgreSQL 19 contrib library pg_plan_advice. For monitoring roles without superuser privileges, preload the library in shared_preload_libraries.

Core Workflow

LOAD 'pg_plan_advice';
CREATE EXTENSION pg_plan_guard;

SELECT plan_guard.capture(
    'semantic_search',
    'SELECT id FROM docs ORDER BY embedding <=> ''[1,2,3]'' LIMIT 10',
    'must use the vector index'
);

SELECT * FROM plan_guard.verify();
SELECT * FROM plan_guard.status WHERE state <> 'ok';

Operational Boundaries

plan_guard.capture() records a query, its advice, author and search path. plan_guard.verify() replans it, and plan_guard.status exposes state; plan_guard.drift_log records transitions and rejects modification. Planning happens read-only in a rolled-back subtransaction as the captured author, so the checking role must be allowed to assume that role. A baseline from before 1.1.7 has no author and must be captured again; otherwise it returns an error. The installer rejects a pre-created schema owned by an unrelated unprivileged role. Planning can invoke functions, so stored SQL must still be trusted and the baseline reviewed. plan_guard.watch() optionally uses pg_living_assertions 0.5.6 or later; ordinary capture/verification do not require it. plan_guard.sync_stash() additionally needs pg_stash_advice preload, restart and a configured stash. Current Apache-2.0 licensing starts at 1.1.3.

Role Isolation in 1.1.9

Version 1.1.9 plans stored SQL inside a temporary author-owned SECURITY DEFINER function when role isolation is needed. A function evaluated by the planner can no longer regain the runner’s identity by resetting role or session authorization. The function is created and rolled back inside the read-only planning subtransaction. The author needs database TEMP permission when this frame is required; the runner needs authority to act as the author. Missing privileges produce a baseline error. This fixes the role-reset escape in 1.1.7/1.1.8 without turning stored SQL into untrusted input.

1.1.11 Compatibility and Dependencies

Version 1.1.11 refuses CREATE EXTENSION on PostgreSQL 18 and earlier because pg_plan_advice is available only on PostgreSQL 19. The PGXN META metadata treats pg_plan_advice as required and pg_living_assertions as optional for watch(). sync_stash() resolves pg_stash_advice in the installation schema, and reports a failed action when that companion extension is absent. The current SQL version is 1.1.12; use the supplied upgrade path and reconnect after replacing files.

1.1.10 Watch Privileges

Version 1.1.10 revokes PUBLIC execution of plan_guard.watch(). Grant it explicitly only to trusted operators that may register monitoring work. This does not remove the captured-author isolation, database TEMP or role-assumption requirements of 1.1.9.

Current Compatibility

Version 1.1.12 updates documentation and tested compatibility without changing SQL objects. Upstream records PostgreSQL 19.0 coverage. The supplied 1.1.11-to-1.1.12 SQL update advances the extension version only; keep the earlier role and dependency requirements above.

Table reader