Logical decoding for agent audits

Last checked · 2 min read

Logical decoding turns a database's write-ahead log into a stream of committed row changes. Used to audit an AI agent, it answers a question the agent can't: which rows did Postgres actually commit for this action? Triggers and cascades show up, because they changed rows in the same transaction.

What logical decoding is

The PostgreSQL documentation describes logical decoding as “the process of extracting all persistent changes to a database’s tables into a coherent, easy to understand format”.1 Postgres writes every change to its write-ahead log (WAL) before it commits. With wal_level = logical, which can only be set at server start, the WAL also carries what is needed to turn those changes back into rows.2 A replication slot keeps your place in that stream, and an output plugin formats it: pgoutput for logical replication, test_decoding for a quick look like the example below.

By default, a transaction reaches the reader only after it commits, with each changed row’s new values and, depending on the table’s REPLICA IDENTITY, its old ones. REPLICA IDENTITY FULL records the old values of every column, which is what you need to say what a row was before the agent touched it.3

Why it suits agent audits

An agent’s tool reports what its own statement touched. In the example, psql printed UPDATE 1, and the slot shows four rows changed in transaction 761: the one the statement named, and three a trigger shipped. Logical decoding doesn’t care how the SQL was spelled, which tool sent it, or what the agent said afterwards. It reports what committed.

That’s the difference from the other common audit sources. A statement log such as pgAudit records the statements sessions ran, with a WRITE class for INSERT, UPDATE, DELETE, TRUNCATE and COPY into a relation.4 It tells you the SQL; it doesn’t list the rows a trigger changed. An audit trigger on each table can record rows, but it runs inside the agent’s own transaction and can be disabled by a role that owns the table.

What it doesn’t see

  • Schema changes. Logical replication doesn’t carry DDL,7 so a DROP TABLE or ALTER TABLE won’t show up as row changes. Block those by policy instead.
  • Sequences aren’t replicated either,7 and nothing outside Postgres is in its WAL: an email the agent sent, a file it wrote, a call to another API.
  • Who wrote it. The stream says which transaction changed which rows, not which agent asked. Tying a transaction to an action takes a marker written in the same transaction, or a window where nothing else writes.
  • Reads. A query that only reads changes nothing, so it leaves nothing to decode.

An example

PostgreSQL 18.6, the test_decoding plugin: one UPDATE, and the trigger's rows in the same transactiontext
$ psql -c "UPDATE orders SET status = 'shipped' WHERE id = 2;"
UPDATE 1
$ psql -Atc "SELECT data FROM pg_logical_slot_get_changes('audit_demo', NULL, NULL);"
BEGIN 761
table public.orders: UPDATE: old-key: id[integer]:2 status[text]:'paid' new-tuple: id[integer]:2 status[text]:'shipped'
table public.orders: UPDATE: old-key: id[integer]:1 status[text]:'paid' new-tuple: id[integer]:1 status[text]:'shipped'
table public.orders: UPDATE: old-key: id[integer]:3 status[text]:'paid' new-tuple: id[integer]:3 status[text]:'shipped'
table public.orders: UPDATE: old-key: id[integer]:4 status[text]:'paid' new-tuple: id[integer]:4 status[text]:'shipped'
COMMIT 761

How Deedglass uses it

Deedglass reads the change log through the deedglass_witness role and a replication slot, matches each committed transaction to the action that caused it, and reports Matches or Doesn't match at the level its evidence supports.56 That is how a one-row UPDATE that a legacy trigger turned into 301 rows gets caught.

Related on this site

Sources & Reference Citations (7)

Citations
  1. 1PostgreSQL 18 documentation: Logical decoding concepts · postgresql.org · checked 18 October 2026
  2. 2PostgreSQL 18 documentation: Write ahead log settings (wal_level) · postgresql.org · checked 18 October 2026
  3. 3PostgreSQL 18 documentation: ALTER TABLE (REPLICA IDENTITY) · postgresql.org · checked 18 October 2026
  4. 4pgaudit/pgaudit, README · github.com · checked 13 October 2026
  5. 5Deedglass docs: Observe mode beside another gate · docs.deedglass.com · checked 14 October 2026
  6. 6Deedglass docs: Verification levels · docs.deedglass.com · checked 14 October 2026
  7. 7PostgreSQL 18 documentation: Logical replication restrictions · postgresql.org · checked 18 October 2026

Last checked 18 October 2026. We re-check the glossary every month. If something here is wrong, write to hello@deedglass.com.