# acdat SQL 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.

---

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

---

## Compile a dictionary {#build}

| Name | Type | Default | Meaning |
| --- | --- | --- | --- |
| `acdat.compile(patterns text[], pattern_ids bigint[], replacements text[], priorities integer[], options jsonb)` | `acdat.machine` | `NULL, NULL, NULL, '{}'`¹ | Compile parallel 1D text arrays into a machine. Omitted IDs default to `1..N`; `NULL` priorities default to `0`. |
| `acdat.compile(patterns bytea[], pattern_ids bigint[], replacements bytea[], priorities integer[], options jsonb)` | `acdat.machine` | `NULL, NULL, NULL, '{}'`¹ | Binary array overload preserving arbitrary bytes (including `\x00`). |
| `acdat.compile(text, bigint)` | aggregate → `acdat.machine` | — | Compile rows of `(pattern, pattern_id)`. Returns `NULL` for zero rows. |
| `acdat.compile(text, bigint, text, integer)` | aggregate → `acdat.machine` | — | Compile rows of `(pattern, pattern_id, replacement, priority)`. Returns `NULL` for zero rows. |
| `acdat.compile(bytea, bigint)` | aggregate → `acdat.machine` | — | Binary aggregate over `(pattern, pattern_id)`. Returns `NULL` for zero rows. |
| `acdat.compile(bytea, bigint, bytea, integer)` | aggregate → `acdat.machine` | — | Binary aggregate over `(pattern, pattern_id, replacement, priority)`. Returns `NULL` for zero rows. |
{.fields meta="type default"}

¹ Defaults apply starting at `pattern_ids`; `patterns` is required. `options` accepts `NULL` or `{}`.

## Scan and replace {#scan}

| Name | Type | Default | Meaning |
| --- | --- | --- | --- |
| `acdat.contains(text, acdat.machine)` | `boolean` | — | Return `true` on the first literal match and stop scanning immediately. |
| `acdat.contains(bytea, acdat.machine)` | `boolean` | — | Binary overload of `contains`. |
| `acdat.matches(input text, machine acdat.machine, policy text, max_matches integer)` | `setof acdat.hit` | `'all_overlapping', 10000` | Return 1-based byte and character offsets for each hit. Candidate limit is checked before overlap resolution. |
| `acdat.matches(input bytea, machine acdat.machine, policy text, max_matches integer)` | `setof acdat.hit` | `'all_overlapping', 10000` | Binary overload; `start_char` and `char_length` are always `NULL`. |
| `acdat.replace(input text, machine acdat.machine, policy text, max_replacements integer, max_output_bytes bigint)` | `text` | `'leftmost_longest', 10000, 268435456` | Single non-recursive pass rewrite. Errors if a selected match has a `NULL` replacement or exceeds a limit. |
| `acdat.replace(input bytea, machine acdat.machine, policy text, max_replacements integer, max_output_bytes bigint)` | `bytea` | `'leftmost_longest', 10000, 268435456` | Binary overload of `replace`. |
{.fields meta="type default"}

## Overlap policies {#policies}

| Name | Applies to | Default | Resolution order |
| --- | --- | --- | --- |
| `all_overlapping` | `matches` | default for `matches` | Emit every candidate hit, including prefixes, suffixes, and duplicate literals with distinct IDs. |
| `leftmost_longest` | `matches` / `replace` | default for `replace` | Leftmost start offset → longer pattern → lower `priority` integer → lower `pattern_id` → lower canonical ordinal. |
| `leftmost_priority` | `matches` / `replace` | — | Leftmost start offset → lower `priority` integer → longer pattern → lower `pattern_id` → lower canonical ordinal. |
{.fields meta="type default"}

## Inspect and transport {#inspection}

| Name | Type | Default | Meaning |
| --- | --- | --- | --- |
| `acdat.info(acdat.machine)` | `jsonb` | — | Metadata JSON: engine, compiler/format versions, input kind, encoding, pattern/state/slot counts, and sizes. |
| `acdat.export(acdat.machine)` | `bytea` | — | Serialize a compiled machine into a portable binary image. |
| `acdat.validate(bytea)` | `boolean` | — | Verify a serialized image without raising an error on corruption. |
| `acdat.import(bytea)` | `acdat.machine` | — | Validate and decode a serialized image; raises an error if malformed. |
| `acdat.source_fingerprint(acdat.machine)` | `bytea` | — | 32-byte SHA-256 digest of the canonical rule set and input kind. |
| `acdat.build_fingerprint(acdat.machine)` | `bytea` | — | 32-byte digest identifying rules plus compiler, engine, and format version. |
| `acdat.artifact_checksum(acdat.machine)` | `bytea` | — | 32-byte checksum of the serialized artifact payload. |
{.fields meta="type default"}

## Managed dictionary catalog {#catalog}

| Name | Type | Default | Meaning |
| --- | --- | --- | --- |
| `acdat.publish(p_dictionary_name text, p_semantic_version bigint, p_compiled_machine acdat.machine, p_source_reference jsonb)` | `bytea` | `p_source_reference = NULL` | Store an immutable `ready` build and return its 32-byte dictionary-scoped `build_id`. |
| `acdat.activate(p_dictionary_name text, p_build_id bytea)` | `void` | — | Atomically point `acdat.active_dictionary` to a `ready` build. |
| `acdat.retire_build(p_dictionary_name text, p_build_id bytea)` | `void` | — | Mark an inactive build as `retired`. |
| `acdat.active_machine` | view | — | Active ready machines: `name`, `input_kind`, `version`, `build_id`, `machine`, `info`. |
| `acdat.dictionary` | table | — | Dictionary name and fixed `input_kind` (`text` or `bytea`). |
| `acdat.dictionary_version` | table | — | Semantic version, `source_fingerprint`, and optional `source_ref` JSON. |
| `acdat.dictionary_build` | table | — | Build metadata, compiler/format/engine info, status (`ready` / `retired`), and checksum. |
| `acdat.dictionary_build_data` | table | — | Compiled `acdat.machine` payload (`STORAGE EXTERNAL`) keyed by `build_id`. |
| `acdat.active_dictionary` | table | — | Current active `build_id` pointer per dictionary. |
{.fields meta="type default"}

## Types and internal functions {#types}

| Name | Type | Default | Meaning |
| --- | --- | --- | --- |
| `acdat.machine` | base type | — | Variable-length compiled automaton (`STORAGE = external`). |
| `acdat.hit` | composite type | — | `(pattern_id bigint, start_byte integer, byte_length integer, start_char integer, char_length integer, priority integer)`. |
| `machine_in` / `machine_out` / `machine_recv` / `machine_send` | internal I/O | — | Type I/O functions revoked from `PUBLIC`; invoked by PostgreSQL. |
| `_compile_*_trans` / `_compile_final` / `_catalog_build_id` | internal helpers | — | Aggregate state transition/final and catalog digest helpers revoked from `PUBLIC`. |
{.fields meta="type default"}
