Skip to content
pg_query_rewrite

pg_query_rewrite

pg_query_rewrite : Rewrite SQL statements with a PostgreSQL ProcessUtility hook

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
5030
pg_query_rewrite
pg_query_rewrite
0.0.5
ADMIN
PostgreSQL
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd--
No
Yes
Yes
Yes
no
no

Requires shared_preload_libraries=pg_query_rewrite.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
0.0.5
18
17
16
15
14
pg_query_rewrite-
RPM
PIGSTY
0.0.5
18
17
16
15
14
pg_query_rewrite_$v-
DEB
PIGSTY
0.0.5
18
17
16
15
14
postgresql-$v-pg-query-rewrite-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
el8.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
el9.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
el9.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
el10.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
el10.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
d12.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
d12.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
d13.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
d13.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
u22.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
u22.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
u24.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
u24.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
u26.x86_64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
u26.aarch64
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PIGSTY 0.0.5
PackageVersionOSORGSIZEFile URL
pg_query_rewrite_180.0.5el8.x86_64pigsty18.8 KiBpg_query_rewrite_18-0.0.5-1PIGSTY.el8.x86_64.rpm
pg_query_rewrite_180.0.5el8.aarch64pigsty19.0 KiBpg_query_rewrite_18-0.0.5-1PIGSTY.el8.aarch64.rpm
pg_query_rewrite_180.0.5el9.x86_64pigsty18.8 KiBpg_query_rewrite_18-0.0.5-1PIGSTY.el9.x86_64.rpm
pg_query_rewrite_180.0.5el9.aarch64pigsty18.9 KiBpg_query_rewrite_18-0.0.5-1PIGSTY.el9.aarch64.rpm
pg_query_rewrite_180.0.5el10.x86_64pigsty18.9 KiBpg_query_rewrite_18-0.0.5-1PIGSTY.el10.x86_64.rpm
pg_query_rewrite_180.0.5el10.aarch64pigsty19.1 KiBpg_query_rewrite_18-0.0.5-1PIGSTY.el10.aarch64.rpm
postgresql-18-pg-query-rewrite0.0.5d12.x86_64pigsty24.1 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~bookworm_amd64.deb
postgresql-18-pg-query-rewrite0.0.5d12.aarch64pigsty24.0 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~bookworm_arm64.deb
postgresql-18-pg-query-rewrite0.0.5d13.x86_64pigsty24.1 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~trixie_amd64.deb
postgresql-18-pg-query-rewrite0.0.5d13.aarch64pigsty24.1 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~trixie_arm64.deb
postgresql-18-pg-query-rewrite0.0.5u22.x86_64pigsty25.5 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~jammy_amd64.deb
postgresql-18-pg-query-rewrite0.0.5u22.aarch64pigsty25.6 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~jammy_arm64.deb
postgresql-18-pg-query-rewrite0.0.5u24.x86_64pigsty25.1 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~noble_amd64.deb
postgresql-18-pg-query-rewrite0.0.5u24.aarch64pigsty25.2 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~noble_arm64.deb
postgresql-18-pg-query-rewrite0.0.5u26.x86_64pigsty25.0 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~resolute_amd64.deb
postgresql-18-pg-query-rewrite0.0.5u26.aarch64pigsty25.1 KiBpostgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~resolute_arm64.deb

Source

pig build pkg pg_query_rewrite;		# 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_query_rewrite;		# install via package name, for the active PG version

pig install pg_query_rewrite -v 18;   # install for PG 18
pig install pg_query_rewrite -v 17;   # install for PG 17
pig install pg_query_rewrite -v 16;   # install for PG 16
pig install pg_query_rewrite -v 15;   # install for PG 15
pig install pg_query_rewrite -v 14;   # install for PG 14

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_query_rewrite';

Create this extension with:

CREATE EXTENSION pg_query_rewrite;

Usage

Source: README

pg_query_rewrite rewrites an exact source SQL statement into a predefined target statement using rules stored in shared memory.

Required setup

-- postgresql.conf
shared_preload_libraries = 'pg_query_rewrite'
pg_query_rewrite.max_rules = 10

CREATE EXTENSION pg_query_rewrite;

Rule management

SELECT pgqr_add_rule('select 10;', 'select 11;');
SELECT pgqr_rules();
SELECT pgqr_remove_rule('select 10;');
SELECT pgqr_truncate();
  • pgqr_add_rule(source, target): add a rewrite rule.
  • pgqr_remove_rule(source): remove one rule.
  • pgqr_truncate(): remove all rules.
  • pgqr_rules(): inspect current shared-memory rules and rewrite counts.

Caveats

  • Matching is exact: case, whitespace, and semicolons must match character-for-character.
  • Parameterized SQL is not supported.
  • Maximum statement length is hard-coded at 32 KB.
  • Rules are not persisted; they vanish on restart unless you reapply them.
Last updated on