Show replication lag
PostgreSQLReplicationCommand
SELECT client_addr, state, sent_lsn, replay_lsn, pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS replay_lag_bytes, write_lag, flush_lag, replay_lag FROM pg_stat_replication;What it does
On the primary, shows each standby's replay gap in bytes relative to the primary's current WAL position, plus write/flush/replay lag.
When to use it
To monitor how far behind streaming replication is (run on the primary).
Caution
For lag in seconds on the standby: SELECT now() - pg_last_xact_replay_timestamp(); this value also grows when there are no writes on the primary, so interpret it together with the LSN gap. An empty result means no standby is connected.