Getting replication lag in PostgreSQL


Here a 2 simple queries to get the replication lag on Postgres servers:

select now() – pg_last_xact_replay_timestamp() as lag;

select pg_last_xlog_receive_location() receive, pg_last_xlog_replay_location() replay, ( extract(epoch FROM now()) – extract(epoch FROM pg_last_xact_replay_timestamp()) )::int lag;