#!/usr/bin/env bash # A replication slot makes Postgres keep WAL until the consumer confirms it. Three ways that goes wrong, and the fixes. set -eu . "$(dirname "$0")/lib.sh" OUT=output/06-wal-retention.txt mkdir -p output slots() { sqlp -c "select slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) as wal_kept_for_slot, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) as unconfirmed from pg_replication_slots order by slot_name" } burst() { sql -c "insert into noise(payload) select repeat('x', 200) from generate_series(1, 100000)"; } { echo "### Safety valve: the default cap on WAL a slot may pin (-1 = unlimited)" sqlp -c "show max_slot_wal_keep_size" echo echo "### Baseline: both connectors running, nothing unusual written" slots echo echo "### Failure 1: healthy connectors, but all the writes go to a table nobody captures (100,000 rows, about 33 MB of WAL)" burst sleep 15 slots echo echo "### Failure 2: delete the outbox connector. Kafka Connect does not drop its slot." curl -s -X DELETE $CONNECT/connectors/outbox-connector -o /dev/null sleep 5 burst slots echo echo "### Fix for 2: drop the orphaned slot by hand" sql -c "select pg_drop_replication_slot('connect_outbox')" >/dev/null slots echo echo "### Fix for 1: heartbeat.interval.ms plus a heartbeat.action.query that writes to a captured table (public.heartbeat)" curl -s -X PUT -H 'Content-Type: application/json' $CONNECT/connectors/orders-connector/config \ --data "$(jq '.config + {"table.include.list":"public.orders,public.heartbeat","heartbeat.interval.ms":"5000","heartbeat.action.query":"INSERT INTO heartbeat (id, ts) VALUES (1, now()) ON CONFLICT (id) DO UPDATE SET ts = EXCLUDED.ts"}' connect/orders-connector.json)" -o /dev/null sleep 60 slots } 2>&1 | tee "$OUT"