From 0595320c87c4bcab9e346cf7904f7e4b00454388 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Wed, 23 Oct 2024 09:55:00 -0600 Subject: [PATCH] Protect call to pg_current_wal_lsn() in retained_wal query We can't call pg_current_wal_lsn() if we are a standby instance (read replica). Any attempt to call this function while in recovery results in: ERROR: recovery is in progress Signed-off-by: Tristan Partin --- compute/etc/sql_exporter/retained_wal.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compute/etc/sql_exporter/retained_wal.sql b/compute/etc/sql_exporter/retained_wal.sql index 6c58359461..3e2aadfc28 100644 --- a/compute/etc/sql_exporter/retained_wal.sql +++ b/compute/etc/sql_exporter/retained_wal.sql @@ -1,5 +1,10 @@ SELECT slot_name, - pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::FLOAT8 AS retained_wal + pg_wal_lsn_diff( + CASE + WHEN pg_is_in_recovery() THEN pg_last_wal_replay_lsn() + ELSE pg_current_wal_lsn() + END, + restart_lsn)::FLOAT8 AS retained_wal FROM pg_replication_slots WHERE active = false;