Sub topic 5
Replication Slots and WAL Retention
This chapter assumes you know basic SQL, can run psql, and understand that PostgreSQL writes changes to its write-ahead log (WAL). A replication slot is a server-side record that preserves the WAL or logical changes needed by a replication client.
Why replication slots matter
A logical replication slot connects a change stream to a specific subscriber. The publisher does not discard changes that the slot still needs. When the subscriber receives and confirms changes, the slot advances.
This protects a slow or temporarily disconnected subscriber. It also creates a risk: an inactive slot can make the publisher retain a large amount of WAL and eventually fill its disk.
A slot has two important positions:
restart_lsnis the oldest WAL position that the slot might still need.confirmed_flush_lsnrecords how far a logical consumer has confirmed processing the stream.
The LSN is a log sequence number, which identifies a position in WAL. A growing distance between the current WAL position and restart_lsn indicates retained WAL.
Inspecting slots on the publisher
Query pg_replication_slots on the publisher:
SELECT slot_name,
slot_type,
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;The active column shows whether a client currently uses the slot. An inactive slot can still retain WAL. The retained_wal calculation estimates the distance from the current WAL position to the slot's restart position.
For a subscription, also inspect progress on the subscriber:
SELECT subname,
pid,
received_lsn,
latest_end_lsn,
latest_end_time
FROM pg_stat_subscription;Compare the latest times and LSNs with the publisher's current activity. A subscriber that has stopped receiving changes, combined with an inactive slot, needs attention.
Limiting the disk-space risk
By default, a slot can retain WAL indefinitely. PostgreSQL provides max_slot_wal_keep_size to limit how much WAL replication slots may retain at a checkpoint.
-- Check the configured limit
SHOW max_slot_wal_keep_size;
-- Set a 10 GB limit in the server configuration
ALTER SYSTEM SET max_slot_wal_keep_size = '10GB';The setting requires a reload or restart according to the parameter's context. Check the actual value after changing configuration:
SELECT name, setting, unit, context
FROM pg_settings
WHERE name = 'max_slot_wal_keep_size';A limit reduces the chance that one abandoned slot fills the disk. However, if a consumer falls too far behind and the required WAL is removed, that consumer may no longer be able to continue. Recreate or resynchronize the subscription instead of assuming the missing changes can be recovered from the slot.
| Situation | Likely action |
|---|---|
| Slot is inactive and no longer needed | Drop the subscription or slot after confirming ownership |
| Subscriber is temporarily slow | Restore it and monitor retained WAL |
| Required WAL was already removed | Reinitialize or resynchronize the subscriber |
Cleaning up an unused slot
Dropping a subscription normally removes its remote slot. If the publisher is unreachable during cleanup, detach the slot before dropping the subscription:
-- On the subscriber
ALTER SUBSCRIPTION orders_sub DISABLE;
ALTER SUBSCRIPTION orders_sub SET (slot_name = NONE);
DROP SUBSCRIPTION orders_sub;
-- On the publisher, only after confirming the slot is unused
SELECT pg_drop_replication_slot('orders_sub');Never drop a slot that an active subscriber still needs. First check active, identify the owning subscription, and confirm that stopping replication is intended.
Exercise
Why can an inactive logical replication slot cause a publisher's disk to fill?
Show answer
The slot keeps required WAL from being removed until a consumer confirms that it has processed the changes. If the consumer never reconnects, retained WAL can grow without bound.