跳转到主要内容

anon

数据匿名化处理工具

pg_anon · 数据匿名化处理工具

概览

ID扩展软件包版本类别许可证编程语言
7070anonpg_anon3.4.0SECPostgreSQLRust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--sLd--否是是是否否
Save masking rules before dropping and recreating the extension.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY3.4.019 18 17 16 15pg_anon-
RPMPIGSTY3.4.019 18 17 16 15pg_anon_$v-
DEBPIGSTY3.4.019 18 17 16 15postgresql-$v-pg-anon-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
el8.aarch64N/APIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
el9.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
el9.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
el10.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
el10.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
d12.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
d12.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
d13.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
d13.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
u22.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
u22.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
u24.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
u24.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
u26.x86_64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
u26.aarch64PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0PIGSTY 3.4.0
软件包版本平台仓库大小下载
pg_anon_183.4.0el8.x86_64pigsty3.4 MiBpg_anon_18-3.4.0-1PGSTY.el8.x86_64.rpm
pg_anon_183.4.0el8.aarch64pigsty3.0 MiBpg_anon_18-3.4.0-1PGSTY.el8.aarch64.rpm
pg_anon_183.4.0el9.x86_64pigsty3.1 MiBpg_anon_18-3.4.0-1PGSTY.el9.x86_64.rpm
pg_anon_183.4.0el9.aarch64pigsty3.0 MiBpg_anon_18-3.4.0-1PGSTY.el9.aarch64.rpm
pg_anon_183.4.0el10.x86_64pigsty3.1 MiBpg_anon_18-3.4.0-1PGSTY.el10.x86_64.rpm
pg_anon_183.4.0el10.aarch64pigsty3.0 MiBpg_anon_18-3.4.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-anon3.4.0d12.x86_64pigsty2.8 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-anon3.4.0d12.aarch64pigsty2.5 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-anon3.4.0d13.x86_64pigsty2.8 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-anon3.4.0d13.aarch64pigsty2.5 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-anon3.4.0u22.x86_64pigsty3.1 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-anon3.4.0u22.aarch64pigsty2.9 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-anon3.4.0u24.x86_64pigsty3.0 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-anon3.4.0u24.aarch64pigsty2.9 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-anon3.4.0u26.x86_64pigsty3.0 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-anon3.4.0u26.aarch64pigsty2.8 MiBpostgresql-18-pg-anon_3.4.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_anon_173.4.0el8.x86_64pigsty3.4 MiBpg_anon_17-3.4.0-1PGSTY.el8.x86_64.rpm
pg_anon_173.4.0el8.aarch64pigsty3.0 MiBpg_anon_17-3.4.0-1PGSTY.el8.aarch64.rpm
pg_anon_173.4.0el9.x86_64pigsty3.1 MiBpg_anon_17-3.4.0-1PGSTY.el9.x86_64.rpm
pg_anon_173.4.0el9.aarch64pigsty3.0 MiBpg_anon_17-3.4.0-1PGSTY.el9.aarch64.rpm
pg_anon_173.4.0el10.x86_64pigsty3.1 MiBpg_anon_17-3.4.0-1PGSTY.el10.x86_64.rpm
pg_anon_173.4.0el10.aarch64pigsty3.0 MiBpg_anon_17-3.4.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-anon3.4.0d12.x86_64pigsty2.8 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-anon3.4.0d12.aarch64pigsty2.5 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-anon3.4.0d13.x86_64pigsty2.8 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-anon3.4.0d13.aarch64pigsty2.5 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-anon3.4.0u22.x86_64pigsty3.1 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-anon3.4.0u22.aarch64pigsty2.9 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-anon3.4.0u24.x86_64pigsty3.0 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-anon3.4.0u24.aarch64pigsty2.9 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-anon3.4.0u26.x86_64pigsty3.0 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-anon3.4.0u26.aarch64pigsty2.8 MiBpostgresql-17-pg-anon_3.4.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_anon_163.4.0el8.x86_64pigsty3.4 MiBpg_anon_16-3.4.0-1PGSTY.el8.x86_64.rpm
pg_anon_163.4.0el8.aarch64pigsty3.0 MiBpg_anon_16-3.4.0-1PGSTY.el8.aarch64.rpm
pg_anon_163.4.0el9.x86_64pigsty3.1 MiBpg_anon_16-3.4.0-1PGSTY.el9.x86_64.rpm
pg_anon_163.4.0el9.aarch64pigsty3.0 MiBpg_anon_16-3.4.0-1PGSTY.el9.aarch64.rpm
pg_anon_163.4.0el10.x86_64pigsty3.1 MiBpg_anon_16-3.4.0-1PGSTY.el10.x86_64.rpm
pg_anon_163.4.0el10.aarch64pigsty3.0 MiBpg_anon_16-3.4.0-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-anon3.4.0d12.x86_64pigsty2.8 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-anon3.4.0d12.aarch64pigsty2.5 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-anon3.4.0d13.x86_64pigsty2.8 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~trixie_amd64.deb
postgresql-16-pg-anon3.4.0d13.aarch64pigsty2.5 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~trixie_arm64.deb
postgresql-16-pg-anon3.4.0u22.x86_64pigsty3.1 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~jammy_amd64.deb
postgresql-16-pg-anon3.4.0u22.aarch64pigsty2.9 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~jammy_arm64.deb
postgresql-16-pg-anon3.4.0u24.x86_64pigsty3.0 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~noble_amd64.deb
postgresql-16-pg-anon3.4.0u24.aarch64pigsty2.8 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~noble_arm64.deb
postgresql-16-pg-anon3.4.0u26.x86_64pigsty3.0 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~resolute_amd64.deb
postgresql-16-pg-anon3.4.0u26.aarch64pigsty2.8 MiBpostgresql-16-pg-anon_3.4.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_anon_153.4.0el8.x86_64pigsty3.4 MiBpg_anon_15-3.4.0-1PGSTY.el8.x86_64.rpm
pg_anon_153.4.0el8.aarch64pigsty3.0 MiBpg_anon_15-3.4.0-1PGSTY.el8.aarch64.rpm
pg_anon_153.4.0el9.x86_64pigsty3.1 MiBpg_anon_15-3.4.0-1PGSTY.el9.x86_64.rpm
pg_anon_153.4.0el9.aarch64pigsty3.0 MiBpg_anon_15-3.4.0-1PGSTY.el9.aarch64.rpm
pg_anon_153.4.0el10.x86_64pigsty3.1 MiBpg_anon_15-3.4.0-1PGSTY.el10.x86_64.rpm
pg_anon_153.4.0el10.aarch64pigsty3.0 MiBpg_anon_15-3.4.0-1PGSTY.el10.aarch64.rpm
postgresql-15-pg-anon3.4.0d12.x86_64pigsty2.8 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~bookworm_amd64.deb
postgresql-15-pg-anon3.4.0d12.aarch64pigsty2.5 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~bookworm_arm64.deb
postgresql-15-pg-anon3.4.0d13.x86_64pigsty2.8 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~trixie_amd64.deb
postgresql-15-pg-anon3.4.0d13.aarch64pigsty2.5 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~trixie_arm64.deb
postgresql-15-pg-anon3.4.0u22.x86_64pigsty3.1 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~jammy_amd64.deb
postgresql-15-pg-anon3.4.0u22.aarch64pigsty2.9 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~jammy_arm64.deb
postgresql-15-pg-anon3.4.0u24.x86_64pigsty3.0 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~noble_amd64.deb
postgresql-15-pg-anon3.4.0u24.aarch64pigsty2.9 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~noble_arm64.deb
postgresql-15-pg-anon3.4.0u26.x86_64pigsty3.0 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~resolute_amd64.deb
postgresql-15-pg-anon3.4.0u26.aarch64pigsty2.8 MiBpostgresql-15-pg-anon_3.4.0-1PGSTY~resolute_arm64.deb

源码

pig build pkg pg_anon;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

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

使用 pig 安装扩展。

pig install pg_anon;		# install by package name
pig install anon;		# install by extension name

pig install anon -v 19;   # install for PG 19
pig install anon -v 18;   # install for PG 18
pig install anon -v 17;   # install for PG 17
pig install anon -v 16;   # install for PG 16
pig install anon -v 15;   # install for PG 15

将扩展加入 shared_preload_libraries,详见配置说明。

shared_preload_libraries = 'anon';

在数据库中创建扩展。

CREATE EXTENSION anon;

用法

来源:

anon 是 PostgreSQL Anonymizer,它应用声明式的遮掩规则以实现受保护查询的访问,并生成匿名化数据集。同时提供伪名化和随机响应辅助功能。在必须保持现实数据有用性而不暴露原始敏感值时使用它;将遮掩策略、角色授权以及未遮掩数据库的访问视为安全边界的一部分。

核心工作流程

在目标数据库会话中加载 anon,安装扩展并启用透明动态遮掩。新连接会自动获取数据库级别的设置。

ALTER DATABASE app SET session_preload_libraries = 'anon';

\connect app
CREATE EXTENSION anon;
ALTER DATABASE app SET anon.transparent_dynamic_masking = true;

将登录标记为被遮掩,并将遮掩规则附加到敏感列上:

CREATE ROLE reporting LOGIN;
SECURITY LABEL FOR anon ON ROLE reporting IS 'MASKED';
GRANT USAGE ON SCHEMA public TO reporting;
GRANT SELECT ON TABLE public.customer TO reporting;

SECURITY LABEL FOR anon ON COLUMN customer.last_name
  IS 'MASKED WITH FUNCTION anon.dummy_last_name()';
SECURITY LABEL FOR anon ON COLUMN customer.phone
  IS 'MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)';

以 reporting 身份执行的查询会看到变换后的值。特权用户仍然可以看到原始值,因此不要授予被遮掩的角色绕过策略的路径。

遮掩策略

  • 动态遮掩 对标记为 MASKED 的角色转换结果而无需重写表。
  • 静态遮掩 永久性地重写选定的数据,并适用于一次性开发或测试副本。
  • 匿名导出和复制体 生成经过净化的导出或下游副本。
  • 遮掩视图和数据包装器 展示一个故意减少或转换后的投影。
  • 伪名化 在必须保持一致性的连接或重复值时使用确定性变换。

关键对象

  • anon.dummy_*、anon.random_* 和 anon.partial(...) 生成或部分遮掩值。
  • anon.hash(text) 和 anon.digest(text, text, text) 提供确定性转换。在 3.1.2 版本中,它们被标记为 RESTRICTED 以限制暴力破解暴露。
  • anon.ldp_grrm(value, epsilon, max_v) 和 anon.ldp_grrm_pttt(value, truth_probability, max_v) 实现了局部差分隐私的通用随机响应。
  • anon.ldp_truth_probability(...) 和 anon.ldp_lie_probability(...) 用于检查随机响应概率。
  • 角色和列上的安全标签定义谁被遮掩以及每个值如何转换。

运营注意事项

anon 是超级用户安装且不可重定位的。使用与预期消费者相同的授权和连接路径测试每项策略。随机化不是自动确定性的;当需要稳定相等时,请使用确认的伪名化函数。静态匿名化是破坏性的,因此在副本上运行它并在之后验证约束条件和应用程序行为。

从旧版本升级到 3.4.0

先保存角色、列、表上的脱敏规则和授权。安装 3.4.0 的库以后,在每个使用扩展的数据库按上游指南删除并重建 anon,重新应用规则和授权,再以实际脱敏角色核对结果。上游不提供 ALTER EXTENSION ... UPDATE 路径。

透明动态脱敏对 CREATE TABLE AS 和 SELECT INTO 的结果同样生效。脱敏角色无法创建或刷新物化视图。只为角色授予所需表的整表读取权限,并限制模式上的 CREATE 权限。

anon.seeded_* 和 anon.pseudo_* 的种子参数只接受 TEXT 或 BIGINT。例如,用 UUID 作为种子的规则可显式转换其类型。

SECURITY LABEL FOR anon ON COLUMN customer.email
  IS 'MASKED WITH FUNCTION anon.seeded_email(user_uuid::TEXT)';

对日期和时间先选定与会话设置无关的表示。例如日期可用 to_char(birth_date, 'YYYY-MM-DD'),时间点可用 extract(epoch FROM created_at)::BIGINT。上游计划于 4.0 移除旧的伪名化函数。

3.3.2 版本

3.3.2 是 3.3 安全系列的后续发布,补回了缺失的 ARM 构建。3.3 系列禁止遮掩规则中的子查询,关闭透明动态遮掩的内联,并拒绝让被遮掩角色执行 SQL 标准函数体函数,同时修改 anon.salt。该系列没有就地升级路径:应按上游说明保存并迁移遮掩规则,然后在各数据库删除并重新创建扩展。

3.2.0 引入可本地化的确定性 anon.seeded_* 函数,旧的 anon.pseudo_* 系列已弃用。该版修复了上游报告的三项安全漏洞,并默认拒绝由超级用户执行脱敏操作;应改用专用的非超级用户脱敏角色。将 anon.nosuperuser 设为 false 会撤销这一屏障,不是推荐的升级路径。脱敏规则 JSON 格式已改变,升级后应重新导出规则。

3.2.3 是针对 CVE-2026-97469 的安全更新:动态脱敏现在会检查子查询中的 RESTRICTED 函数。升级已安装的库,然后在各数据库按上游流程删除并重新创建扩展;不支持 ALTER EXTENSION ... UPDATE。重新创建前应保存并迁移脱敏规则;应检查此前通过子查询访问受限函数的脱敏角色查询。

表格阅读器