A PostgreSQL primary filled its disk after the data-warehouse change consumer stopped; how do you confirm a replication slot caused it, recover safely, and prevent a repeat?
answer
- free space, never touch pg_wal
- inactive slot, old restart_lsn
- bytes behind current WAL position
- drop, then checkpoint
- cap by size, cap by idle time
basics
~20 sFree or add disk space (never delete pg_wal files), start the primary, find the inactive slot whose restart_lsn trails far behind in pg_replication_slots, drop it, then checkpoint. Prevent repeats with max_slot_wal_keep_size, idle_replication_slot_timeout and retained-WAL alerts.
solid answer
~40 sWhen the file system holding `pg_wal` fills, PostgreSQL does a PANIC shutdown; committed data is safe, but you must first free or add space, never by deleting files from `pg_wal`. Once it is up, query `pg_replication_slots`: the culprit is typically a logical slot with `active = false`, an old `inactive_since`, `wal_status = 'extended'`, and a huge `pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)`; rule out a failing archiver before blaming it. If the warehouse consumer cannot catch up soon, drop the slot with `pg_drop_replication_slot`; if it says the slot is active for a PID, stop that consumer or `pg_terminate_backend` the walsender first. The next checkpoint, or a manual `CHECKPOINT`, then removes or recycles the WAL. The consumer has lost its position and must re-snapshot. Prevent repeats with `max_slot_wal_keep_size`, `idle_replication_slot_timeout` (18), and alerts on retained bytes and `inactive_since`.
code
sql · 9 linesSELECT slot_name, slot_type, active, active_pid, inactive_since, wal_status,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal,
age(catalog_xmin) AS catalog_xmin_age
FROM pg_replication_slots;
SELECT pg_size_pretty(sum(size)) AS pg_wal_size FROM pg_ls_waldir();
SELECT pg_drop_replication_slot('dw_cdc');
CHECKPOINT;go deeper
Recall that an abandoned replication slot keeps WAL forever by default and that you never delete pg_wal files by hand.
Explain how to read pg_replication_slots for the culprit: active, inactive_since, wal_status, and retained bytes from restart_lsn.
Run the incident end to end: safe restart, confirm the cause against other WAL retainers, drop versus wait, checkpoint, guardrails and alerting.
Set policy for who owns each slot and which side loses when they conflict: a capped slot sacrifices the consumer's continuity to protect the primary.
## What happened A **logical replication slot** created for the data-warehouse change consumer tells the primary to keep every WAL record from the slot's `restart_lsn` onward, and system-catalog rows newer than its `catalog_xmin`, until the consumer confirms them. When the consumer stopped, the slot stopped advancing but kept pinning. `max_slot_wal_keep_size` defaults to `-1`, so nothing limited retention, and `pg_wal` grew with every write. When the file system that holds `pg_wal` is full, PostgreSQL does a **PANIC shutdown**. No committed transactions are lost, but the database stays offline until space is freed. ## Step 1: get the server running without damaging it 1. **Do not delete files from `pg_wal`.** Segments after a checkpoint's redo point are needed for crash recovery; removing them by hand can leave the cluster unable to start. 2. **Do not run `pg_resetwal`** to make room: it discards WAL and can corrupt data. It is a last resort for a cluster that cannot otherwise start, not a space tool. 3. **Free or add space**: grow the volume, or remove non-PostgreSQL files from the same file system (old logs, dumps). 4. Start the server and let crash recovery finish. ## Step 2: confirm the slot is the cause Query `pg_replication_slots` on the primary and look for: - `active = false` and an `inactive_since` timestamp (17+) from around when the consumer stopped; - `wal_status = 'extended'`: the slot is keeping WAL beyond `max_wal_size`; - retained bytes, `pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)`, close to the total of `pg_ls_waldir()`; - a large `age(catalog_xmin)`, the catalog-bloat side of the same stall. Other things also keep WAL: a failing `archive_command` (check it through `pg_stat_archiver`, the PITR leaf's view) and a large `wal_keep_size`. If retained bytes for the slot roughly equal the size of `pg_wal`, the slot is the cause. ## Step 3: decide, then release The question is whether the consumer will resume soon enough to read everything the slot kept. | Situation | Action | Consequence | |---|---|---| | Consumer can resume now and space is safe | restart it and watch `restart_lsn` move | no resync; WAL drains as it catches up | | Consumer is gone or hours away | `pg_drop_replication_slot('dw_cdc')` | consumer must create a new slot and re-snapshot | | Unsure, disk nearly full again | drop it; the primary outranks the warehouse | same as above | - Dropping needs a superuser or a role with `REPLICATION`. - If the call fails with `replication slot "dw_cdc" is active for PID 4711`, a consumer is attached: stop it, or call `pg_terminate_backend(4711)` and drop before it reconnects. - Dropping releases the WAL and the `catalog_xmin`, but files go only at the **next checkpoint**; run `CHECKPOINT` (superuser or `pg_checkpoint`) to free space now. - Do not "fast-forward" the slot with `pg_replication_slot_advance` to save the consumer: every change it skips is lost to the warehouse just as surely, while the slot pretends nothing happened. How the warehouse pipeline re-snapshots is the change-data-capture leaf's subject. ## Step 4: prevent the next one - **`max_slot_wal_keep_size`** caps WAL any slot may retain (in MB without a unit; reload). A slot beyond it is invalidated at a checkpoint with `invalidation_reason = 'wal_removed'`; the consumer must resync, but the primary stays up. - **`idle_replication_slot_timeout`** (new in 18, seconds without a unit, default `0` = off) invalidates slots inactive longer than the timeout at the next checkpoint, `invalidation_reason = 'idle_timeout'`. It also catches a slot that pins `catalog_xmin` on a database writing little WAL. - **Alerts** on retained bytes, `safe_wal_size` when a cap is set, `inactive_since`, and free space on the `pg_wal` file system. - **Ownership**: every slot has a named owner, and decommissioning a consumer includes dropping its slot. ## Why it builds up unnoticed Nothing about a stalled slot looks wrong from the application's side. Writes succeed, queries run at normal speed, and the primary stays healthy until the last few gigabytes go. The slot's growth is linear with write volume, so a quiet weekend can hide it and a Monday batch load can finish it. That is why the alert belongs on the slot itself (retained bytes, `inactive_since`) and on `pg_wal` free space, not on the consumer's own health checks, which may be the very thing that stopped. A dashboard that shows only `pg_stat_replication` also misses it: that view lists connected walsenders, so a slot whose consumer is gone has no row there at all.
- The consumer is attached again and slowly catching up, but the disk is at 90%. What are your options short of dropping the slot?Add disk space to buy time, and watch `pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)` fall as it catches up. Setting `max_slot_wal_keep_size` just below the remaining headroom and reloading turns a future outage into a slot invalidation instead. Reducing write volume (pausing batch jobs) slows growth. If the trend still points at a full disk, drop the slot: the warehouse can resync, while a PANIC takes the primary down.
- After you dropped the slot, pg_wal is still large. Why?Dropping releases the slot's claim, but files are removed or recycled only at the next checkpoint, so run `CHECKPOINT` or wait for one. Recycled segments also stay up to `min_wal_size`. If space still does not return, something else retains WAL: another slot, a large `wal_keep_size`, or an archiver that is failing, so files are kept until they are archived.
- Why is pg_replication_slot_advance a bad way to save the warehouse feed here?Advancing moves the slot's confirmed position forward, so everything between the old and new position is never sent to the consumer. It frees the WAL, but the warehouse silently misses those changes, which is worse than a clean drop and a known resync, because a drop at least forces the consumer to rebuild.
- Delete the oldest files in pg_wal to bring the server back up.
- Run pg_resetwal to clear out the WAL the slot is holding.
- A PANIC shutdown on a full pg_wal loses recently committed transactions.
- Raising max_wal_size stops a slot from filling the disk.
- Dropping the slot frees the disk space immediately, without any checkpoint.
- Advancing the slot to the current LSN keeps the warehouse consistent.