replication slot이 보존하는 WAL 모니터링 및 방치된 slot 안전하게 삭제
PostgreSQL복제명령어
SELECT slot_name, slot_type, database, active, active_pid, wal_status,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal,
pg_size_pretty(safe_wal_size) AS safe_wal_size,
xmin, catalog_xmin
FROM pg_replication_slots
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC NULLS LAST;
-- Safe drop: drops only if not active (returns 0 rows if active)
SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_name = '<slot>' AND NOT active;하는 일
primary에서 각 slot이 보존하는 WAL 양, wal_status(reserved/extended/unreserved/lost), max_slot_wal_keep_size 대비 남은 안전 여유(safe_wal_size), VACUUM을 붙잡는 xmin/catalog_xmin 값을 보여 준다. 두 번째 쿼리는 사용 중이 아닌 경우에만 slot을 삭제한다.
사용 시점
pg_wal 디렉터리가 커질 때, 디스크 알림 시, 정기 모니터링에서. standby나 subscriber를 영구 제거한 후 slot 정리용.
주의
비활성 slot은 WAL을 무제한 보존하여 디스크를 채우고 primary를 멈출 수 있다. xmin/catalog_xmin을 잡고 있는 slot은 dead tuple 정리도 막아 bloat/wraparound 위험을 만든다. slot을 삭제하면 해당 standby/subscriber가 중단 지점부터 재개할 수 없으므로(재구축 필요) 삭제 전에 소유자를 확인한다. primary에서 실행한다(standby에서는 pg_current_wal_lsn()이 실패한다).