Steps to reproduce:
- Create Postgresql master-slave replication servers
- Monitor it using standart Template DB PostgreSQL
- Start some workload (pgbench)
- Kill some process of master postgresql server with kill -9 <pid>
Restart master postgresql server
Look at pgsql.replication.lag.sec item - the values start to rise.
When postgres start after unclean shutdown it perform some initial recovery before database became open. That recover process involves some WAL replay, so pg_last_wal_replay_lsn() points to recovery end. A lag measured by pgsql.replication.lag.sql script grows linear by time.
So i have covered original script with pg_is_in_recovery() function.