Sub topic 12
Monitoring Replication and Cleaning Up Slots
This chapter assumes basic SQL, familiarity with psql, database roles, and the purpose of PostgreSQL's write-ahead log (WAL). You should also know which database is the publisher and which is the subscriber.
What to monitor
A logical replication slot remembers how far a subscriber has consumed the publisher's WAL. PostgreSQL cannot remove WAL needed by an active slot. If a subscriber stops, the slot can therefore retain growing amounts of WAL and eventually fill the publisher's disk.
Run this query 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
WHERE slot_type = 'logical';The active column shows whether a client currently uses the slot. restart_lsn is the oldest WAL position that PostgreSQL must keep for the slot. confirmed_flush_lsn is the latest position confirmed by the logical replication client. retained_wal gives an estimate of the WAL held back by the slot.
On the subscriber, inspect the subscription worker:
SELECT subname,
pid,
received_lsn,
latest_end_lsn,
latest_end_time,
last_msg_receipt_time
FROM pg_stat_subscription;A missing pid can mean that the worker is not running. An old latest_end_time or last_msg_receipt_time suggests that the subscriber is not receiving current changes. Compare the subscriber's progress with the publisher's slot activity before diagnosing a problem.
| Observation | Likely meaning | First check |
|---|---|---|
| `active = false` and retained WAL grows | No client is consuming the slot | Subscription status and network connection |
| Worker has no `pid` | Subscription worker is stopped or restarting | PostgreSQL logs and subscription settings |
| Worker is active but progress is old | Apply or connection problem | Subscriber logs and table conflicts |
| Retained WAL is near available disk space | Publisher may run out of disk | Stop the cause or remove the unused slot |
Investigate before cleanup
Check PostgreSQL logs on both servers for authentication failures, network errors, missing tables, permission errors, and apply conflicts. Also check disk usage on the publisher. A large retained-WAL value can grow between two monitoring queries, so track its trend rather than relying on one measurement.
If a subscription is intentionally paused, decide whether retaining WAL is acceptable. If it is not, fix or remove the subscription. Dropping a slot discards the subscriber's position. The subscriber cannot continue from that slot without a new synchronization or another recovery plan.
Safely remove a subscription and slot
Normally, dropping a subscription also attempts to remove its remote slot. If the publisher is unreachable, detach the slot first on the subscriber:
ALTER SUBSCRIPTION orders_sub DISABLE;
ALTER SUBSCRIPTION orders_sub SET (slot_name = NONE);
DROP SUBSCRIPTION orders_sub;After confirming that the slot is no longer needed, remove a leftover slot on the publisher:
SELECT slot_name, active
FROM pg_replication_slots
WHERE slot_name = 'orders_sub';
SELECT pg_drop_replication_slot('orders_sub');Do not run the final statement while the slot is active. If it is active, identify the client using it and stop that client through its normal administration procedure.
Exercise
A logical slot is inactive and retains 80 GB of WAL. The subscription may be needed next week. Should you drop the slot immediately?
Show answer
No. Confirm the subscription's recovery plan and available disk space first. Dropping the slot discards its position. If the subscription is no longer needed, detach it from the subscription, remove the subscription, and then drop the confirmed-unused slot.