Sub topic 12
Monitoring Replication and Cleaning Up Slots
A logical replication link can look healthy while it quietly fills the publisher's disk. This chapter covers what to watch on each side, how to read the signals, and how to remove a slot safely. It assumes you can create a publication and subscription and know that a replication slot makes the publisher keep WAL until the subscriber has consumed it. It also assumes you can run queries as a superuser or a role with pg_monitor.
Why monitoring matters
The publisher keeps WAL files as long as any slot still needs them. If the subscriber stops, falls behind, or is dropped without removing its slot, WAL piles up in pg_wal until the disk is full. A full WAL disk stops the publisher from accepting writes.
Two kinds of problems need watching:
- Retention problems on the publisher. A slot holds back WAL.
- Progress problems on the subscriber. The apply worker is not running, is erroring, or is slow.
Monitoring on the publisher
The main view is pg_replication_slots. This query shows how much WAL each slot is holding 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;The columns mean:
activeis true while a walsender process is connected to the slot. A false value on a slot you expect to be in use means the subscriber is not connected.restart_lsnis the oldest WAL position the slot may still need. The publisher cannot remove WAL from this point onward. Theretained_walcolumn in the query measures the distance from here to the current WAL position.confirmed_flush_lsnis the position up to which the subscriber has confirmed it received and flushed changes. The distance from here to the current position is the amount of data the subscriber has not yet confirmed.
On PostgreSQL 13 and later, two more columns help:
-- On the publisher (PostgreSQL 13 or later)
SELECT slot_name,
slot_type,
active,
wal_status,
safe_wal_size,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) AS unconfirmed
FROM pg_replication_slots;wal_status takes one of these values:
| wal_status | Meaning |
|---|---|
| reserved | The slot's WAL is within the normal retention limits. |
| extended | The slot's WAL exceeds `max_wal_size` but is still kept. |
| unreserved | The slot is at risk. The next checkpoint may remove WAL it needs. |
| lost | Required WAL has been removed. The slot can no longer be used. |
safe_wal_size is the number of bytes that can still be written before the slot is at risk of losing WAL. It is only meaningful when max_slot_wal_keep_size is set. By default that setting is -1, which means unlimited retention, so a stuck slot can fill the disk. Setting it to a finite value trades disk safety for the risk of invalidating a slot.
For time-based lag, use pg_stat_replication. Each subscription with a running apply worker appears as a walsender, and its application_name is the subscription name:
-- On the publisher
SELECT application_name, state, sent_lsn, flush_lsn,
write_lag, flush_lag, replay_lag
FROM pg_stat_replication;If a subscription is missing from this view, its worker is not connected. If it is present and the lag values keep growing, the subscriber is connected but cannot keep up.
Monitoring on the subscriber
pg_stat_subscription has one row per subscription worker. It lists the apply worker and any table synchronization workers that are running:
-- On the subscriber: progress of each subscription worker
SELECT subname, pid, received_lsn, latest_end_lsn, latest_end_time
FROM pg_stat_subscription;Read it like this:
- A
pidof NULL means no worker is running. The subscription may be disabled, or the worker may keep crashing and restarting. - If
received_lsnandlatest_end_timeadvance between two runs of the query, data is flowing. - If
latest_end_timeis old on a busy publisher, the link is stalled.
On PostgreSQL 15 and later, pg_stat_subscription_stats counts errors:
-- On the subscriber (PostgreSQL 15 or later)
SELECT subname, apply_error_count, sync_error_count
FROM pg_stat_subscription_stats;A rising apply_error_count almost always means a conflict, such as a unique violation on a row that already exists. The error text is in the subscriber's server log, with the failing transaction's LSN. See the chapter on apply errors for fixes.
During initial synchronization, check each table's state in pg_subscription_rel:
-- On the subscriber
SELECT srrelid::regclass AS table_name, srsubstate
FROM pg_subscription_rel;The srsubstate codes are i (initializing), d (data being copied), f (copy finished), s (synchronized) and r (ready, streaming normally). A table stuck at d for a long time usually means the copy is large or is failing and retrying.
Common failure patterns
| Symptom | Likely cause | Action |
|---|---|---|
| Slot `active = false`, `retained_wal` growing | Subscriber down, subscription disabled, or network failure | Restore the subscriber, or drop the slot if the subscription is abandoned |
| Slot active, lag growing | Subscriber too slow, or a long transaction on the publisher | Check subscriber load, missing indexes on subscriber tables, and long-running publisher transactions |
| `apply_error_count` rising, worker restarting | Conflict or constraint violation on the subscriber | Read the subscriber log and fix the data or skip the transaction |
| `wal_status = lost` | `max_slot_wal_keep_size` exceeded while the subscriber was away | Recreate the subscription. The slot cannot be recovered |
| Subscriber table has no matching columns or table | DDL was not replicated | Create or alter the subscriber table, then refresh the subscription |
A missing index on the subscriber is a frequent cause of slow apply. The apply worker locates each updated or deleted row using the replica identity. Without an index on those columns, it scans the whole table for every changed row.
A long-running transaction on the publisher delays logical decoding for that transaction until it commits (before PostgreSQL 14, always; later versions can stream in-progress transactions if the subscription enables streaming). The subscriber will look idle and then receive a large burst.
Cleaning up slots
Normally you do not manage slots by hand. DROP SUBSCRIPTION connects to the publisher and drops the slot for you. This breaks down when the publisher is unreachable, or when the subscription was created with create_slot = false and the slot belongs elsewhere. In those cases, the drop fails with an error about being unable to drop the replication slot.
The fix is to detach the slot from the subscription, drop the subscription, and then remove the slot on the publisher yourself:
-- 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');The subscription must be disabled before you can set slot_name to NONE. After that, DROP SUBSCRIPTION does not contact the publisher. The remote slot is then your responsibility.
pg_drop_replication_slot fails if the slot is in use. If a walsender is still attached, find and stop it first:
-- On the publisher: find the process using the slot
SELECT slot_name, active_pid
FROM pg_replication_slots
WHERE slot_name = 'orders_sub';
-- Stop it, then drop the slot
SELECT pg_terminate_backend(active_pid)
FROM pg_replication_slots
WHERE slot_name = 'orders_sub' AND active;To find slots that nobody is using, list inactive logical slots with their retained WAL:
-- On the publisher: candidates for cleanup
SELECT slot_name,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal
FROM pg_replication_slots
WHERE slot_type = 'logical' AND NOT active
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC;Confirm that each listed slot is really abandoned before you drop it. Slots may also belong to other consumers, such as a change-data-capture tool, which will lose its position if you remove its slot. Table synchronization during initial copy uses temporary slots. These are removed automatically when the sync worker finishes or the connection ends, so you do not need to clean them up.
Exercise
On the publisher, pg_replication_slots shows a logical slot with active = false and retained_wal of 80 GB. The subscriber server was decommissioned weeks ago and cannot be reached. What steps remove the slot and stop WAL growth, and why can you not simply run DROP SUBSCRIPTION on the subscriber?
Show answer
If the subscriber still exists but cannot reach the publisher, DROP SUBSCRIPTION fails because it tries to drop the remote slot and cannot connect. On that subscriber, run ALTER SUBSCRIPTION name DISABLE, then ALTER SUBSCRIPTION name SET (slot_name = NONE), then DROP SUBSCRIPTION name. If the subscriber is gone entirely, skip those steps. Then, on the publisher, confirm no process is attached (active_pid is NULL) and run SELECT pg_drop_replication_slot('slot_name'). WAL is released at the next checkpoints, so the retained size drops after they run.