Databases

PostgreSQL logical replication slot grows and WAL accumulates after logical decoding plugin crashes on high-write primary

Environment

Environment

PostgreSQL 13.5 on Ubuntu 20.04 primary, logical replication slot used for an external decoder plugin (pgoutput/custom plugin) streaming to a downstream consumer over a networked decoder process; high write rate (large UPDATE batches).

Symptoms

Symptoms

Replication slot's restart_lsn stops advancing and pg_replication_slots reports 'active' false. wal directory accumulates many WAL files and disk usage grows until alarms. Downstream consumer shows disconnections and plugin process logs show decoder plugin segfaults or crashed worker process.

Observations

Observations

Postgres logs show 'logical decoding found consistent point' then later 'terminating walreceiver due to unexpected end of WAL' and plugin-related crashes. pg_stat_activity shows no active replication connection. Attempts to restart decoder process do not clear existing slot; vacuum/pg_switch_wal doesn't remove WAL files due to slot being retained.

Already attempted

Already attempted

Restarted decoder process and the consumer application; server side left slot intact. Created a new logical slot and pointed new consumer to it; it works but old slot still holds WAL. Attempting to drop slot with pg_drop_replication_slot fails while slot is active=false and no streaming connection present.

Question

Question

How can I safely recover from a crashed logical decoding plugin that left a replication slot holding WAL indefinitely, reclaim disk space without data loss, and prevent recurrence under heavy write load?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
agnesdigital Answer
user
Posted 1mo ago • 0 confirmations

If the decoder can be restarted and attached to the same slot, that’s the safest path: bring the consumer back, resume streaming and let the slot advance — WAL files will then be removed. If the consumer is unrecoverable and disk is critical, after confirming you have an up-to-date base backup and accept losing that downstream state, drop the slot with SELECT pg_drop_replication_slot('slotname') to free WAL. Add monitoring for slot lag and supervise the decoding process so future crashes auto-restart and resume the slot. Verify by watching pg_replication_slots and disk WAL usage.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

External logical decoding plugin or consumer process crashed and did not advance or close the replication slot, causing WAL retention 95%
Plugin hit a corner-case bug under high write rates causing fatal error and leaving slot orphaned 60%
Network blips causing frequent disconnects that left slot unadvanced and intermittent reconnect attempts failing to resume position 30%

Summary

Logical slots retain WAL until a consumer confirms consumption; a crashed decoding plugin left the slot in place, preventing WAL purge. Safe recovery options: reconnect a consumer to the slot and advance it (best), or if consumer cannot be recovered, drop the slot (data lost for that slot) after ensuring downstream state is acceptable. Prevent recurrence by making decoder process resilient (supervised service), monitor slot lag, and consider using replication slots with max_slot_wal_keep (PG 13 lacks built-in limit) or offload logical decoding to a durable process that can resume.

Suggested Fixes

Diagnostic step 1
Check pg_replication_slots and pg_stat_replication to confirm slot state and last_confirmed_flush_lsn.
Diagnostic step 2
If you can recover the decoder/consumer, start it and point it at the existing slot to stream and advance the slot; monitor WAL purge after consumer advances.
Diagnostic step 3
If consumer cannot be recovered and disk space is urgent, ensure you have a recent base backup; then drop the slot with SELECT pg_drop_replication_slot('slotname') to free WAL (note: downstream data for that slot will be lost).

Continue troubleshooting

Matched from the title, description and tags