# Getting started with acdat

> Install acdat and build a complete comment moderation and redaction pipeline in PostgreSQL in three minutes.

---

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

---

## 1. Install acdat {#install}

### Install from Pigsty packages

CI covers PostgreSQL 14–18, and Pigsty ships packages for PostgreSQL 15–19 across Linux distributions. Check [Downloads](/download/#acdat) for OS and architecture availability.

```bash
pig repo add pgsql -u
pig ext add acdat
```

### Or build from the release tag

Point `PG_CONFIG` to your target PostgreSQL installation:

```bash
git clone --branch v0.1.1 --depth 1 https://github.com/pgsty/acdat.git
cd acdat
make PG_CONFIG=pg_config
sudo make install PG_CONFIG=pg_config
```

## 2. Enable the extension {#enable}

Connect to a UTF8 database as a superuser and create the extension. All functions and types live in the fixed `acdat` schema:

```sql
CREATE EXTENSION IF NOT EXISTS acdat;
```

## 3. Build and run a moderation pipeline {#first-scan}

Instead of scanning rows one keyword at a time, keep rules in a table, compile them into one `acdat.machine`, and run filtering, annotation, and redaction in a single pass.

1. Create a rule table and sample comments:

   ```sql
   CREATE TEMP TABLE moderation_rules (
       pattern_id  bigint PRIMARY KEY,
       pattern     text NOT NULL,
       replacement text NOT NULL,
       priority    integer NOT NULL DEFAULT 0,
       category    text NOT NULL
   );

   INSERT INTO moderation_rules (pattern_id, pattern, replacement, priority, category) VALUES
       (101, 'insider tip',       '[BLOCKED]', 10, 'compliance'),
       (102, 'guaranteed return', '[BLOCKED]', 10, 'compliance'),
       (103, 'DM on Telegram',    '[CONTACT]',  5, 'spam');

   CREATE TEMP TABLE comments (
       comment_id bigint PRIMARY KEY,
       body       text NOT NULL
   );

   INSERT INTO comments VALUES
       (1, 'DM on Telegram for a guaranteed return today'),
       (2, 'PostgreSQL 18 release notes'),
       (3, 'Never act on an insider tip');
   ```

2. Compile the rule table into a single `acdat.machine`:

   ```sql
   CREATE TEMP TABLE compiled_dict AS
   SELECT acdat.compile(pattern, pattern_id, replacement, priority) AS machine
   FROM moderation_rules;
   ```

3. Filter only the comments that match at least one rule (`acdat.contains` stops on the first hit):

   ```sql
   SELECT comment_id, body
   FROM comments
   WHERE acdat.contains(body, (SELECT machine FROM compiled_dict))
   ORDER BY comment_id;
   ```

   ```text
    comment_id |                     body
   ------------+----------------------------------------------
             1 | DM on Telegram for a guaranteed return today
             3 | Never act on an insider tip
   ```

4. Extract every match with its position and join back to your rule table for business labels:

   ```sql
   SELECT c.comment_id,
          h.pattern_id,
          r.category,
          h.start_char,
          h.char_length,
          substring(c.body FROM h.start_char FOR h.char_length) AS matched_text
   FROM comments AS c
   CROSS JOIN LATERAL acdat.matches(c.body, (SELECT machine FROM compiled_dict)) AS h
   JOIN moderation_rules AS r USING (pattern_id)
   ORDER BY c.comment_id, h.start_byte, h.pattern_id;
   ```

   ```text
    comment_id | pattern_id |  category  | start_char | char_length |   matched_text
   ------------+------------+------------+------------+-------------+-------------------
             1 |        103 | spam       |          1 |          14 | DM on Telegram
             1 |        102 | compliance |         22 |          17 | guaranteed return
             3 |        101 | compliance |         17 |          11 | insider tip
   ```

5. Redact all matched spans in one non-recursive pass:

   ```sql
   SELECT comment_id,
          acdat.replace(body, (SELECT machine FROM compiled_dict)) AS sanitized
   FROM comments
   ORDER BY comment_id;
   ```

   ```text
    comment_id |            sanitized
   ------------+----------------------------------
             1 | [CONTACT] for a [BLOCKED] today
             2 | PostgreSQL 18 release notes
             3 | Never act on an [BLOCKED]
   ```
{.steps}

> [!TIP]
> Notice the scalar subquery `(SELECT machine FROM compiled_dict)` above. Writing the machine lookup as an uncorrelated subquery lets PostgreSQL load the automaton once in an `InitPlan` rather than re-reading it per row during parallel scans.

For one-off ad-hoc checks without a rule table, you can also compile an inline array directly:

```sql
SELECT acdat.contains('PostgreSQL timeout error', acdat.compile(ARRAY['timeout', 'deadlock'])) AS matched;
-- t
```

## Next steps {#next}

- [Examples and workflows](/docs/acdat/examples/) — Explore overlap policies, log IOC extraction, managed dictionary publishing & rollback, binary matching, and cross-database export/import.
- [Performance and tuning](/docs/acdat/performance/) — Benchmark charts, avoiding the 22x parallel-plan trap, and per-worker memory budgets.
- [Operations](/docs/acdat/operations/) — Separate dictionary admin privileges, backups, and error troubleshooting.
- Run the tagged end-to-end SQL script [examples/demo.sql](https://github.com/pgsty/acdat/blob/v0.1.1/examples/demo.sql) inside a single rolled-back `psql` transaction.

---

Backlinks:

- [Package downloads](/download/)
