A replication slot tells the PostgreSQL primary to keep write-ahead log (WAL) around until the consumer on the other end — a streaming replica or a logical decoding client — has confirmed it read up to a given point. That is what guarantees a replica never asks for a WAL segment the primary already recycled. The catch is the flip side: a slot that stops advancing, because its replica is down or its logical consumer stalled, pins WAL forever. The primary honours the slot, refuses to recycle those segments, and pg_wal grows until the disk fills and the whole cluster stops. The fix is not to abandon slots but to watch them: read pg_replication_slots, measure how much WAL each slot retains, cap the damage with max_slot_wal_keep_size, and drop slots the moment their consumer is gone for good. This guide covers physical and logical slots on PostgreSQL 14 through 17.
What you’ll accomplish
- Create physical slots for streaming replicas and logical slots for decoding clients, using the built-in functions.
- Read
pg_replication_slotsand interpretactive,wal_status, andsafe_wal_size. - Compute exactly how much WAL each slot is pinning with
pg_wal_lsn_diff. - Cap a slot’s WAL retention with
max_slot_wal_keep_sizeso a dead slot can’t take the primary down. - Identify an inactive slot safely and drop it with
pg_drop_replication_slot.
Prerequisites
- A self-managed PostgreSQL 14, 15, 16, or 17 primary where you can run SQL as a superuser or a role with
REPLICATION. On managed Postgres you usually see the slots but not the underlyingpg_waldirectory. wal_levelset toreplica(enough for physical slots) orlogical(required for logical slots).replicais the default; changing tologicalneeds a restart.- Shell access to the data directory helps for the disk-side checks, but every diagnostic here runs from SQL.
- One consumer to test against: a streaming standby with
primary_slot_nameset, or a logical decoding plugin such aspgoutputortest_decoding.
How it works
Every change in PostgreSQL is written to WAL before it touches a data file. Once a 16 MB WAL segment is filled and safely flushed (and archived, if archiving is on), the primary is normally free to recycle it. A replication slot changes that bargain. Each slot stores a restart_lsn — the oldest log position its consumer still needs. The primary promises never to recycle any segment at or after the lowest restart_lsn across all slots. As the consumer confirms progress, restart_lsn moves forward and the freed segments are recycled.
A physical slot feeds a streaming replica. The standby names it through primary_slot_name, and its restart_lsn tracks how far the standby has replayed. A logical slot feeds a decoding client (change-data-capture, pgoutput-based logical replication, an ETL pipeline). Logical slots also hold back the system catalog xmin, so an abandoned logical slot blocks vacuum as well as pinning WAL — a second, quieter failure mode.
The danger is entirely in the gap between restart_lsn and the current WAL position. A healthy, active slot keeps that gap small. A slot whose consumer disappeared freezes its restart_lsn while the primary keeps generating WAL, so the gap — and pg_wal — grows without bound.
Step-by-step
1. Create a physical slot for a streaming replica. Make the slot on the primary first, then point the standby at it. Creating the slot does not start retaining much WAL until a consumer connects and sets restart_lsn.
SELECT * FROM pg_create_physical_replication_slot('standby_1');
Expected output — the slot exists but has no position yet:
slot_name | lsn
-----------+-----
standby_1 |
(1 row)
On the standby, set primary_slot_name = 'standby_1' in postgresql.conf and restart. From then on the standby consumes through the slot and its restart_lsn starts advancing.
2. Create a logical slot for a decoding consumer. Logical slots need wal_level = logical and take an output plugin. pgoutput ships with PostgreSQL and backs native logical replication; test_decoding is handy for inspection.
SELECT * FROM pg_create_logical_replication_slot('cdc_1', 'pgoutput');
Expected output — the slot is created at the current position:
slot_name | lsn
-----------+-----------
cdc_1 | 0/1A2B3C0
(1 row)
A logical slot starts retaining WAL immediately, because it captures the position from which decoding must begin. It only advances once a consumer connects and confirms flush.
3. List every slot and its status in one query. This is the single view you bookmark. Run it on the primary.
SELECT slot_name, slot_type, active, restart_lsn, wal_status, safe_wal_size
FROM pg_replication_slots;
Expected output — one healthy slot, one abandoned:
slot_name | slot_type | active | restart_lsn | wal_status | safe_wal_size
-----------+-----------+--------+-------------+------------+---------------
standby_1 | physical | t | 0/9C0000A8 | reserved | 1069547520
cdc_1 | logical | f | 0/1A2B3C0 | extended | 161480704
(2 rows)
active = f on cdc_1 means no consumer is connected right now. That alone is not an emergency — a replica can reconnect — but an active = f slot whose restart_lsn never moves is the classic WAL-bloat culprit.
4. Read wal_status correctly. This column is your early-warning system. Its four values, in order of escalating trouble:
reserved -- WAL is within max_wal_size; normal
extended -- WAL exceeds max_wal_size but is still kept for the slot
unreserved -- WAL is at risk; past max_slot_wal_keep_size, next checkpoint may remove it
lost -- required WAL is already gone; the slot can no longer be used
reserved and extended are safe. unreserved means you are one checkpoint away from the slot breaking. lost means it already broke — the consumer can no longer resume from this slot and must be rebuilt.
5. Measure exactly how much WAL each slot is pinning. safe_wal_size tells you the headroom before lost; this tells you the debt already accrued. pg_wal_lsn_diff returns the byte distance between the current WAL position and each slot’s restart_lsn.
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;
Expected output — the abandoned slot is retaining far more:
slot_name | active | wal_status | retained_wal
-----------+--------+------------+--------------
cdc_1 | f | extended | 14 GB
standby_1 | t | reserved | 48 MB
(2 rows)
standby_1 holds 48 MB — one active replica a little behind, fine. cdc_1 is holding 14 GB and climbing because nothing is consuming it. That number, sorted descending, tells you which slot to deal with first.
6. Cap the blast radius with max_slot_wal_keep_size. Available since PostgreSQL 13, this GUC (in postgresql.conf) puts a ceiling on how much WAL any single slot may pin. Beyond it, the slot is marked lost at the next checkpoint and its WAL is freed — the primary survives at the cost of that one replica.
max_slot_wal_keep_size = 50GB
Reload, not restart:
SELECT pg_reload_conf();
The tradeoff is real and worth stating plainly. The default (-1) means unlimited retention: a dead slot can fill the disk and stop the primary. A finite value protects the primary but will invalidate a slot that lags past the cap, forcing that replica to be re-seeded. Set it to a value larger than your worst legitimate replication lag, but small enough that it can never consume the pg_wal volume.
7. Identify an inactive slot before dropping it. Never drop a slot on active = f alone — a replica may simply be restarting. Confirm the slot is both inactive and not advancing, and that you know which consumer it belonged to.
SELECT slot_name, active, active_pid,
restart_lsn,
pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)
) AS retained_wal
FROM pg_replication_slots
WHERE active = false;
Expected output — active_pid empty confirms no backend is attached:
slot_name | active | active_pid | restart_lsn | retained_wal
-----------+--------+------------+-------------+--------------
cdc_1 | f | | 0/1A2B3C0 | 14 GB
(1 row)
Run it twice a minute apart: if restart_lsn is unchanged and active_pid stays empty, no one is using the slot. Trace the name back to a decommissioned replica or a retired CDC job before you act.
8. Drop the orphaned slot. Once you have confirmed it is dead, drop it. This immediately releases the retained WAL; the next checkpoint recycles the freed segments.
SELECT pg_drop_replication_slot('cdc_1');
Expected output:
pg_drop_replication_slot
--------------------------
(1 row)
You cannot drop a slot that is currently active — PostgreSQL raises an error. Stop the consumer first, or let it disconnect.
Validate it works
After capping and cleanup, re-run the retention query and confirm the numbers are back under control:
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;
Expected output — only the healthy slot remains, retaining little:
slot_name | active | wal_status | retained_wal
-----------+--------+------------+--------------
standby_1 | t | reserved | 51 MB
(1 row)
Then confirm the disk actually recovered. pg_wal should shrink toward max_wal_size (plus any archiving backlog) within a checkpoint or two:
SELECT pg_size_pretty(sum(size)) AS pg_wal_bytes
FROM pg_ls_waldir();
Expected output — down from tens of gigabytes to steady state:
pg_wal_bytes
--------------
1088 MB
(1 row)
If pg_wal does not shrink after a slot is dropped or capped, force a checkpoint with CHECKPOINT; and check again — recycling happens at checkpoint time, not instantly.
Troubleshooting
pg_wal grows without bound and the disk is filling. Find the slot pinning the most WAL with the query in step 5, sorted descending. A slot with active = f and a frozen restart_lsn is the usual cause. Decide per slot: if the replica is coming back, fix its connection so restart_lsn advances; if it is gone, drop the slot. Set max_slot_wal_keep_size so this can never again reach a disk-full condition.
“requested WAL segment … has already been removed” on the replica. The primary recycled WAL the standby still needed — either the slot was dropped, or wal_status reached lost because retention passed max_slot_wal_keep_size. The slot can’t rescue the replica now; check pg_replication_slots for wal_status = 'lost'. The fix is to re-seed the standby from a fresh pg_basebackup (with -C --slot=standby_1 to recreate the slot) and let it catch up.
An inactive slot from a decommissioned replica. After you retire a standby or a read replica, its slot lives on with active = f, silently pinning WAL. Nothing removes a slot automatically. Make “drop the slot on the primary” part of every decommissioning runbook, and audit periodically with SELECT slot_name, active FROM pg_replication_slots WHERE active = false;.
A logical slot stuck because no consumer is reading. A logical slot with no connected client never advances restart_lsn and also holds back catalog_xmin, so it blocks vacuum and bloats the catalog on top of pinning WAL. Confirm with SELECT slot_name, active, catalog_xmin FROM pg_replication_slots WHERE slot_type = 'logical';. Either restart the decoding consumer so it confirms flush, or drop the slot if the pipeline is retired. Logical slots are covered in depth in PostgreSQL logical replication.
safe_wal_size is small or negative. A negative safe_wal_size means the slot has already passed its safe margin and is heading for lost at the next checkpoint. Treat it as an alert threshold: page when safe_wal_size drops below, say, one segment’s worth. It gives you lead time that wal_status = 'lost' does not.
When this is the wrong approach
- You are on managed Postgres. RDS, Cloud SQL, and most PaaS offerings manage slots for their own replicas and may hide
pg_wal. You can still readpg_replication_slotsto spot a stuck CDC slot, butmax_slot_wal_keep_sizeand manual drops may be restricted to their console. - You only need a short-lived copy. For a one-off logical dump or a temporary decoding session, use a temporary slot (the functions accept a
temporaryflag) so it disappears automatically when the session ends — no orphan to clean up later. - The real goal is backups, not replicas. If you are keeping WAL to enable point-in-time recovery rather than to feed a live consumer, a replication slot is the wrong tool — it has no size ceiling by design. Archive WAL to object storage instead; see continuous WAL archiving with WAL-G.
For the authoritative reference, see the PostgreSQL manual on replication slots and the pg_replication_slots view.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.