Sub topic 8
Replication slots: keeping WAL for standbys and the risk of disk growth
This chapter assumes you have a working primary and a hot standby built with pg_basebackup, and that you can read pg_stat_replication. You should know what a WAL segment and an LSN are. Here you will learn what a replication slot does, how to create and inspect one, and how an unused slot can fill the primary's disk.
The problem slots solve
A primary recycles WAL segments after a checkpoint, once nothing local needs them. A standby that falls behind can ask for a segment the primary has already removed. The standby then fails with an error like requested WAL segment 000000010000000000000042 has already been removed, and you must rebuild it from a new base backup.
Without slots, you have two ways to reduce that risk:
- Set
wal_keep_sizeso the primary keeps a fixed amount of extra WAL. This is a guess. Too small and a slow standby breaks. Too large and you waste disk. - Archive WAL and give the standby a
restore_command. This works, but it adds an archive to run.
A replication slot is a persistent record on the primary that tracks how much WAL a particular consumer still needs. The primary will not remove WAL at or after the slot's restart_lsn, no matter how long the consumer is disconnected.
Physical slots
Streaming replication uses physical slots. A physical slot tracks a byte position in the WAL. (Logical slots, used by logical replication, track decoded changes and are covered later in the tutorial.)
There are two ways to create a physical slot. The first is the one you saw with pg_basebackup -C -S standby1. The second is a SQL call on the primary:
SELECT pg_create_physical_replication_slot('standby1');The standby then names the slot in its configuration:
# postgresql.conf (or postgresql.auto.conf) on the standby
primary_conninfo = 'host=primary.example.com user=replicator password=change-me application_name=standby1'
primary_slot_name = 'standby1'
hot_standby = onWhen the standby connects, the walsender attaches to the slot. As the standby confirms that it has flushed WAL to disk, the primary advances the slot's restart_lsn. Segments older than that position can then be recycled.
A new slot created with pg_create_physical_replication_slot has no restart_lsn until a consumer connects, unless you pass true as the second argument (immediately_reserve). If no WAL is reserved, the primary may recycle segments before the standby first connects. pg_basebackup -C avoids this because it reserves WAL as part of the backup.
Inspecting slots
Run this on the primary:
SELECT slot_name, slot_type, active, restart_lsn, wal_status
FROM pg_replication_slots;| Column | Meaning |
|---|---|
| `slot_name` | The name the standby uses in `primary_slot_name`. |
| `slot_type` | `physical` or `logical`. |
| `active` | `true` if a walsender is currently attached to the slot. |
| `restart_lsn` | The oldest WAL position the primary must keep for this slot. |
| `wal_status` | How safe the retained WAL is: `reserved`, `extended`, `unreserved` or `lost`. |
The wal_status values matter in practice:
reserved: the needed WAL is withinmax_wal_size, the normal range.extended: the slot holds more thanmax_wal_size. The WAL is kept only because of the slot (orwal_keep_size).unreserved: the slot is about to lose WAL. This only happens whenmax_slot_wal_keep_sizeis set. The next checkpoint may remove the data.lost: required WAL has been removed and the standby cannot resume. You must rebuild it.
The risk: an inactive slot fills the disk
By default a slot has no upper limit. If a standby is shut down, broken or decommissioned, and its slot still exists, restart_lsn stops moving. The primary keeps every WAL segment from that point forward. On a busy server this can be many gigabytes per hour, and the data lives in pg_wal.
If the volume holding pg_wal fills up, the primary cannot write WAL. It then stops accepting writes and may shut down with a PANIC. This is one of the most common causes of a primary outage on replicated systems.
A slot that is inactive is the warning sign. Find slots and how much WAL they hold back:
SELECT slot_name,
active,
wal_status,
pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)
) AS retained_wal
FROM pg_replication_slots
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC;If active is false and retained_wal keeps growing between runs, the slot is holding WAL for nobody. Alert on this. A simple monitoring rule is to page when any slot is inactive for more than a set time, or when retained WAL passes a percentage of the pg_wal volume.
Limiting retention with max_slot_wal_keep_size
PostgreSQL 13 added max_slot_wal_keep_size. It sets the most WAL, in addition to what max_wal_size covers, that slots may force the primary to keep. The default, -1, means unlimited.
# postgresql.conf on the primary
max_slot_wal_keep_size = '50GB'When a slot falls further behind than this limit, the primary removes the WAL anyway and marks the slot's wal_status as lost. The standby can no longer stream from it and must be rebuilt (or, if it also has a restore_command and an archive, it can catch up from the archive).
This is a trade-off between two failures:
| Setting | If a standby stays away | Cost |
|---|---|---|
| Unlimited (`-1`) | Primary disk fills and the primary may go down. | You protect the standby at the primary's expense. |
| Capped | Slot becomes `lost`, standby needs a rebuild. | You protect the primary at the standby's expense. |
For most production systems the primary matters more than any one standby, so choose a cap. Size it from the free space on the pg_wal volume, leaving a safety margin, and from how long you want to tolerate a standby outage (WAL generation rate times that duration).
Dropping a slot
You cannot drop a slot that is active. Stop the standby first, then run this on the primary:
SELECT pg_drop_replication_slot('standby1');Once the slot is gone, the primary recycles the held WAL at the next checkpoints. Disk space is returned gradually, not at the moment of the drop.
Slots and other settings
Slots interact with settings you already know:
max_replication_slotslimits the number of slots. Set it at least as high as the number of standbys plus any logical consumers and spare room forpg_basebackup -C. Changing it requires a restart.wal_keep_sizestill applies. The primary keeps the larger of what the slot requires and whatwal_keep_sizerequires. With a slot you can often lowerwal_keep_size.- A slot does not make replication synchronous. It only controls WAL retention. Commit durability is set by
synchronous_standby_namesandsynchronous_commit. - Slots are not copied to the standby by default. After a failover, the new primary has no slot for the other standbys unless you created one in advance, so plan slot creation as part of your failover procedure.
Slots versus archiving
A slot and WAL archiving protect against different failures. A slot keeps WAL on the primary's own disk for connected-or-recently-connected consumers. An archive keeps WAL in separate storage, indefinitely, and also supports point-in-time recovery. A common design uses both: a slot for normal streaming, plus an archive with restore_command as a fallback if a standby falls too far behind and the slot is capped.
Practical checklist
- Give every standby its own slot, named after its
application_name. - Set
max_slot_wal_keep_sizeto a value yourpg_walvolume can hold. - Alert on inactive slots and on retained WAL per slot.
- Drop the slot when you remove a standby.
- Recreate slots on a newly promoted primary for the remaining standbys.
Exercise
A standby was shut down for maintenance last week and never restarted. The primary's pg_wal directory is growing and pg_replication_slots shows the standby's slot with active = false. Name two actions you can take, and say which one you must choose if the standby is being retired permanently.
Show answer
You can restart the standby so it reconnects and lets the slot's restart_lsn advance, or you can drop the slot with pg_drop_replication_slot('standby1'), which lets the primary recycle the held WAL. If the standby is being retired permanently, drop the slot. Restarting a standby you do not intend to keep only delays the problem. To stop this from recurring, also set max_slot_wal_keep_size so an abandoned slot cannot fill the disk.
Exercise
You set max_slot_wal_keep_size = '20GB'. A standby is offline long enough that its slot falls 30 GB behind. What happens to the slot, and what must you do to bring the standby back?
Show answer
The primary removes WAL beyond the limit and the slot's wal_status becomes lost. The standby can no longer stream the missing WAL. You must rebuild it from a new base backup, unless it has a restore_command pointing at an archive that still holds the missing segments, in which case it can catch up from the archive.