diff --git a/pgwatch2/metrics/replication_slots/10/metric_master.sql b/pgwatch2/metrics/replication_slots/10/metric_master.sql index 46867515..b5f8aa8f 100644 --- a/pgwatch2/metrics/replication_slots/10/metric_master.sql +++ b/pgwatch2/metrics/replication_slots/10/metric_master.sql @@ -4,8 +4,10 @@ select /* pgwatch2_generated */ coalesce(plugin, 'physical')::text as tag_plugin, active, case when active then 0 else 1 end as non_active_int, - pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::int8 as restart_lsn_lag_b, - pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)::int8 as confirmed_flush_lsn_lag_b, + case when not pg_is_in_recovery() then pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::int8 + else pg_wal_lsn_diff(pg_last_wal_replay_lsn(), restart_lsn)::int8 end as restart_lsn_lag_b, + case when not pg_is_in_recovery() then pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)::int8 + else pg_wal_lsn_diff(pg_last_wal_replay_lsn(), confirmed_flush_lsn)::int8 end as confirmed_flush_lsn_lag_b, greatest(age(xmin), age(catalog_xmin))::int8 as xmin_age_tx from pg_replication_slots; diff --git a/pgwatch2/sql/config_store/metric_definitions.sql b/pgwatch2/sql/config_store/metric_definitions.sql index 7581b71a..4e500dfb 100644 --- a/pgwatch2/sql/config_store/metric_definitions.sql +++ b/pgwatch2/sql/config_store/metric_definitions.sql @@ -6929,8 +6929,10 @@ select coalesce(plugin, 'physical')::text as plugin, active, case when active then 0 else 1 end as non_active_int, - pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::int8 as restart_lsn_lag_b, - pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)::int8 as confirmed_flush_lsn_lag_b, + case when not pg_is_in_recovery() then pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::int8 + else pg_wal_lsn_diff(pg_last_wal_replay_lsn(), restart_lsn)::int8 end as restart_lsn_lag_b, + case when not pg_is_in_recovery() then pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)::int8 + else pg_wal_lsn_diff(pg_last_wal_replay_lsn(), confirmed_flush_lsn)::int8 end as confirmed_flush_lsn_lag_b, greatest(age(xmin), age(catalog_xmin))::int8 as xmin_age_tx from pg_replication_slots;