Replica Lag in PostgreSQL
It can be calculated in two ways -
1. Time
2. Bytes
Time
SELECT
CASE
WHEN pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() THEN 0
ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())::INTEGER
END
AS replication_lag;
replication_lag
-----------------
28
(1 row)
Bytes
postgres=# SELECT client_addr, pg_wal_lsn_diff(pg_stat_replication.sent_lsn, pg_stat_replication.replay_lsn) AS byte_lag FROM pg_stat_replication;
client_addr | byte_lag
-------------+----------
127.0.0.1 | 0
(1 row)
postgres=#