Skip to content

Conversation

@v0112358
Copy link

In case the query pg_replication run in the slave which currently has no database traffic (for example in the middle of the night), the exporter will show an incorrect lag value.

Example from idle PostgreSQL cluster:

  • Current WAL lsn (from master)
# select pg_current_wal_lsn(); pg_current_wal_lsn -------------------- 1/3E029AE8 (1 row) 
  • Current WAL recevice lsn (from slave)
# select pg_last_wal_receive_lsn(); pg_last_wal_receive_lsn ------------------------- 1/3E029AE8 (1 row) 
  • Current WAL replay lsn (from slave)
# select pg_last_wal_replay_lsn(); pg_last_wal_replay_lsn ------------------------ 1/3E029AE8 (1 row) 
  • Current replication lag (from slave)
# SELECT CASE WHEN NOT pg_is_in_recovery() THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))) END AS lag; lag ------------- 7466.495493 (1 row) 
  • Improved query (from slave)
# SELECT CASE WHEN NOT pg_is_in_recovery() THEN 0 WHEN pg_last_wal_receive_lsn () = pg_last_wal_replay_lsn () THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))) END AS lag; lag ----- 0 (1 row) 
…L cluster Signed-off-by: v0112358 <ntv1090@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants