# acdat

> Compile hundreds to hundreds of thousands of literal patterns into a compact Aho–Corasick automaton and scan PostgreSQL text or bytea values in a single linear pass. Ideal for content moderation, log IOC tagging, entity extraction, and bulk redaction.

---

LLMS index: [llms.txt](/llms.txt)

---

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 {#why}

- **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) and `bytea` (arbitrary binary payloads including `\x00`).
- **One-pass deterministic rewriting**: `acdat.replace()` rewrites overlapping spans according to an explicit policy (`leftmost_longest` or `leftmost_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) {#choose}

| 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 {#mechanism}

```mermaid
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)"]
```

1. **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).
2. **Compile once**: `acdat.compile()` turns arrays or table rows into an immutable `acdat.machine` value. A 1,114-rule real-world dictionary compiles in ~1.2 ms and caches in ~227 KiB per backend.
3. **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.
4. **Scan in one pass**: Pass the machine to `acdat.contains()` to filter rows, `acdat.matches()` to extract rule IDs and offsets, or `acdat.replace()` to redact or rewrite values.
{.steps}

---

Section pages:

- [Getting started with acdat](/docs/acdat/getting-started/): Install acdat and build a complete comment moderation and redaction pipeline in PostgreSQL in three minutes.
- [Examples and workflows](/docs/acdat/examples/): 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.
- [Performance and tuning](/docs/acdat/performance/): 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.
- [Operations](/docs/acdat/operations/): Configure least-privilege roles for scanning vs. publishing dictionaries, plan logical and physical backups, diagnose common errors, and uninstall safely.
- [acdat SQL reference](/docs/acdat/reference/): 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.
