Quick revision
The short answer
max_wal_size is only a soft target for when checkpoints happen, not a limit: PostgreSQL keeps any WAL that something still needs. So we check what is holding it: replication slots, especially an inactive logical slot left by a stopped CDC connector such as Debezium, which is the most common cause; a failing archive_command, visible in pg_stat_archiver; and wal_keep_size or checkpoints that cannot complete. We recover by fixing or dropping the stale slot after confirming the consumer can resnapshot, or by fixing archiving, never by deleting files in pg_wal by hand, and we prevent a repeat with max_slot_wal_keep_size, alerts and an owner for every slot.
What to remember
In this answer, we saw that max_wal_size is a soft checkpoint target, and that WAL grows far beyond it only when something still needs it. We check pg_replication_slots for inactive slots retaining WAL, the most common cause being a stopped Debezium connector, pg_stat_archiver for failing archiving, then wal_keep_size and checkpoint health. We recover by adding space, then dropping the stale slot after confirming the consumer can resnapshot or fixing archiving, never deleting pg_wal files by hand, and we prevent a repeat with max_slot_wal_keep_size, alerts and an owner for every slot.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
max_wal_size is only a soft target for when checkpoints happen, not a limit: PostgreSQL keeps any WAL that something still needs. So we check what is holding it: replication slots, especially an inactive logical slot left by a stopped CDC connector such as Debezium, which is the most common cause; a failing archive_command, visible in pg_stat_archiver; and wal_keep_size or checkpoints that cannot complete. We recover by fixing or dropping the stale slot after confirming the consumer can resnapshot, or by fixing archiving, never by deleting files in pg_wal by hand, and we prevent a repeat with max_slot_wal_keep_size, alerts and an owner for every slot.
Why this question is asked
A primary that runs out of disk stops accepting writes, so this is a full outage for every service using the database. Under that pressure, the tempting move is to delete old files from pg_wal, which can destroy the database. Interviewers ask this to see whether we know why WAL is retained, how to find the holder quickly with the right queries, and how to recover without making things worse. In this answer, we will recap how WAL is recycled, check slots, archiving and other causes, recover safely, and put prevention in place.
1A Quick Recap: What max_wal_size Really Means
PostgreSQL writes every change to the WAL, the write-ahead log, in 16 MB segment files in the pg_wal directory. After a checkpoint, segments older than the checkpoint's redo point are no longer needed for crash recovery and can be removed or recycled. max_wal_size, here 4 GB, tells PostgreSQL to trigger a checkpoint when about that much WAL has accumulated since the last one. It is a soft target that controls checkpoint frequency.
So WAL beyond max_wal_size is retained when something other than crash recovery still needs it. The question is never 'why is PostgreSQL ignoring the limit' but 'who is holding the WAL'. There are a few usual suspects, and each has a query.
2Check Replication Slots First
A replication slot is a promise PostgreSQL makes to a consumer, a standby or a logical decoding client such as Debezium: it will keep all WAL from the slot's restart_lsn onwards until the consumer confirms it has processed it. If the consumer stops, the slot keeps the promise forever, and WAL piles up.
Notice the pattern that explains most of these incidents: a logical slot that is not active and retains almost all of the WAL. A CDC connector, the Debezium connector streaming changes to Kafka, was stopped or deleted two weeks ago, and nobody dropped its slot. A physical slot of a standby that is down or far behind produces the same symptom, so we check that the active standbys show small retained sizes.
3Check Archiving and Other Causes
If the slots are fine, the next suspect is WAL archiving. With archive_mode on, PostgreSQL keeps each segment until archive_command has succeeded for it. If the archive destination is unreachable, full, or the command returns an error, every segment stays.
- Slot with large retained size?YesFix or drop the slotNoCheck archiving
- pg_stat_archiver failures rising?YesFix archive_command or destinationNoCheck wal_keep_size
- wal_keep_size very large?YesReduce itNoCheck checkpoint logs
- Checkpoints slow or failing?YesFix I/O, let checkpoint finishNoEscalate with full diagnosis
4 steps in order. Play them, or step through them one at a time.
Each branch has a query; none of them involves deleting files.
A failed_count that keeps rising with a recent last_failed_time means archiving is broken, and the server log shows the command's error. Two smaller causes remain. A large wal_keep_size deliberately keeps that much WAL for standbys without slots. And checkpoints that cannot finish, because the disk is very slow or a checkpoint is extremely long, delay removal; the log lines from log_checkpoints show their timing.
4Recovering Safely
The first priority is to stop the outage without risking data. If we can add disk space to the volume, that buys time immediately and is the safest first step. Then we remove the cause.
For a stale CDC slot, we confirm with the owning team that the connector is gone or can take a fresh snapshot, because dropping the slot discards its position. Then we drop it, and PostgreSQL frees the WAL at the next checkpoint, which we can trigger.
For broken archiving, we fix the destination or the command; PostgreSQL then archives the backlog and removes segments. Only if archiving cannot be fixed quickly would we consider temporarily setting archive_command to a no-op, knowing it breaks point-in-time recovery until a new base backup is taken.
What we never do is delete files in pg_wal by hand. PostgreSQL may need any of them for crash recovery, and removing the wrong one can leave a database that cannot start or has silently lost data. The tools above free exactly the WAL that is safe to free.
5Preventing It Next Time
Since PostgreSQL 13, max_slot_wal_keep_size caps how much WAL a slot may retain. Past the cap, the slot is invalidated instead of filling the disk: the consumer breaks and must resnapshot, but the primary keeps running, which is almost always the right priority. We set it to a size the disk can comfortably hold, for example 100 GB on a volume with room for more.
Then we add alerts: on retained WAL per slot, on inactive slots, on pg_stat_archiver failures and on pg_wal and disk usage, well before they become critical. And every slot gets an owner, recorded when it is created, so that decommissioning a connector includes dropping its slot. Most incidents of this kind are not technical failures but forgotten slots.
6Follow-up questions
What happens to the CDC consumer after you drop its slot?
It loses its place. The changes between its last confirmed position and now are no longer available, so when the connector restarts it must create a new slot and take a fresh snapshot of the tables, or the downstream systems will miss those changes. With Debezium, the snapshot mode setting controls this, so check your version's options. Downstream consumers must handle the snapshot, which usually means idempotent upserts.
Why not just raise max_wal_size?
Because max_wal_size is not what is retaining the WAL. Raising it changes checkpoint frequency and does nothing about a slot or a failing archive, which would keep growing past any value. It is the wrong lever for this problem.
Can a standby's slot cause the same problem?
Yes. A physical slot for a standby that is down keeps WAL just like a logical one. If the standby will be rebuilt anyway, dropping its slot is safe; if it should catch up, fixing the standby or its network is the better path, as long as disk space allows.
7Conclusion
In this answer, we saw that max_wal_size is a soft checkpoint target, and that WAL grows far beyond it only when something still needs it. We check pg_replication_slots for inactive slots retaining WAL, the most common cause being a stopped Debezium connector, pg_stat_archiver for failing archiving, then wal_keep_size and checkpoint health. We recover by adding space, then dropping the stale slot after confirming the consumer can resnapshot or fixing archiving, never deleting pg_wal files by hand, and we prevent a repeat with max_slot_wal_keep_size, alerts and an owner for every slot.
Open with
First, max_wal_size is not a limit, it is a soft target for how often checkpoints run. WAL is retained beyond it whenever something still needs it, so the question is who holds it.
Then, if they want more
I check replication slots first: select slot name, type, active and the difference between pg_current_wal_lsn and restart_lsn from pg_replication_slots. The classic finding is an inactive logical slot from a Debezium connector that was stopped weeks ago, retaining almost all the WAL. A down standby's physical slot does the same.
If slots are fine, I check pg_stat_archiver for a rising failed_count and last_failed_wal, which means archive_command is failing and every segment is kept, and the server log tells me why. Then wal_keep_size and the checkpoint log lines. To recover, I add disk if I can, to end the outage safely.
Then I fix the cause: for a stale slot, I confirm with its owners that the consumer is gone or can resnapshot, drop it with pg_drop_replication_slot and run a checkpoint; for archiving, I fix the destination or command so the backlog drains. I never delete files from pg_wal by hand, because that can corrupt the database.
To prevent it, I set max_slot_wal_keep_size so a stuck slot gets invalidated instead of filling the disk, alert on slot lag, inactive slots, archiver failures and disk usage, and make sure every slot has an owner.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 13, PostgreSQL 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A PostgreSQL 17 primary runs out of disk. The pg_wal directory has grown to 400 GB while...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/7385c33556/