Working with Logical Decoding

1. Understanding Logical Decoding

ConceptDetail
ReadsWAL into logical row-level changes
Use CasesCDC, audit, downstream sync, ETL
SlotDurable cursor in WAL
Output PluginFormat (test_decoding, pgoutput, wal2json)

2. Configuring wal_level for Logical Decoding

# postgresql.conf
wal_level = logical
max_replication_slots = 10
max_wal_senders = 10

3. Creating Logical Replication Slot

SELECT * FROM pg_create_logical_replication_slot('cdc_slot', 'pgoutput');

4. Using Output Plugins

PluginUse
pgoutputBuilt-in; powers logical replication / Debezium
test_decodingHuman-readable, debugging only
wal2jsonJSON change stream

5. Consuming Changes

SELECT * FROM pg_logical_slot_get_changes('cdc_slot', NULL, NULL);
-- Advances slot; changes are gone after fetch

6. Peeking at Changes

SELECT * FROM pg_logical_slot_peek_changes('cdc_slot', NULL, NULL);
-- Does NOT advance slot (debugging)

7. Dropping Replication Slot

SELECT pg_drop_replication_slot('cdc_slot');
Warning: Inactive slots pin WAL forever; disk fills up. Always drop unused slots.

8. Monitoring Replication Slots

SELECT slot_name, slot_type, active, restart_lsn,
       pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained
  FROM pg_replication_slots;

9. Understanding Change Data Capture

ToolBuilt On
Debeziumpgoutput slot + Kafka Connect
AWS DMSlogical decoding (test_decoding/pglogical)
Native logical replicationpgoutput + subscriptions

10. Using Logical Decoding for Audit Trails

-- Consume slot in background worker → write each change to audit_log topic
-- Guarantees: every committed change observed exactly once per consumer.