Sub topic 5
Replication slots and WAL retention
This chapter assumes you can run psql, edit postgresql.conf, and read a basic SELECT against system views. It also assumes you know that the write-ahead log (WAL) is the sequential record of every change the server makes. You should already have a publication on the publisher and know what a subscription is.
What a slot is
A replication slot is a named record on the publisher that remembers how far one consumer has read the WAL. Normally PostgreSQL recycles WAL segments once a checkpoint no longer needs them. A slot tells the server to keep every segment its consumer has not yet confirmed.
Logical replication needs this guarantee. If the subscriber is offline for an hour and the publisher recycles the WAL from that hour, the subscriber cannot catch up and must be rebuilt.
A logical slot is tied to one database and one output plugin. Logical replication uses the built-in plugin pgoutput. Each slot serves exactly one consumer. Two subscriptions never share a slot.
What a slot stores
Three fields in pg_replication_slots matter for logical slots:
| Column | Meaning |
|---|---|
| `restart_lsn` | The oldest WAL position the slot may still need. The server keeps all WAL from here onward. |
| `confirmed_flush_lsn` | The position up to which the subscriber has confirmed it received and flushed changes. Decoding resumes after this point. |
| `catalog_xmin` | The oldest transaction whose system catalog rows must be kept. Vacuum cannot remove catalog rows newer than this. |
restart_lsn can lag behind confirmed_flush_lsn. Decoding a transaction requires the WAL from where that transaction began. While a long transaction is open, restart_lsn stays near its start, even if the subscriber has confirmed later positions.
Slots are persistent. They survive a restart of the publisher and keep holding WAL until someone drops them.
How slots are created
CREATE SUBSCRIPTION creates a slot on the publisher by default. The slot has the same name as the subscription. During initial synchronization, each table copy worker also creates a temporary slot named like pg_<subid>_sync_<relid>_<sysid>. Those slots are removed when the copy finishes.
You can also create the slot yourself and tell the subscription to use it. This is useful when your tooling cannot give the subscriber a connection that is allowed to create slots, or when you need to control exactly when the snapshot is taken.
-- On the publisher: create the slot first
SELECT pg_create_logical_replication_slot('orders_sub', 'pgoutput');
-- On the subscriber: attach to the existing slot
CREATE SUBSCRIPTION orders_sub
CONNECTION 'host=publisher.example.com port=5432 dbname=shop user=repl_user password=change_me'
PUBLICATION orders_pub
WITH (create_slot = false, slot_name = 'orders_sub');The slot limit is max_replication_slots on the publisher. Each subscription uses one slot, and each concurrent table-sync worker temporarily uses another. Setting the limit too low makes CREATE SUBSCRIPTION or the initial copy fail with an error that no replication slots are available.
The slot life cycle
The diagram shows the exchange between the publisher and the subscriber. The key point is that WAL is released only after the subscriber confirms it.
If the subscriber stops confirming, the last step never happens. The publisher keeps writing new WAL and cannot free the old segments.
Why retained WAL becomes a disk problem
A slot that nobody consumes is one of the most common causes of a full WAL disk on a publisher. Typical causes:
- The subscriber is down, unreachable, or paused with
ALTER SUBSCRIPTION ... DISABLE. - The subscriber's apply worker keeps failing on an error, such as a unique-key conflict, and restarts without making progress.
- A subscription was dropped or the subscriber was destroyed, but the slot on the publisher was left behind.
- The subscriber is alive but too slow to keep up with the write rate.
In every case the publisher retains WAL from restart_lsn onward. If the WAL directory fills the disk, the publisher stops accepting writes and can shut down. Retention is not limited to the published tables. The slot holds the WAL for the whole cluster, because WAL is not split by table.
A stuck slot also holds back catalog_xmin. Over a long period this causes bloat in system catalogs.
Watching the slots
Run this on the publisher to see how much WAL each slot holds back:
-- On the publisher: how much WAL each slot is holding back
SELECT slot_name,
active,
restart_lsn,
confirmed_flush_lsn,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal
FROM pg_replication_slots;Read the output this way:
active = truemeans a walsender process is currently connected to the slot.falsemeans no consumer is attached.- A small and stable
retained_walis healthy. A value that grows steadily is a warning. - An inactive slot with a growing
retained_walis the case that fills disks.
To see the other side, query the subscriber:
-- On the subscriber: progress of each subscription worker
SELECT subname, pid, received_lsn, latest_end_lsn, latest_end_time
FROM pg_stat_subscription;If pid is null, no worker is running. If latest_end_time is old, the worker is not receiving anything recent.
Setting a safety limit
PostgreSQL 13 and later has max_slot_wal_keep_size. It caps how much WAL a slot may force the publisher to keep. The default is -1, which means no limit.
-- On the publisher: allow a slot to hold at most 50 GB of WAL
ALTER SYSTEM SET max_slot_wal_keep_size = '50GB';
SELECT pg_reload_conf();When a slot falls further behind than the limit, the publisher removes the WAL anyway and marks the slot as lost. The subscription cannot resume from that slot. You must drop and rebuild it, which means a new initial copy of the tables.
This trades a broken subscription for a healthy publisher. For most production systems the publisher is more important, so a limit is the safer choice. Set it comfortably below the free space on the WAL volume.
The wal_status column in pg_replication_slots shows how close a slot is to that point:
| `wal_status` | Meaning |
|---|---|
| `reserved` | The slot's WAL is within the normal retention window. |
| `extended` | The slot holds more WAL than normal limits would keep, but is still within `max_slot_wal_keep_size`. |
| `unreserved` | The slot is about to lose WAL. It may still recover if the consumer catches up soon. |
| `lost` | Required WAL has been removed. The slot cannot be used. |
The safe_wal_size column shows how many more bytes can be written before the slot is at risk.
Removing slots you no longer need
Dropping a subscription normally drops its remote slot too. If the publisher is unreachable at that moment, the drop fails. You then detach the slot from the subscription and clean it up by hand:
-- On the subscriber: detach the slot first, then drop the subscription
ALTER SUBSCRIPTION orders_sub DISABLE;
ALTER SUBSCRIPTION orders_sub SET (slot_name = NONE);
DROP SUBSCRIPTION orders_sub;
-- On the publisher: remove the slot manually if it is left over
SELECT pg_drop_replication_slot('orders_sub');Be aware of these edge cases:
pg_drop_replication_slotfails if the slot is active. Stop the consumer first, or find and terminate the walsender usingactive_pidinpg_replication_slots.- Never drop a slot that a live subscription still uses. The subscription will fail on its next connection attempt, and you cannot resume without a full resync.
- A slot created by hand with
create_slot = falseis not removed byDROP SUBSCRIPTION. You must drop it yourself. - Logical slots exist only on the server where they were created. A physical standby of the publisher does not have them in older versions. After a failover to that standby, subscribers cannot continue from their old position. PostgreSQL 17 added slot synchronization to standbys to address this, but it needs explicit configuration.
A short checklist for operations
- Alert when any slot has
active = falsefor more than a few minutes. - Alert on
retained_walgrowth, and on anywal_statusother thanreserved. - Set
max_slot_wal_keep_sizebelow the free space on the WAL volume. - Size
max_replication_slotsfor subscriptions plus a few table-sync workers. - When you retire a subscriber, drop its slot in the same change.
Exercise
On the publisher, pg_replication_slots shows a slot reporting_sub with active = false, and its retained_wal has grown from 2 GB to 40 GB over two days. The WAL disk has 60 GB free. Name two possible causes, and explain what you would change to protect the publisher if the subscriber cannot be fixed today.
Show answer
Possible causes: the subscriber is down or unreachable, the subscription was disabled, or the subscriber's apply worker keeps failing (for example on a conflict) and never confirms a position. A dropped subscriber whose slot was left behind also fits. To protect the publisher, either drop the slot with pgdropreplicationslot after confirming nobody needs it, or set maxslotwalkeep_size to a value well below the free space (for example 30GB) and reload the configuration. The second choice stops the disk from filling but will mark the slot lost once it exceeds the limit, so the subscription would need a full resync later.