Skip to content

Getting started with acdat

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

1. Install acdat

Install from Pigsty packages

CI covers PostgreSQL 14–18, and Pigsty ships packages for PostgreSQL 15–19 across Linux distributions. Check Downloads for OS and architecture availability.

pig repo add pgsql -u
pig ext add acdat

Or build from the release tag

Point PG_CONFIG to your target PostgreSQL installation:

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

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

CREATE EXTENSION IF NOT EXISTS acdat;

3. Build and run a moderation pipeline

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:

    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:

    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):

    SELECT comment_id, body
    FROM comments
    WHERE acdat.contains(body, (SELECT machine FROM compiled_dict))
    ORDER BY comment_id;
     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:

    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;
     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:

    SELECT comment_id,
           acdat.replace(body, (SELECT machine FROM compiled_dict)) AS sanitized
    FROM comments
    ORDER BY comment_id;
     comment_id |            sanitized
    ------------+----------------------------------
              1 | [CONTACT] for a [BLOCKED] today
              2 | PostgreSQL 18 release notes
              3 | Never act on an [BLOCKED]
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:

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

Next steps

  • Examples and workflows — Explore overlap policies, log IOC extraction, managed dictionary publishing & rollback, binary matching, and cross-database export/import.
  • Performance and tuning — Benchmark charts, avoiding the 22x parallel-plan trap, and per-worker memory budgets.
  • Operations — Separate dictionary admin privileges, backups, and error troubleshooting.
  • Run the tagged end-to-end SQL script examples/demo.sql inside a single rolled-back psql transaction.