跳转到主要内容

acdat

在 PostgreSQL 中编译 Aho-Corasick 双数组机,实现精确多模式匹配与替换

acdat · 在 PostgreSQL 中编译 Aho-Corasick 双数组机,实现精确多模式匹配与替换

概览

ID扩展软件包版本类别许可证编程语言
2260acdatacdat0.1.1FTSApache-2.0C
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d--否是否是否否
关联
模式acdat
相关扩展pg_trgm pg_bigm pgroonga pg_search
0.1.1 preserves the 0.1.0 SQL API and format-major-1 machine compatibility; fixed acdat schema; no preload required.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.1.119 18 17 16 15acdat-
RPMPIGSTY0.1.119 18 17 16 15acdat_$v-
DEBPIGSTY0.1.119 18 17 16 15postgresql-$v-acdat-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el8.aarch64N/APIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el9.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el9.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el10.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el10.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d12.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d12.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d13.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d13.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u22.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u22.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u24.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u24.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u26.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u26.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
软件包版本平台仓库大小下载
acdat_190.1.1el9.x86_64pigsty117.5 KiBacdat_19-0.1.1-1PGSTY.el9.x86_64.rpm
acdat_190.1.1el9.aarch64pigsty116.3 KiBacdat_19-0.1.1-1PGSTY.el9.aarch64.rpm
acdat_190.1.1el10.x86_64pigsty121.4 KiBacdat_19-0.1.1-1PGSTY.el10.x86_64.rpm
acdat_190.1.1el10.aarch64pigsty120.1 KiBacdat_19-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-19-acdat0.1.1d12.x86_64pigsty110.3 KiBpostgresql-19-acdat_0.1.1-1PGSTY~bookworm_amd64.deb
postgresql-19-acdat0.1.1d12.aarch64pigsty107.7 KiBpostgresql-19-acdat_0.1.1-1PGSTY~bookworm_arm64.deb
postgresql-19-acdat0.1.1d13.x86_64pigsty111.6 KiBpostgresql-19-acdat_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-19-acdat0.1.1d13.aarch64pigsty109.3 KiBpostgresql-19-acdat_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-19-acdat0.1.1u22.x86_64pigsty116.3 KiBpostgresql-19-acdat_0.1.1-1PGSTY~jammy_amd64.deb
postgresql-19-acdat0.1.1u22.aarch64pigsty115.0 KiBpostgresql-19-acdat_0.1.1-1PGSTY~jammy_arm64.deb
postgresql-19-acdat0.1.1u24.x86_64pigsty112.2 KiBpostgresql-19-acdat_0.1.1-1PGSTY~noble_amd64.deb
postgresql-19-acdat0.1.1u24.aarch64pigsty111.0 KiBpostgresql-19-acdat_0.1.1-1PGSTY~noble_arm64.deb
postgresql-19-acdat0.1.1u26.x86_64pigsty113.0 KiBpostgresql-19-acdat_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-19-acdat0.1.1u26.aarch64pigsty112.0 KiBpostgresql-19-acdat_0.1.1-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
acdat_180.1.1el8.x86_64pigsty118.3 KiBacdat_18-0.1.1-1PGSTY.el8.x86_64.rpm
acdat_180.1.1el8.aarch64pigsty115.7 KiBacdat_18-0.1.1-1PGSTY.el8.aarch64.rpm
acdat_180.1.1el9.x86_64pigsty117.3 KiBacdat_18-0.1.1-1PGSTY.el9.x86_64.rpm
acdat_180.1.1el9.aarch64pigsty116.4 KiBacdat_18-0.1.1-1PGSTY.el9.aarch64.rpm
acdat_180.1.1el10.x86_64pigsty121.2 KiBacdat_18-0.1.1-1PGSTY.el10.x86_64.rpm
acdat_180.1.1el10.aarch64pigsty120.0 KiBacdat_18-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-18-acdat0.1.1d12.x86_64pigsty110.2 KiBpostgresql-18-acdat_0.1.1-1PGSTY~bookworm_amd64.deb
postgresql-18-acdat0.1.1d12.aarch64pigsty107.6 KiBpostgresql-18-acdat_0.1.1-1PGSTY~bookworm_arm64.deb
postgresql-18-acdat0.1.1d13.x86_64pigsty111.1 KiBpostgresql-18-acdat_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-18-acdat0.1.1d13.aarch64pigsty109.1 KiBpostgresql-18-acdat_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-18-acdat0.1.1u22.x86_64pigsty116.2 KiBpostgresql-18-acdat_0.1.1-1PGSTY~jammy_amd64.deb
postgresql-18-acdat0.1.1u22.aarch64pigsty114.9 KiBpostgresql-18-acdat_0.1.1-1PGSTY~jammy_arm64.deb
postgresql-18-acdat0.1.1u24.x86_64pigsty111.7 KiBpostgresql-18-acdat_0.1.1-1PGSTY~noble_amd64.deb
postgresql-18-acdat0.1.1u24.aarch64pigsty110.8 KiBpostgresql-18-acdat_0.1.1-1PGSTY~noble_arm64.deb
postgresql-18-acdat0.1.1u26.x86_64pigsty112.7 KiBpostgresql-18-acdat_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-18-acdat0.1.1u26.aarch64pigsty111.8 KiBpostgresql-18-acdat_0.1.1-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
acdat_170.1.1el8.x86_64pigsty118.2 KiBacdat_17-0.1.1-1PGSTY.el8.x86_64.rpm
acdat_170.1.1el8.aarch64pigsty115.6 KiBacdat_17-0.1.1-1PGSTY.el8.aarch64.rpm
acdat_170.1.1el9.x86_64pigsty117.3 KiBacdat_17-0.1.1-1PGSTY.el9.x86_64.rpm
acdat_170.1.1el9.aarch64pigsty116.2 KiBacdat_17-0.1.1-1PGSTY.el9.aarch64.rpm
acdat_170.1.1el10.x86_64pigsty121.0 KiBacdat_17-0.1.1-1PGSTY.el10.x86_64.rpm
acdat_170.1.1el10.aarch64pigsty119.8 KiBacdat_17-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-17-acdat0.1.1d12.x86_64pigsty110.1 KiBpostgresql-17-acdat_0.1.1-1PGSTY~bookworm_amd64.deb
postgresql-17-acdat0.1.1d12.aarch64pigsty107.7 KiBpostgresql-17-acdat_0.1.1-1PGSTY~bookworm_arm64.deb
postgresql-17-acdat0.1.1d13.x86_64pigsty111.4 KiBpostgresql-17-acdat_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-17-acdat0.1.1d13.aarch64pigsty109.1 KiBpostgresql-17-acdat_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-17-acdat0.1.1u22.x86_64pigsty122.4 KiBpostgresql-17-acdat_0.1.1-1PGSTY~jammy_amd64.deb
postgresql-17-acdat0.1.1u22.aarch64pigsty121.0 KiBpostgresql-17-acdat_0.1.1-1PGSTY~jammy_arm64.deb
postgresql-17-acdat0.1.1u24.x86_64pigsty111.7 KiBpostgresql-17-acdat_0.1.1-1PGSTY~noble_amd64.deb
postgresql-17-acdat0.1.1u24.aarch64pigsty110.8 KiBpostgresql-17-acdat_0.1.1-1PGSTY~noble_arm64.deb
postgresql-17-acdat0.1.1u26.x86_64pigsty112.7 KiBpostgresql-17-acdat_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-17-acdat0.1.1u26.aarch64pigsty111.8 KiBpostgresql-17-acdat_0.1.1-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
acdat_160.1.1el8.x86_64pigsty118.2 KiBacdat_16-0.1.1-1PGSTY.el8.x86_64.rpm
acdat_160.1.1el8.aarch64pigsty115.6 KiBacdat_16-0.1.1-1PGSTY.el8.aarch64.rpm
acdat_160.1.1el9.x86_64pigsty117.3 KiBacdat_16-0.1.1-1PGSTY.el9.x86_64.rpm
acdat_160.1.1el9.aarch64pigsty116.2 KiBacdat_16-0.1.1-1PGSTY.el9.aarch64.rpm
acdat_160.1.1el10.x86_64pigsty121.0 KiBacdat_16-0.1.1-1PGSTY.el10.x86_64.rpm
acdat_160.1.1el10.aarch64pigsty119.8 KiBacdat_16-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-16-acdat0.1.1d12.x86_64pigsty110.2 KiBpostgresql-16-acdat_0.1.1-1PGSTY~bookworm_amd64.deb
postgresql-16-acdat0.1.1d12.aarch64pigsty107.6 KiBpostgresql-16-acdat_0.1.1-1PGSTY~bookworm_arm64.deb
postgresql-16-acdat0.1.1d13.x86_64pigsty111.4 KiBpostgresql-16-acdat_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-16-acdat0.1.1d13.aarch64pigsty109.1 KiBpostgresql-16-acdat_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-16-acdat0.1.1u22.x86_64pigsty122.3 KiBpostgresql-16-acdat_0.1.1-1PGSTY~jammy_amd64.deb
postgresql-16-acdat0.1.1u22.aarch64pigsty120.9 KiBpostgresql-16-acdat_0.1.1-1PGSTY~jammy_arm64.deb
postgresql-16-acdat0.1.1u24.x86_64pigsty111.8 KiBpostgresql-16-acdat_0.1.1-1PGSTY~noble_amd64.deb
postgresql-16-acdat0.1.1u24.aarch64pigsty110.8 KiBpostgresql-16-acdat_0.1.1-1PGSTY~noble_arm64.deb
postgresql-16-acdat0.1.1u26.x86_64pigsty112.7 KiBpostgresql-16-acdat_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-16-acdat0.1.1u26.aarch64pigsty111.8 KiBpostgresql-16-acdat_0.1.1-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
acdat_150.1.1el8.x86_64pigsty118.8 KiBacdat_15-0.1.1-1PGSTY.el8.x86_64.rpm
acdat_150.1.1el8.aarch64pigsty116.1 KiBacdat_15-0.1.1-1PGSTY.el8.aarch64.rpm
acdat_150.1.1el9.x86_64pigsty119.2 KiBacdat_15-0.1.1-1PGSTY.el9.x86_64.rpm
acdat_150.1.1el9.aarch64pigsty118.1 KiBacdat_15-0.1.1-1PGSTY.el9.aarch64.rpm
acdat_150.1.1el10.x86_64pigsty122.8 KiBacdat_15-0.1.1-1PGSTY.el10.x86_64.rpm
acdat_150.1.1el10.aarch64pigsty121.6 KiBacdat_15-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-15-acdat0.1.1d12.x86_64pigsty110.9 KiBpostgresql-15-acdat_0.1.1-1PGSTY~bookworm_amd64.deb
postgresql-15-acdat0.1.1d12.aarch64pigsty108.2 KiBpostgresql-15-acdat_0.1.1-1PGSTY~bookworm_arm64.deb
postgresql-15-acdat0.1.1d13.x86_64pigsty111.6 KiBpostgresql-15-acdat_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-15-acdat0.1.1d13.aarch64pigsty109.6 KiBpostgresql-15-acdat_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-15-acdat0.1.1u22.x86_64pigsty123.9 KiBpostgresql-15-acdat_0.1.1-1PGSTY~jammy_amd64.deb
postgresql-15-acdat0.1.1u22.aarch64pigsty122.7 KiBpostgresql-15-acdat_0.1.1-1PGSTY~jammy_arm64.deb
postgresql-15-acdat0.1.1u24.x86_64pigsty113.5 KiBpostgresql-15-acdat_0.1.1-1PGSTY~noble_amd64.deb
postgresql-15-acdat0.1.1u24.aarch64pigsty112.6 KiBpostgresql-15-acdat_0.1.1-1PGSTY~noble_arm64.deb
postgresql-15-acdat0.1.1u26.x86_64pigsty114.2 KiBpostgresql-15-acdat_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-15-acdat0.1.1u26.aarch64pigsty113.6 KiBpostgresql-15-acdat_0.1.1-1PGSTY~resolute_arm64.deb

源码

pig build pkg acdat;		# build rpm/deb

安装

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

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

使用 pig 安装扩展。

pig install acdat;		# install by package name

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

在数据库中创建扩展。

CREATE EXTENSION acdat;

用法

来源:

acdat 0.1.1 将大规模精确字面量模式词典编译为不可变的 Aho-Corasick Double-Array machine,再对每个 text 或 bytea 值执行一次扫描以完成匹配或替换。它适合策略规则、失陷指标、实体名称、脱敏别名等稳定且会被反复使用的词典。

核心流程

创建扩展、编译词典,并在大量输入上复用生成的 acdat.machine 值:

CREATE EXTENSION acdat;

WITH machine AS (
    SELECT acdat.compile(
        ARRAY['he', 'she', 'his', 'hers'],
        ARRAY[1, 2, 3, 4]::bigint[]
    ) AS value
)
SELECT acdat.contains('ushers', value) AS matched,
       acdat.info(value)->>'pattern_count' AS patterns
FROM machine;

生产词典的源规则应保存在应用自有表中。acdat.compile() 的聚合重载可以直接从模式、ID、替换值和优先级记录构建确定性的 machine;一次编译后即可扫描大量输入。

匹配与替换

acdat.contains() 在首次命中后停止。acdat.matches() 返回 acdat.hit 行,其中包含模式 ID、字节与字符坐标以及优先级。acdat.replace() 执行字面量、非递归替换:

WITH machine AS (
    SELECT acdat.compile(
        ARRAY['病毒', '特征码', '病毒特征码'],
        ARRAY[10, 11, 12]::bigint[],
        ARRAY['[VIRUS]', '[SIGNATURE]', '[IOC]'],
        ARRAY[20, 20, 5]::integer[]
    ) AS value
)
SELECT *
FROM acdat.matches('发现病毒特征码', (SELECT value FROM machine), 'all_overlapping');

SELECT acdat.replace(
    'aaa',
    acdat.compile(
        ARRAY['a', 'aa', 'aaa'],
        ARRAY[1, 2, 3]::bigint[],
        ARRAY['[x]', '[yy]', '[zzz]']
    ),
    'leftmost_longest'
);

匹配策略包括 all_overlapping、leftmost_longest 和 leftmost_priority。替换只能使用非重叠策略。可通过 acdat.info() 检查已编译 machine,并在移动或校验产物时使用导出、验证、导入和指纹函数。

acdat.matches() 的 max_matches 默认值为 10000,acdat.replace() 的 max_output_bytes 默认值为 268435456。对于不可信或高命中输入,应设置更严格的上限,确保命中枚举和替换输出有界。

托管词典

可选的目录层用于发布不可变、内容寻址的 build,并以原子方式选择一个活动 build。其控制函数使用 SECURITY INVOKER,且不向 PUBLIC 授予执行权限:

WITH machine AS (
    SELECT acdat.compile(pattern, pattern_id)
    FROM app_keyword
    WHERE enabled
), published AS (
    SELECT acdat.publish('moderation', 1, machine) AS build_id
    FROM machine
)
SELECT acdat.activate('moderation', build_id)
FROM published;

SELECT name, version, build_id, machine
FROM acdat.active_machine
WHERE name = 'moderation';

应用表始终是事实源。逻辑备份包含目录元数据和活动 machine 载荷,但不会包含所有历史产物,因此应保留重建已退休或非活动版本所需的源模式。

兼容性与安全

0.1.1 已在 PostgreSQL 14 至 18 上测试,不需要预加载或重启服务器,没有外部扩展依赖,也不定义 GUC。控制文件将 schema 固定为 acdat,并设置 relocatable = false 和 trusted = false,因此 CREATE EXTENSION 需要超级用户。

0.1.1 保持 0.1.0 SQL API 与 format-major-1 machine 兼容,并提供 0.1.0 -> 0.1.1 扩展升级路径;同时加入可取消编译、保守的构建工作量预算和更快的物化扫描路径,而不改变持久化 machine 契约。

ACDAT 索引的是模式词典,而不是文档表:扫描已有大表时仍需读取候选行。匹配是精确且区分大小写的;扩展不提供正则表达式、模糊匹配、分词、自动大小写折叠、Unicode 规范化或文档侧索引。文本引擎支持 UTF-8 和单字节服务器编码,二进制数据应使用 bytea 接口。需要反复反向查询时,应将 (document_id, pattern_id) 命中物化到应用表中。

编译格式具备自描述和校验和,导入的产物会在使用前接受验证。卸载前应清点依赖:DROP EXTENSION acdat 会删除托管词典状态,而添加 CASCADE 还可能删除依赖 acdat.machine 的用户列或其他对象。

表格阅读器