diff --git a/dbutils.c b/dbutils.c index 7074625f..fe441393 100644 --- a/dbutils.c +++ b/dbutils.c @@ -5195,7 +5195,7 @@ set_upstream_last_seen(PGconn *conn) int -get_upstream_last_seen(PGconn *conn) +get_upstream_last_seen(PGconn *conn, t_server_type node_type) { PQExpBufferData query; PGresult *res = NULL; @@ -5203,11 +5203,19 @@ get_upstream_last_seen(PGconn *conn) initPQExpBuffer(&query); - appendPQExpBufferStr(&query, - "SELECT CASE WHEN pg_catalog.pg_is_in_recovery() IS FALSE " - " THEN -1 " - " ELSE repmgr.get_upstream_last_seen() " - " END AS upstream_last_seen "); + if (node_type == WITNESS) + { + appendPQExpBufferStr(&query, + "SELECT repmgr.get_upstream_last_seen()"); + } + else + { + appendPQExpBufferStr(&query, + "SELECT CASE WHEN pg_catalog.pg_is_in_recovery() IS FALSE " + " THEN -1 " + " ELSE repmgr.get_upstream_last_seen() " + " END AS upstream_last_seen "); + } res = PQexec(conn, query.data); diff --git a/dbutils.h b/dbutils.h index f4d9ea0f..3982e030 100644 --- a/dbutils.h +++ b/dbutils.h @@ -559,7 +559,7 @@ int get_replication_lag_seconds(PGconn *conn); void get_node_replication_stats(PGconn *conn, t_node_info *node_info); bool is_downstream_node_attached(PGconn *conn, char *node_name); void set_upstream_last_seen(PGconn *conn); -int get_upstream_last_seen(PGconn *conn); +int get_upstream_last_seen(PGconn *conn, t_server_type node_type); bool is_wal_replay_paused(PGconn *conn, bool check_pending_wal); /* BDR functions */ diff --git a/repmgr-action-daemon.c b/repmgr-action-daemon.c index fdbfbb66..97e7c4a6 100644 --- a/repmgr-action-daemon.c +++ b/repmgr-action-daemon.c @@ -201,8 +201,7 @@ do_daemon_status(void) } } - repmgrd_info[i]->upstream_last_seen = get_upstream_last_seen(cell->node_info->conn); - + repmgrd_info[i]->upstream_last_seen = get_upstream_last_seen(cell->node_info->conn, cell->node_info->type); if (repmgrd_info[i]->upstream_last_seen < 0) { maxlen_snprintf(repmgrd_info[i]->upstream_last_seen_text, "%s", _("n/a")); diff --git a/repmgr.c b/repmgr.c index 19837f4d..29a12785 100644 --- a/repmgr.c +++ b/repmgr.c @@ -391,10 +391,6 @@ get_upstream_last_seen(PG_FUNCTION_ARGS) if (!shared_state) PG_RETURN_INT32(-1); - /* A primary is always visible */ - if (!RecoveryInProgress()) - PG_RETURN_INT32(0); - LWLockAcquire(shared_state->lock, LW_SHARED); last_seen = shared_state->upstream_last_seen; diff --git a/repmgrd-physical.c b/repmgrd-physical.c index 92ca2a16..274c0688 100644 --- a/repmgrd-physical.c +++ b/repmgrd-physical.c @@ -1612,7 +1612,11 @@ monitor_streaming_witness(void) while (true) { - if (check_upstream_connection(&upstream_conn, upstream_node_info.conninfo) == false) + if (check_upstream_connection(&primary_conn, upstream_node_info.conninfo) == true) + { + set_upstream_last_seen(local_conn); + } + else { if (upstream_node_info.node_status == NODE_STATUS_UP) { @@ -3440,6 +3444,7 @@ do_election(NodeInfoList *sibling_nodes) } } + /* don't interrogate a witness server */ if (cell->node_info->type == WITNESS) { @@ -3503,12 +3508,12 @@ do_election(NodeInfoList *sibling_nodes) * configurable. */ - if (sibling_replication_info.upstream_last_seen >= 0 && sibling_replication_info.upstream_last_seen < (config_file_options.monitor_interval_secs * 2)) { nodes_with_primary_still_visible++; log_notice(_("node %i last saw primary node %i second(s) ago, considering primary still visible"), - cell->node_info->node_id, sibling_replication_info.upstream_last_seen); + cell->node_info->node_id, + sibling_replication_info.upstream_last_seen); appendPQExpBuffer(&nodes_with_primary_visible, " - node \"%s\" (ID: %i): %i second(s) ago\n", cell->node_info->node_name, @@ -3518,8 +3523,10 @@ do_election(NodeInfoList *sibling_nodes) else { log_info(_("node %i last saw primary node %i second(s) ago"), - cell->node_info->node_id, sibling_replication_info.upstream_last_seen); + cell->node_info->node_id, + sibling_replication_info.upstream_last_seen); } + /* get node's last receive LSN - if "higher" than current winner, current node is candidate */ cell->node_info->last_wal_receive_lsn = sibling_replication_info.last_wal_receive_lsn;