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
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

构建

您可以使用 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;

用法

来源:README

pg_query_rewrite 会把一个完全匹配的源 SQL 语句重写为预定义的目标语句,规则存储在共享内存中。

所需设置

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

CREATE EXTENSION pg_query_rewrite;

规则管理

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):添加一条重写规则。
  • pgqr_remove_rule(source):删除一条规则。
  • pgqr_truncate():删除全部规则。
  • pgqr_rules():查看当前共享内存中的规则和重写计数。

注意事项

  • 匹配是精确的:大小写、空白和分号都必须逐字符一致。
  • 不支持参数化 SQL。
  • 最大语句长度硬编码为 32 KB。
  • 规则不会持久化;重启后会消失,除非你重新应用。

最后修改 2026-05-01: update extension data (e399d22)