PostgreSQL problems

pg_wal Fills the Disk: The Replication Slot Nobody Reads, and the Fix

22 November 2026

PostgreSQL removes old WAL at every checkpoint unless something still needs it. When pg_wal grows for days, one of three things needs it: a replication slot whose consumer is gone, an archiver that fails, or a setting that asks for it. A full WAL volume stops the whole instance, and the cause is one query away.

What PostgreSQL says

PANIC: could not write to file "pg_wal/xlogtemp.2181": No space left on device. The server stops, and it does not start again until the volume has room. Before that point there is no error at all: a slot that keeps WAL is silent, and only the size of pg_wal tells.

SELECT count(*)                  AS wal_files,
       pg_size_pretty(sum(size)) AS wal_size
FROM   pg_ls_waldir();

A steady instance keeps pg_wal near max_wal_size, 1 GB by default. Many times that is the sign.

Is it a replication slot

A slot promises its consumer that no WAL is removed before the consumer has read it. When the standby is switched off or the logical subscriber is deleted without dropping its slot, the promise has no end. Run this on the primary:

SELECT slot_name, slot_type, 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
ORDER  BY restart_lsn;
ColumnHow to read it
activef means nobody is connected to the slot. An inactive slot with gigabytes retained is the usual finding.
retainedThe WAL this slot keeps. The first row keeps the most.
wal_statusreserved is normal. extended: the slot keeps more than max_wal_size. unreserved: it will lose its WAL at the next checkpoint. lost: it already did, and the consumer must be rebuilt.
safe_leftHow much more WAL can be written before the slot is lost. Empty when max_slot_wal_keep_size is not set, which means there is no bound.

Is it the archiver

With archive_mode on, a WAL file stays in pg_wal until archive_command has copied it. A command that fails keeps every file:

SELECT archived_count, last_archived_wal,
       last_archived_time,
       failed_count, last_failed_wal, last_failed_time
FROM   pg_stat_archiver;

A last_failed_time newer than last_archived_time means the archiver is stuck on last_failed_wal. The reason is in the server log: a full backup target, an expired credential, a path that no longer exists.

Is it a setting

SELECT name, setting, unit
FROM   pg_settings
WHERE  name IN ('wal_keep_size', 'max_wal_size',
                'max_slot_wal_keep_size',
                'checkpoint_timeout');

wal_keep_size keeps that much WAL on purpose. A long checkpoint_timeout with a heavy load keeps more than max_wal_size between two checkpoints, which is a peak and not a leak.

The fix, in order

  1. Decide whether the consumer comes back. A standby in maintenance for an hour will read its WAL and the slot empties by itself. A standby that was removed last month will not.
  2. Drop the slot nobody reads. SELECT pg_drop_replication_slot('standby2'); The WAL goes at the next checkpoint, and CHECKPOINT; makes that now. An active slot must be disconnected first: active_pid is the process to end.
  3. Repair the archiver. Fix what archive_command fails on. The archiver tries again by itself and pg_wal shrinks as it catches up.
  4. Never delete files in pg_wal by hand. A file the server still needs for crash recovery looks like every other one. An instance that lost one does not start.
  5. If the volume is already full, give it room by growing it or by moving something else off it, start the server, and then remove the cause with the steps above.
  6. Bound it for the future. ALTER SYSTEM SET max_slot_wal_keep_size = '50GB'; and a reload. A slot that falls further behind than this is lost, and the standby must be rebuilt, which is a far smaller harm than a primary that is down.

See it coming

An abandoned slot is the slowest outage PostgreSQL has: nothing is wrong for days and then everything is. Radar Engine, the detection layer of Scops, measures what each slot keeps and how fast it grows, with no agent on the server:

DetectorWhen it fires
Slot holding WALA replication slot keeps WAL and nobody consumes it. The finding names the slot and the time left to its limit: max_slot_wal_keep_size, where the slot is lost, or the room of the WAL volume, where the disk fills.
WAL archiver failingArchiving fails and nothing is archived. Warning at once, Critical when it has lasted an hour.
Standby not streamingA known standby stopped streaming. Synchronous: Critical. Asynchronous: Warning after 5 minutes, Critical after 60.
Disk fillingA volume PostgreSQL writes to climbs toward 100%, with the time left and what grows on it.

Health Check lists every slot with what it retains, and Sizing shows the WAL directory next to the databases, so the growth is visible long before it is a finding.

Questions

Why does pg_wal keep growing?
Because something still needs the old WAL: a replication slot whose consumer is behind or gone, an archive_command that fails, or wal_keep_size. pg_replication_slots and pg_stat_archiver tell which.

Is it safe to drop a replication slot?
Dropping the slot is safe for the primary. The standby or subscriber that used it can no longer continue from the primary's WAL and has to be rebuilt or resynchronized, unless it can get the WAL from an archive.

Can I delete files from pg_wal?
No. Remove what holds the WAL and let a checkpoint delete the files. A WAL file removed by hand can make the instance unable to start.

What does max_slot_wal_keep_size do?
It limits how much WAL replication slots may keep. A slot that needs more is marked lost and its WAL is removed, so an abandoned slot can no longer fill the disk. The default, -1, is no limit.

Related problems

Sources