Examples and workflows
Every example below is self-contained and runs inside PostgreSQL using acdat v0.1.1.
1. Overlapping rules and redaction policies
Real dictionaries almost always contain overlapping phrases—for instance, "credit card", "credit card fraud", and "fraud". acdat resolves overlaps deterministically using three policies:
all_overlapping(default formatches): reports every candidate hit, including prefixes, suffixes, and duplicate text with distinct IDs.leftmost_longest(default forreplace): consumes the leftmost match; at the same start offset, prefers the longer pattern, then lower priority number, then lower ID.leftmost_priority: consumes the leftmost match; at the same start offset, prefers the lower priority number first (e.g., priority10beats priority20), then longer pattern, then lower ID.
Why the two results differ:
- Under
leftmost_longest,"credit card fraud"(17 bytes) beats"credit card"(11 bytes) at byte 8 and consumes the entire span. - Under
leftmost_priority,"credit card"(priority10) beats"credit card fraud"(priority20) at byte 8; scanning then resumes after byte 18 and also matches"fraud".
Practical rules for acdat.replace():
- Delete spans with
'': An empty replacement string deletes the matched span; selecting a rule whose replacement isNULLraises an error (acdat pattern ID <n> has no replacement). - Single non-recursive pass: Replacement output is never rescanned, so replacing
"foo"with"foobar"cannot loop. - Guard against expansion: Pass explicit
max_replacements(default10000) andmax_output_bytes(default256 MiB) when short inputs could expand into large outputs. The candidate limit counts raw overlaps before policy resolution, and an outer SQLLIMITdoes not cap it.
2. Extract log IOCs and build a reverse-lookup table
When auditing security logs or tagging documents against a large IOC (Indicator of Compromise) list, you often need two answers: “which indicators hit this log?” and later “which logs matched indicator #402?”
Notice log #3 has mixed casing (PowerShell -Enc). Because acdat compares exact UTF-8 bytes without case folding or Unicode normalization, normalize both rules and input identically (for example with lower()) when case-insensitive matching is required:
With the index on (pattern_id, log_id), reverse lookups (WHERE pattern_id = 402) are instant. In production, store dictionary_version and build_id alongside each materialized hit so you know which rule build produced it and can rebuild cleanly when rules change.
Offsets are 1-based. For UTF-8 text, start_byte/byte_length measure UTF-8 bytes while start_char/char_length measure Unicode code points (which plug directly into PostgreSQL substring(... FROM start_char FOR char_length)). Always add ORDER BY if you need stable output order.
3. Publish, atomically switch, and roll back dictionaries
For production dictionaries that change over time, use acdat’s built-in catalog instead of overwriting a single row. Publishing stores an immutable semantic version and returns a 32-byte build_id; activating flips a pointer in acdat.active_dictionary atomically under MVCC so concurrent readers switch cleanly without locks.
If version 2 triggers false positives, roll back immediately by reactivating version 1, then retire version 2:
Keep these compilation rules in mind:
- Parallel arrays passed to
acdat.compile()must be 1D and equal in length; patterns must be non-empty; explicit IDs must be non-NULL and unique (duplicate literal strings with distinct IDs are allowed); NULL priorities default to0. - An aggregate over zero rows returns
NULL; an explicitly typed empty array (ARRAY[]::text[]) builds an empty machine. - Canonical sorting makes
source_fingerprint()independent of input row order when rule IDs are stable.
4. Scan and patch binary payloads (bytea)
Use the bytea overloads to match protocol headers, file magic bytes, or raw signatures containing \x00 or non-UTF-8 octets. Binary hits always return NULL for start_char and char_length.
Text machines reject bytea inputs and vice versa (acdat machine input kind does not match function input). On databases using encodings other than UTF8 or single-byte encodings, use bytea.
5. Inspect and ship compiled machines across databases
If you compile a 100,000-pattern dictionary in CI or on a build server, you can inspect its metadata and ship the compiled bytea artifact to production databases without recompiling there.
Export on the build database, validate and import on the target database:
source_fingerprint()hashes the canonical rule set;build_fingerprint()also includes engine, compiler, and format version;artifact_checksum()hashes the serialized payload.validate()returnsfalseon corrupted bytes whileimport()raises an error.- Transfer
textmachines only between databases with compatible server encodings;byteamachines are encoding-independent.