Skip to content
pg_query_rewrite

pg_query_rewrite

pg_query_rewrite : Rewrite SQL statements with a PostgreSQL ProcessUtility hook

Overview

ID Extension Package Version Category License Language
5030
pg_query_rewrite
pg_query_rewrite
0.0.5
ADMIN
PostgreSQL
C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--sLd--
No
Yes
Yes
Yes
no
no

Requires shared_preload_libraries=pg_query_rewrite.

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
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 / PG PG18 PG17 PG16 PG15 PG14
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
Package Version OS ORG SIZE File URL
pg_query_rewrite_18 0.0.5 el8.x86_64 pigsty 18.8 KiB pg_query_rewrite_18-0.0.5-1PIGSTY.el8.x86_64.rpm
pg_query_rewrite_18 0.0.5 el8.aarch64 pigsty 19.0 KiB pg_query_rewrite_18-0.0.5-1PIGSTY.el8.aarch64.rpm
pg_query_rewrite_18 0.0.5 el9.x86_64 pigsty 18.8 KiB pg_query_rewrite_18-0.0.5-1PIGSTY.el9.x86_64.rpm
pg_query_rewrite_18 0.0.5 el9.aarch64 pigsty 18.9 KiB pg_query_rewrite_18-0.0.5-1PIGSTY.el9.aarch64.rpm
pg_query_rewrite_18 0.0.5 el10.x86_64 pigsty 18.9 KiB pg_query_rewrite_18-0.0.5-1PIGSTY.el10.x86_64.rpm
pg_query_rewrite_18 0.0.5 el10.aarch64 pigsty 19.1 KiB pg_query_rewrite_18-0.0.5-1PIGSTY.el10.aarch64.rpm
postgresql-18-pg-query-rewrite 0.0.5 d12.x86_64 pigsty 24.1 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~bookworm_amd64.deb
postgresql-18-pg-query-rewrite 0.0.5 d12.aarch64 pigsty 24.0 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~bookworm_arm64.deb
postgresql-18-pg-query-rewrite 0.0.5 d13.x86_64 pigsty 24.1 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~trixie_amd64.deb
postgresql-18-pg-query-rewrite 0.0.5 d13.aarch64 pigsty 24.1 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~trixie_arm64.deb
postgresql-18-pg-query-rewrite 0.0.5 u22.x86_64 pigsty 25.5 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~jammy_amd64.deb
postgresql-18-pg-query-rewrite 0.0.5 u22.aarch64 pigsty 25.6 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~jammy_arm64.deb
postgresql-18-pg-query-rewrite 0.0.5 u24.x86_64 pigsty 25.1 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~noble_amd64.deb
postgresql-18-pg-query-rewrite 0.0.5 u24.aarch64 pigsty 25.2 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~noble_arm64.deb
postgresql-18-pg-query-rewrite 0.0.5 u26.x86_64 pigsty 25.0 KiB postgresql-18-pg-query-rewrite_0.0.5-1PIGSTY~resolute_amd64.deb
postgresql-18-pg-query-rewrite 0.0.5 u26.aarch64 pigsty 25.1 KiB postgresql-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