Sub topic 8
Replication Slots: Keeping WAL for Standbys
What a replication slot does
This chapter assumes you understand PostgreSQL primary and standby servers, WAL streaming, and basic SQL administration.
A replication slot is a named record on the primary that tracks the oldest WAL position a standby still needs. WAL, or write-ahead log, contains the changes PostgreSQL records before applying them to database files. A physical replication slot is used by a physical standby that replays those changes.
Without a slot, the primary may remove old WAL after normal retention rules decide it is no longer needed. A standby that disconnects for too long might then request WAL that no longer exists. The standby must be rebuilt from a new base backup.
With a slot, the primary retains WAL from the slot's restart_lsn, which is the oldest WAL position still required by that slot.
Creating and inspecting a physical slot
A slot can be created directly on the primary:
SELECT * FROM pg_create_physical_replication_slot('standby1');A base backup can also create a slot when you use pg_basebackup -C -S standby1. The standby must use the same name in primary_slot_name. The slot name connects the standby's WAL receiver to the primary's retention tracking.
Inspect slots on the primary with this query:
SELECT slot_name,
slot_type,
active,
restart_lsn,
wal_status
FROM pg_replication_slots;| Column | Meaning |
|---|---|
| `slot_name` | Name used by the standby or replication client |
| `active` | Whether a client is currently connected through the slot |
| `restart_lsn` | Oldest WAL position the slot still requires |
| `wal_status` | Whether the required WAL remains available or has exceeded a limit |
An inactive slot is not automatically safe. It may still force the primary to retain WAL indefinitely. A failed or permanently removed standby can therefore fill the primary's disk even though no client is using the slot.
Managing the disk-growth risk
The primary stores retained WAL in its pg_wal directory. Monitor filesystem space as well as slot state. Look for inactive slots and compare their restart_lsn with the current WAL position.
By default, max_slot_wal_keep_size is -1, which means slots can retain WAL without a size limit. Set a finite value to establish a safety boundary:
# postgresql.conf on the primary
max_slot_wal_keep_size = '10GB'This limit protects disk space, but it can make a lagging standby unable to continue. If the standby needs WAL that the primary discarded, create a fresh base backup and configure the standby again.
Remove a slot only after confirming that no standby or backup process needs it:
SELECT pg_drop_replication_slot('standby1');The command fails while the slot is active. Stopping the client first prevents accidental interruption of a working standby.
Exercise
A standby has been offline for two days, and its physical slot is inactive. What should you check before deleting the slot?
Show answer
Confirm whether the standby will return and whether it still needs the retained WAL. Check the slot's restart_lsn, retained WAL size, filesystem space, and standby logs. Delete the slot only if the standby is permanently removed or will be rebuilt.