pg_query_rewrite

使用 ProcessUtility hook 重写 SQL 语句

概览

扩展包名版本分类许可证语言
pg_query_rewrite0.0.5ADMINPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
5030pg_query_rewrite-

Requires shared_preload_libraries=pg_query_rewrite.

版本

类型仓库版本PG 大版本包名依赖
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

构建

您可以使用 pig build 命令构建 pg_query_rewrite 扩展的 RPM / DEB 包:

pig build pkg pg_query_rewrite         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_query_rewrite 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_query_rewrite;          # 当前活跃 PG 版本安装
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

预加载配置

shared_preload_libraries = 'pg_query_rewrite';

创建扩展

CREATE EXTENSION pg_query_rewrite;

用法

pg_query_rewrite 是一个 PostgreSQL 扩展,可以把一条精确匹配的源 SQL 语句改写为另一条预定义 SQL 语句。它必须先通过 shared_preload_libraries 在服务器级加载,然后再在每个数据库中安装。

README 将它描述为一个由共享内存规则驱动的简易语句改写引擎。

安装

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

README 说明该扩展已在 PostgreSQL 9.5 到 18 之间通过测试。

管理规则

使用辅助函数管理改写规则:

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

README 中的示例会把 select 10; 改写为 select 11;,然后展示插入后的规则列表。

行为

  • 匹配是精确匹配,对大小写、空格和分号都敏感。
  • 不支持参数化 SQL 语句。
  • SQL 语句长度上限硬编码为 32K。
  • 规则只存在于共享内存中;扩展本身不会持久化这些设置。
  • pg_query_rewrite.max_rules 限制可改写的 SQL 语句数量,未设置时默认值为 10

范围

上游 README 已足够说明这个扩展的用途、服务器级加载方式、规则管理、具体改写示例和主要限制,因此不需要额外的文档页或主页。


最后修改 2026-04-14: update extension catalog (fa7cf58)