pg_query_rewrite

Rewrite SQL statements with a PostgreSQL ProcessUtility hook

Overview

PackageVersionCategoryLicenseLanguage
pg_query_rewrite0.0.5ADMINPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
5030pg_query_rewriteNoYesYesYesNoNo-

Requires shared_preload_libraries=pg_query_rewrite.

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.0.51817161514pg_query_rewrite-
RPMPIGSTY0.0.51817161514pg_query_rewrite_$v-
DEBPIGSTY0.0.51817161514postgresql-$v-pg-query-rewrite-
OS / 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

Build

You can build the RPM / DEB packages for pg_query_rewrite using pig build:

pig build pkg pg_query_rewrite         # build RPM / DEB packages

Install

You can install pg_query_rewrite directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

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

Install the extension using pig or apt/yum/dnf:

pig install pg_query_rewrite;          # Install for current active PG version
pig ext install -y pg_query_rewrite -v 18  # PG 18
pig ext install -y pg_query_rewrite -v 17  # PG 17
pig ext install -y pg_query_rewrite -v 16  # PG 16
pig ext install -y pg_query_rewrite -v 15  # PG 15
pig ext install -y pg_query_rewrite -v 14  # PG 14
dnf install -y pg_query_rewrite_18       # PG 18
dnf install -y pg_query_rewrite_17       # PG 17
dnf install -y pg_query_rewrite_16       # PG 16
dnf install -y pg_query_rewrite_15       # PG 15
dnf install -y pg_query_rewrite_14       # PG 14
apt install -y postgresql-18-pg-query-rewrite   # PG 18
apt install -y postgresql-17-pg-query-rewrite   # PG 17
apt install -y postgresql-16-pg-query-rewrite   # PG 16
apt install -y postgresql-15-pg-query-rewrite   # PG 15
apt install -y postgresql-14-pg-query-rewrite   # PG 14

Preload:

shared_preload_libraries = 'pg_query_rewrite';

Create Extension:

CREATE EXTENSION pg_query_rewrite;

Usage

pg_query_rewrite is a PostgreSQL extension that translates an exact source SQL statement into another predefined SQL statement. It must be loaded at server level with shared_preload_libraries, then installed in each database.

The README shows the extension as a simple statement rewrite engine backed by shared-memory rules.

Setup

shared_preload_libraries = 'pg_query_rewrite'
pg_query_rewrite.max_rules = 10
CREATE EXTENSION pg_query_rewrite;

The README says the extension has been successfully tested with PostgreSQL 9.5 through 18.

Managing Rules

Use the helper functions to manage translations:

SELECT pgqr_add_rule('select 10;', 'select 11;');
SELECT pgqr_remove_rule('select 10;');
SELECT pgqr_truncate();
SELECT pgqr_rules();

The example in the README rewrites select 10; to select 11; and then shows the rule list after insertion.

Behavior

  • Matching is exact and sensitive to case, spaces, and semicolons.
  • Parameterized SQL statements are not supported.
  • The maximum SQL statement length is hard-coded at 32K.
  • Rules live only in shared memory; the extension does not persist settings on its own.
  • pg_query_rewrite.max_rules caps the number of SQL statements that can be translated and defaults to 10 when unset.

Scope

The upstream README is sufficient here: it covers purpose, server-level loading, rule management, a concrete rewrite example, and the main limitations. No separate docs page or homepage was needed.


Last Modified 2026-04-14: update extension catalog (29617e5)