Replication lag and slots
Overview
Lag can be expressed as positions, bytes, or time, and receive, flush and replay progress are different. PostgreSQL LSN is a WAL byte position, not a transaction number or timestamp. Monitor sender and receiver progress and workload; an idle system may make time metrics misleading. Replication slots retain required WAL and sometimes tuple/catalog history; inactive slots can cause disk pressure, and retention limits can make slots unusable.
First example
SELECT node,5-received,5-flushed,5-applied FROM ra_progress ORDER BY node;Observed SQLite result (rows in JSON):
[ [ "A", 0, 1, 2 ], [ "B", 1, 1, 1 ]]How it works
Here sequence gaps count events, not bytes or seconds. A is two events behind in apply and B one. A separate byte-position model uses head 10000 and each slot restart position; the slow slot spans 8000 bytes. Retention spans overlap, so do not sum them as physical disk usage. Actual WAL segment allocation and other retention policies affect disk size.
Practical example
SELECT name,10000-restart_byte FROM ra_slot ORDER BY name;Observed SQLite result (rows in JSON):
[ [ "fast", 2000 ], [ "slow", 8000 ]]Practice and mistakes
Practice: determine the oldest required byte and the largest lag. Hint 1: use MIN(restart_byte). Hint 2: subtract from the current head. Do not drop a real slot merely to clear space; coordinate consumer recovery and retention with an administrator.
PostgreSQL 17 example — NOT RUN
SELECT slot_name,active,restart_lsn,confirmed_flush_lsn,pg_wal_lsn_diff(pg_current_wal_lsn(),restart_lsn) AS retained_span_bytes FROM pg_replication_slots ORDER BY slot_name;Expected result, not executed: "Run on an authorized primary only. NULL positions and empty rows are possible; byte span is not exact disk usage."