Home / Databases / PostgreSQL

DatabasesChecklistAdvanced

PostgreSQL CDC with Debezium in production: a checklist for slots, publications, replica identity, heartbeats and failover

A pre-production checklist for Debezium on PostgreSQL. Covers slot and WAL limits, publication scope, REPLICA IDENTITY and TOAST, heartbeats for quiet databases, snapshots, and what happens to the slot on failover.

Deepak Bhardwaj7 min read

Applies toPostgreSQL 13 to 18 with pgoutput, Debezium 2.x and 3.x PostgreSQL connector, Amazon RDS/Aurora PostgreSQL, Azure Database for PostgreSQL Flexible Server

Why a checklist

A Debezium connector looks finished once events show up in Kafka. The failures come weeks later. A paused connector leaves a logical slot that holds WAL until the disk fills. A table without a primary key quietly emits updates with no "before" image. A failover loses the slot, and with it every change since the last snapshot. Go through these items before go-live and again after every major change.

1. Server prerequisites

  • wal_level = logical is set. A change needs a restart, or a parameter change on managed services (rds.logical_replication = 1 on RDS/Aurora; wal_level in the server parameters on Azure Flexible Server).
  • max_replication_slots and max_wal_senders have headroom for every connector, plus physical replicas, plus one spare for re-creation.
  • The connector user has REPLICATION (or rds_replication on RDS) and SELECT on the captured tables, and is not a superuser.
SHOW wal_level;
SELECT rolname, rolreplication FROM pg_roles WHERE rolname = 'debezium';

2. Slot safety: WAL can't grow without limit

  • max_slot_wal_keep_size is set (PG 13+). Without it, a stalled connector can fill pg_wal and stop the primary.
  • You alert on retained WAL per slot and on wal_status reaching unreserved or lost.
  • On PG 18, decide whether idle_replication_slot_timeout suits you. It invalidates slots that have been inactive too long.
ALTER SYSTEM SET max_slot_wal_keep_size = '200GB';   -- size to disk headroom
SELECT pg_reload_conf();

SELECT slot_name, plugin, active, wal_status,
       pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained,
       pg_size_pretty(safe_wal_size) AS safe_left
FROM pg_replication_slots WHERE slot_type = 'logical';

Caution

max_slot_wal_keep_size protects the primary by invalidating the slot. If the slot is lost, the connector has to re-snapshot. Size the limit so that a weekend outage of Kafka Connect fits inside it, and alert long before it's reached.

3. Publication scope

  • plugin.name = pgoutput is set. It ships with PostgreSQL and needs no extension.
  • The publication is created by the DBA, listing explicit tables, with publication.autocreate.mode = disabled. Alternatively, use filtered so Debezium limits it to table.include.list. Avoid all_tables on shared databases, because it needs elevated rights and decodes everything.
  • There's a process for adding tables: alter the publication and the connector's include list together.
CREATE PUBLICATION dbz_orders FOR TABLE app.orders, app.order_lines, app.customers;
ALTER PUBLICATION dbz_orders ADD TABLE app.payments;
SELECT * FROM pg_publication_tables WHERE pubname = 'dbz_orders';
plugin.name=pgoutput
slot.name=dbz_orders
publication.name=dbz_orders
publication.autocreate.mode=disabled
table.include.list=app.orders,app.order_lines,app.customers,app.payments

4. Replica identity and TOAST

  • Every captured table has a primary key, or a deliberate REPLICA IDENTITY.
  • You know which tables need full "before" images. REPLICA IDENTITY FULL gives them, at the cost of more WAL and slower updates and deletes.
  • Consumers handle unchanged TOASTed columns. When a large text/jsonb value didn't change and the identity isn't FULL, Debezium emits a placeholder (unavailable.value.placeholder, default __debezium_unavailable_value) in place of the value.
-- Tables in the publication without a primary key
SELECT pt.schemaname, pt.tablename, c.relreplident
FROM pg_publication_tables pt
JOIN pg_class c ON c.relname = pt.tablename
JOIN pg_namespace n ON n.oid = c.relnamespace AND n.nspname = pt.schemaname
WHERE pt.pubname = 'dbz_orders'
  AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE i.indrelid = c.oid AND i.indisprimary);
-- relreplident: d = default (PK), f = full, i = index, n = nothing

ALTER TABLE app.audit_log REPLICA IDENTITY FULL;

Tables without a primary key and with REPLICA IDENTITY DEFAULT can't publish updates or deletes. PostgreSQL rejects those statements on the source once the table is in a publication that replicates updates. That's an outage, not a CDC gap, so check before you add the table.

5. Heartbeats for low-traffic databases

  • heartbeat.interval.ms is set, so the connector confirms progress even when the captured tables are quiet.
  • If the captured database is quiet but others on the server are busy, heartbeat.action.query writes to a small heartbeat table that is in the publication. Otherwise the slot's confirmed_flush_lsn never moves and WAL builds up.
CREATE TABLE IF NOT EXISTS app.dbz_heartbeat (id int PRIMARY KEY, ts timestamptz);
ALTER PUBLICATION dbz_orders ADD TABLE app.dbz_heartbeat;
heartbeat.interval.ms=10000
heartbeat.action.query=INSERT INTO app.dbz_heartbeat (id, ts) VALUES (1, now()) ON CONFLICT (id) DO UPDATE SET ts = excluded.ts

6. Snapshots

  • snapshot.mode is chosen on purpose. initial snapshots once, then streams. no_data streams only (named never in older releases, check your version). when_needed re-snapshots if offsets are no longer valid.
  • You know how long the initial snapshot of the largest table takes, and a long-running snapshot transaction is acceptable on the primary. It holds back vacuum's xmin horizon while it runs.
  • Incremental (signal-based) snapshots are set up, with a signal table, so you can backfill new or repaired tables without restarting from scratch.

7. Failover and upgrades

  • You know what happens to the slot on failover. Before PG 17, logical slots don't exist on physical standbys, so after a promotion the connector has no slot and must re-snapshot (or you accept a gap).
  • On PG 17+, failover slots are set up. Create the slot with failover = true (recent Debezium versions expose a slot.failover option; check your version's documentation). On the standby, set sync_replication_slots = on, hot_standby_feedback = on, a dbname in primary_conninfo, and list the standby's physical slot in synchronized_standby_slots on the primary.
  • The managed-service behaviour is documented for your platform. Some services preserve logical slots across HA failover, others don't, and the details change between releases.
  • Major-version upgrades have a plan. pg_upgrade before PG 17 doesn't carry logical slots over, so plan a controlled stop, upgrade, re-create and re-snapshot (or a no-data restart with a verified LSN).
-- PG 17+: confirm the slot is synced on the standby
SELECT slot_name, failover, synced, inactive_since FROM pg_replication_slots;

8. Monitoring

Signal Source Alert when
Retained WAL per slot pg_replication_slots Above 50% of max_slot_wal_keep_size
Slot inactive active = false Longer than the connector restart time
Decoding spill to disk pg_stat_replication_slots.spill_bytes (PG 14+) Rising steadily. Consider raising logical_decoding_work_mem
Connector lag Debezium MilliSecondsBehindSource JMX metric Above your freshness SLO
Connector task state Kafka Connect REST /connectors/<name>/status Any FAILED task
curl -s http://connect:8083/connectors/pg-orders/status | jq '.tasks[].state'

9. Decommissioning

  • Deleting a connector doesn't drop its slot. Drop the slot explicitly, after the connector is gone.
SELECT pg_drop_replication_slot('dbz_orders');
DROP PUBLICATION dbz_orders;

Warning

An orphaned logical slot is the most common cause of pg_wal filling up on CDC sources. Make "drop slot" a mandatory step in your connector removal runbook.

Prevent it next time

  • Keep the publication, replica identity changes and connector configuration in the same repository, and review them together.
  • Rehearse a failover with the connector running in staging at least once per major version.
  • Track retained WAL per slot on the same dashboard as disk free space.