Sub topic 11
WAL recycling, removal and what keeps old WAL alive
This chapter assumes basic SQL and psql, familiarity with transactions and ACID properties, and a basic understanding of PostgreSQL pages and configuration settings.
PostgreSQL writes WAL (write-ahead log) records into files under pg_wal. A WAL segment is a file containing a range of log sequence numbers (LSNs). After PostgreSQL has passed a segment and no feature needs it, PostgreSQL can recycle the file for future WAL or remove it.
Recycling is usually better than creating new files. It avoids repeated file creation and keeps the pg_wal directory manageable. A segment is not removed just because a checkpoint completed. PostgreSQL must first check all consumers of WAL.
The main retention rules
A checkpoint records a redo point. Crash recovery needs WAL from that point onward. After a newer checkpoint makes an older segment unnecessary, PostgreSQL can recycle or remove it, subject to other requirements.
The following features can keep old WAL alive:
| Consumer | What it needs | Common symptom |
|---|---|---|
| Replication slot | WAL from its `restart_lsn` | `pg_wal` grows continuously |
| Physical standby | WAL it has not received or replayed | Standby falls behind |
| `wal_keep_size` | A configured minimum for standby connections | Extra local WAL remains |
| Failed archiving | WAL that `archive_command` has not archived | Archive failures increase |
| Base backup or backup tooling | WAL needed to make the backup consistent | Retention lasts for the backup |
A replication slot is especially important. PostgreSQL protects WAL starting at the slot's restart_lsn, even when the consumer is disconnected. A forgotten or broken slot can eventually fill the disk.
SELECT slot_name,
slot_type,
active,
restart_lsn
FROM pg_replication_slots
ORDER BY slot_name;
SELECT pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)
) AS retained_for_slot,
slot_name
FROM pg_replication_slots
WHERE restart_lsn IS NOT NULL
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC;The second query estimates how much WAL lies between the current write position and each slot's restart position. It does not show the exact disk usage of every retained file, but it identifies slots that are far behind.
Archiving and standby retention
When archive_mode is enabled, PostgreSQL uses archive_command or an archive library to copy completed WAL segments elsewhere. PostgreSQL normally removes local segments after archiving succeeds and after recovery and replication no longer need them. If archiving fails, PostgreSQL keeps retrying and retains the files.
SELECT archived_count,
failed_count,
last_archived_wal,
last_failed_wal,
last_failed_time
FROM pg_stat_archiver;A physical standby can receive WAL through streaming replication. A replication slot provides exact retention for that standby. Without a slot, wal_keep_size provides only a lower bound. If the standby needs more WAL than the primary retains, it may require a new base backup.
Safe management
Do not delete files manually from pg_wal. PostgreSQL tracks checkpoints, timelines, archiving, and replication state together. Manual deletion can make crash recovery, replication, or point-in-time recovery fail.
Inspect settings before changing them:
SHOW wal_keep_size;
SHOW max_slot_wal_keep_size;
SHOW archive_mode;
SHOW archive_command;max_slot_wal_keep_size can limit how much WAL a replication slot may retain. A limit protects disk space, but an affected standby or logical consumer may need to be rebuilt or resynchronized after its required WAL disappears. Choose the limit from observed generation rates and the longest outage you need to tolerate.
Exercise
A disconnected replication slot has an old restartlsn, and pgwal keeps growing. What is the safest first action?
Show answer
Identify the slot's consumer and determine whether it is still needed. If it is abandoned, remove the slot through PostgreSQL's replication-slot commands after confirming that no consumer depends on it. Do not delete WAL files manually.