plpgsql_wrap

Oracle WRAP 等价的 PL/pgSQL 语言处理器,以 AES-256-GCM 加密存储过程源码。

概览

扩展包名版本分类许可证语言
plpgsql_wrap1.0SIMPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
9210plpgsql_wrap-
相关扩展plpgsql orafce pg_dbms_metadata pgaudit

PGDG RPM and Pigsty DEB package hexacluster/plpgsql_wrap 1.0; control requires plpgsql and superuser=true; links OpenSSL.

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.01817161514plpgsql_wrapplpgsql
RPMPGDG1.01817161514plpgsql_wrap_$vopenssl-libs
DEBPIGSTY1.01817161514postgresql-$v-plpgsql-wraplibssl3
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u26.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u26.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0

构建

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

pig build pkg plpgsql_wrap         # 构建 RPM / DEB 包

安装

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

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

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

pig install plpgsql_wrap;          # 当前活跃 PG 版本安装
pig ext install -y plpgsql_wrap -v 18  # PG 18
pig ext install -y plpgsql_wrap -v 17  # PG 17
pig ext install -y plpgsql_wrap -v 16  # PG 16
pig ext install -y plpgsql_wrap -v 15  # PG 15
pig ext install -y plpgsql_wrap -v 14  # PG 14
dnf install -y plpgsql_wrap_18       # PG 18
dnf install -y plpgsql_wrap_17       # PG 17
dnf install -y plpgsql_wrap_16       # PG 16
dnf install -y plpgsql_wrap_15       # PG 15
dnf install -y plpgsql_wrap_14       # PG 14
apt install -y postgresql-18-plpgsql-wrap   # PG 18
apt install -y postgresql-17-plpgsql-wrap   # PG 17
apt install -y postgresql-16-plpgsql-wrap   # PG 16
apt install -y postgresql-15-plpgsql-wrap   # PG 15
apt install -y postgresql-14-plpgsql-wrap   # PG 14

创建扩展

CREATE EXTENSION plpgsql_wrap CASCADE;  -- 依赖: plpgsql

用法

来源:READMEv1.0 releasecontrol file

plpgsql_wrap 为 PostgreSQL 提供 Oracle WRAP 风格的 procedural language。使用 LANGUAGE plpgsql_wrap 编写的函数会先按 PL/pgSQL 校验,然后以 PLPGSQLWRAP:1:<hex> 形式加密存储在 pg_proc.prosrc 中。

带 Key 安装

使用 32-byte AES-256-GCM key 构建扩展:

export WRAP_KEY_HEX=$(openssl rand -hex 32)
make WRAP_KEY_HEX=$WRAP_KEY_HEX
sudo make install

备份该 key。只有正确的 compiled key 可用时,wrapped functions 才能安全 unwrap 或 restore。

在每个需要该 language 的数据库中安装扩展:

CREATE EXTENSION plpgsql_wrap; -- requires plpgsql

创建 Wrapped Functions

使用普通 PL/pgSQL 语法,只是 language name 不同:

CREATE OR REPLACE FUNCTION public.calculate_bonus(emp_id int, yr int)
RETURNS numeric
LANGUAGE plpgsql_wrap
AS $$
DECLARE
  v_salary numeric;
BEGIN
  SELECT salary INTO v_salary FROM employees WHERE id = emp_id;
  RETURN v_salary * 0.15;
END;
$$;

存储的函数体不可读:

SELECT substring(prosrc, 1, 32) AS wrapped_code
FROM pg_proc
WHERE proname = 'calculate_bonus';

Dump、Restore 与 Unwrap

pg_dump 会输出加密后的 PLPGSQLWRAP:1: blob。在具有相同 compiled key 的服务器上 restore 可以正常工作。不同 key 会保留 blob,但调用时如果 validator/authentication path 无法认证它,就会失败。

superuser 知道 key 时,可以永久 unwrap 一个函数:

SELECT plpgsql_wrap.unwrap_procedure(
  'myhexkey',
  'public',
  'calculate_bonus',
  'emp_id int, yr int'
);

注意事项

  • 版本 1.0 支持 PostgreSQL 14-18。
  • Control file 要求 plpgsql,并且需要 superuser installation。
  • 这可以防止随意查看源码和 dumps,但 compiled key 是关键 secret。应相应保护 package artifacts 和 build logs。
  • 语法会在加密前校验,因此普通 PL/pgSQL syntax errors 会在写入 encrypted storage 前中止 CREATE FUNCTION

最后修改 2026-07-01: routine extension update (d1ad21a)