pre_prepare

在服务端预先准备好PreparedStatement备用

概览

扩展包名版本分类许可证语言
preprepare0.9ADMINPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
5170pre_prepare-
相关扩展pg_store_plans auto_explain pg_stat_statements plpgsql_check pg_show_plans pg_qualstats pg_hint_plan pgaudit

版本

类型仓库版本PG 大版本包名依赖
EXTMIXED0.91817161514preprepare-
RPMPIGSTY0.91817161514preprepare_$v-
DEBPGDG0.91817161514postgresql-$v-preprepare-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
d12.aarch64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
d13.x86_64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
d13.aarch64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
u22.x86_64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
u22.aarch64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
u24.x86_64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
u24.aarch64
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9
PGDG 0.9

构建

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

pig build pkg preprepare         # 构建 RPM 包

安装

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

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

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

pig install preprepare;          # 当前活跃 PG 版本安装
pig ext install -y preprepare -v 18  # PG 18
pig ext install -y preprepare -v 17  # PG 17
pig ext install -y preprepare -v 16  # PG 16
pig ext install -y preprepare -v 15  # PG 15
pig ext install -y preprepare -v 14  # PG 14
dnf install -y preprepare_18       # PG 18
dnf install -y preprepare_17       # PG 17
dnf install -y preprepare_16       # PG 16
dnf install -y preprepare_15       # PG 15
dnf install -y preprepare_14       # PG 14
apt install -y postgresql-18-preprepare   # PG 18
apt install -y postgresql-17-preprepare   # PG 17
apt install -y postgresql-16-preprepare   # PG 16
apt install -y postgresql-15-preprepare   # PG 15
apt install -y postgresql-14-preprepare   # PG 14

创建扩展

CREATE EXTENSION pre_prepare;

用法

pre_prepare: 在服务器端预准备你的语句

pre_prepare 在连接建立时自动准备 SQL 语句,使客户端可以直接使用 EXECUTE 而无需先调用 PREPARE

设置

postgresql.conf 中配置:

preprepare.relation = 'preprepare.statements'
preprepare.at_init = on    -- 连接时自动准备(需要 local_preload_libraries)

创建存储语句的表:

CREATE TABLE preprepare.statements (name text PRIMARY KEY, statement text);

插入语句(包含完整的 PREPARE 语法):

INSERT INTO preprepare.statements VALUES ('test', 'prepare test as select 1');

函数

prepare_all() – 从配置的关系中准备所有语句:

SELECT prepare_all();

prepare_all('schema.table') – 从指定表准备语句:

SELECT prepare_all('public.expensive_planning');

discard() – 类似 DISCARD ALL 但不执行 DEALLOCATE ALL(保留已准备的语句):

SELECT discard();

配合 PgBouncer 使用

设置 connect_query 在每次服务器连接时自动准备:

[databases]
foo = port=5432 connect_query='SELECT prepare_all();'

避免使用 DISCARD ALL 作为 reset_query(它会释放已准备的语句)。


最后修改 2026-03-14: update extension metadata (953cbd0)