From 25019d1cc56c884c264ca1daed9b6719f76da4c5 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 12 Feb 2019 10:21:05 +0900 Subject: [PATCH] Refactor is_wal_replay_paused() query Make sure it doesn't emit an error if executed on a node not in recovery. The caller should theoretically only execute it on nodes in recovery, but there are sure to be corner cases where the node has come out of recovery. --- dbutils.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/dbutils.c b/dbutils.c index a781f206..f70d2b70 100644 --- a/dbutils.c +++ b/dbutils.c @@ -5079,26 +5079,37 @@ is_wal_replay_paused(PGconn *conn, bool check_pending_wal) initPQExpBuffer(&query); + appendPQExpBufferStr(&query, + "SELECT paused.wal_replay_paused "); + if (PQserverVersion(conn) >= 100000) { - appendPQExpBufferStr(&query, - "SELECT pg_catalog.pg_is_wal_replay_paused()"); - if (check_pending_wal == true) { appendPQExpBufferStr(&query, - " AND pg_catalog.pg_last_wal_replay_lsn() < pg_catalog.pg_last_wal_receive_lsn()"); + " AND pg_catalog.pg_last_wal_replay_lsn() < pg_catalog.pg_last_wal_receive_lsn() "); } + + appendPQExpBufferStr(&query, + " FROM (SELECT CASE WHEN pg_catalog.pg_is_in_recovery() IS FALSE " + " THEN FALSE " + " ELSE pg_catalog.pg_is_wal_replay_paused() " + " END AS wal_replay_paused) paused "); } else { - appendPQExpBufferStr(&query, - "SELECT pg_catalog.pg_is_xlog_replay_paused()"); if (check_pending_wal == true) { appendPQExpBufferStr(&query, - " AND pg_catalog.pg_last_xlog_replay_location() < pg_catalog.pg_last_xlog_receive_location()"); + " AND pg_catalog.pg_last_xlog_replay_location() < pg_catalog.pg_last_xlog_receive_location() "); } + + appendPQExpBufferStr(&query, + " FROM (SELECT CASE WHEN pg_catalog.pg_is_in_recovery() IS FALSE " + " THEN FALSE " + " ELSE pg_catalog.pg_is_xlog_replay_paused() " + " END AS wal_replay_paused) paused "); + } res = PQexec(conn, query.data);