acdat
Checking a text column against thousands of keywords with LIKE ANY, ~ regex alternation, or repeated replace() calls slows down linearly as your rule list grows. acdat compiles an entire dictionary once into an Aho–Corasick automaton stored on a double-array trie (acdat.machine), then evaluates every rule simultaneously in one pass over each input value.
Why use acdat
- Constant-cost rule scaling: Scan time scales with the length of the input string, not the number of keywords—whether your dictionary holds 20 rules or 100,000 rules.
- Exact text and binary matching: Supports both
text(reporting 1-based byte and Unicode character offsets) andbytea(arbitrary binary payloads including\x00). - One-pass deterministic rewriting:
acdat.replace()rewrites overlapping spans according to an explicit policy (leftmost_longestorleftmost_priority) and never rescans already-replaced output. - Managed dictionary lifecycle: Publish immutable dictionary versions, switch active builds atomically via MVCC, or roll back instantly without taking queries offline.
When to use acdat (and when not to)
| Need | Best tool | Why |
|---|---|---|
| Check, tag, or redact many exact literal phrases or binary signatures inside PostgreSQL | acdat | Single-pass scan with stable rule IDs, priorities, and deterministic replacement |
| Search by stemmed natural-language words or relevance ranking | PostgreSQL full-text search / vchord_bm25 | acdat matches exact literal bytes, not linguistic tokens or stem variants |
Match variable patterns (\d{4}-\d{2}, lookaheads, wildcards) | Regular expressions (~ / regexp_replace) | acdat handles literal strings only |
| Filter a tiny fraction of rows out of a huge table by a few substrings | pg_trgm GIN/GiST index (or combine with acdat) | Narrow candidate rows with an index first, then run acdat on the survivors |
How acdat works
flowchart LR
R["Application Rule Table (pattern, id, replacement, priority)"] -->|acdat.compile| M["acdat.machine (Double-Array Trie)"]
M -->|acdat.publish + activate| CAT["acdat.active_machine (MVCC Pointer)"]
CAT -->|acdat.contains| B["boolean (fast filter)"]
CAT -->|acdat.matches| H["SETOF acdat.hit (id & offsets)"]
CAT -->|acdat.replace| T["text / bytea (single-pass rewrite)"]
- Keep rules in your own table: Your application table owns the pattern text, business
pattern_id, optional replacement text, priority, and metadata (such as risk level or category). - Compile once:
acdat.compile()turns arrays or table rows into an immutableacdat.machinevalue. A 1,114-rule real-world dictionary compiles in ~1.2 ms and caches in ~227 KiB per backend. - Publish or pass directly: Store the compiled machine in your own table or publish it to
acdat’s built-in catalog (acdat.publish+acdat.activate) for atomic rollouts and rollbacks. - Scan in one pass: Pass the machine to
acdat.contains()to filter rows,acdat.matches()to extract rule IDs and offsets, oracdat.replace()to redact or rewrite values.
Install acdat and build a complete comment moderation and redaction pipeline in PostgreSQL in three minutes.
Five practical acdat recipes: resolving overlapping phrases, extracting log IOCs with a reverse-lookup index, rolling out versioned dictionaries atomically, scanning binary payloads, and shipping compiled artifacts across databases.
Reference throughput and memory measurements for acdat v0.1.1, how to avoid the 22x parallel-scan planning trap, and sizing rules for production rule sets.
Configure least-privilege roles for scanning vs. publishing dictionaries, plan logical and physical backups, diagnose common errors, and uninstall safely.
Complete SQL API lookup for acdat v0.1.1. All types, functions, and catalog tables live in the fixed acdat schema; always pair text or bytea inputs with a matching machine.