From 9be4d1fd665b4a726fd21671c25ea8c112d134c4 Mon Sep 17 00:00:00 2001 From: Tatsuo Ishii Date: Sun, 6 Feb 2022 17:11:52 +0900 Subject: [PATCH] Fix failover() to deal with the case when no former primary node exists. Consider a case when no primary node exists when Pgpool-II starts up. In this case Req_info->primary_node_id is -1. failover() did not consider this and skipped to call find_primary_node_repeatedly(). Also follow_master_command was not executed if Req_info->primary_node_id is -1. This commit fixes the bug above. Discussion: https://www.pgpool.net/pipermail/pgpool-hackers/2022-February/004114.html --- src/main/pgpool_main.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c index 80a25d00f..13049e1f9 100644 --- a/src/main/pgpool_main.c +++ b/src/main/pgpool_main.c @@ -1863,11 +1863,14 @@ failover(void) * we can avoid calling find_primary_node_repeatedly() and recognize * the former primary as the new primary node, which will reduce the * time to process standby down. + * This does not apply to the case when no primary node existed + * (Req_info->primary_node_id < 0). In this case + * find_primary_node_repeatedly() should be called. */ else if (SL_MODE && reqkind == NODE_DOWN_REQUEST) { - if (Req_info->primary_node_id != node_id) + if (Req_info->primary_node_id >= 0 && Req_info->primary_node_id != node_id) { new_primary = Req_info->primary_node_id; } @@ -1901,10 +1904,16 @@ failover(void) if (*pool_config->follow_primary_command != '\0' || reqkind == PROMOTE_NODE_REQUEST) { - /* only if the failover is against the current primary */ + /* + * follow primary command is executed in following cases: + * - failover against the current primary + * - no primary exists and new primary is created by failover + * - promote node request + */ if (((reqkind == NODE_DOWN_REQUEST) && Req_info->primary_node_id >= 0 && (nodes[Req_info->primary_node_id])) || + (reqkind == NODE_DOWN_REQUEST && Req_info->primary_node_id < 0 && new_primary >= 0) || (node_id >= 0 && (reqkind == PROMOTE_NODE_REQUEST) && (VALID_BACKEND(node_id)))) { -- 2.39.5