Skip to content

acdat

Compiled Aho-Corasick double-array machines for exact multi-pattern matching and replacement in PostgreSQL

acdat · Compiled Aho-Corasick double-array machines for exact multi-pattern matching and replacement in PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2260acdatacdat0.1.1FTSApache-2.0C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--NoYesNoYesnono
Relationships
Schemasacdat
See Alsopg_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.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
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
PackageVersionOSORGSIZEFile URL
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

Source

pig build pkg acdat;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with 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 this extension with:

CREATE EXTENSION acdat;

Usage

Sources:

acdat 0.1.1 compiles a large dictionary of exact literal patterns into an immutable Aho-Corasick Double-Array machine, then scans each text or bytea value once for matching or replacement. It is designed for stable, repeatedly used dictionaries such as policy rules, indicators of compromise, entity names, and redaction aliases.

Core Workflow

Create the extension, compile a dictionary, and reuse the resulting acdat.machine value across many inputs:

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;

For production dictionaries, the source rules should stay in an application-owned table. The aggregate overload of acdat.compile() can build one deterministic machine directly from pattern, ID, replacement, and priority rows; compile once and scan many values.

Matching and Replacement

acdat.contains() stops after the first hit. acdat.matches() returns acdat.hit rows with the pattern ID, byte and character coordinates, and priority. acdat.replace() applies literal, non-recursive replacements:

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'
);

The match policies are all_overlapping, leftmost_longest, and leftmost_priority. Replacement accepts only a non-overlapping policy. Use acdat.info() to inspect a compiled machine and the export, validation, import, and fingerprint functions when moving or checking artifacts.

acdat.matches() defaults max_matches to 10000, and acdat.replace() defaults max_output_bytes to 268435456. Set tighter limits for untrusted or high-hit inputs so match enumeration and replacement output stay bounded.

Managed Dictionaries

The optional catalog layer publishes immutable, content-addressed builds and atomically selects one active build. Its control functions use SECURITY INVOKER and are not executable by 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';

Application tables remain the source of truth. Logical dumps include catalog metadata and active machine payloads, but not every historical artifact, so retain the source patterns required to rebuild retired or inactive versions.

Compatibility and Safety

Version 0.1.1 is tested on PostgreSQL 14 through 18. It needs no preload or server restart, has no external extension dependency, and defines no GUC. The control file fixes the schema to acdat, sets relocatable = false and trusted = false, so CREATE EXTENSION requires a superuser.

The 0.1.1 release preserves the 0.1.0 SQL API and format-major-1 machine compatibility and ships the 0.1.0 -> 0.1.1 extension update path. It also adds cancellable compilation, a conservative build-work budget, and a faster materialized scan path without changing the stored machine contract.

ACDAT indexes the pattern dictionary, not the document table: scanning a large existing table still reads its candidate rows. Matching is exact and case-sensitive; the extension does not provide regular expressions, fuzzy matching, tokenization, automatic case folding, Unicode normalization, or a document-side index. The text engine supports UTF-8 and single-byte server encodings, while binary data should use the bytea interface. Materialize (document_id, pattern_id) hits into an application table when repeated reverse lookup is required.

The compiled format is self-describing and checksummed, and imported artifacts are validated before use. Inventory dependencies before uninstalling: DROP EXTENSION acdat removes managed dictionary state, while adding CASCADE can also remove user columns or other objects that depend on acdat.machine.

Table reader