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 TABLEorALTER TABLEwon’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.