diff --git a/src/include/pool.h b/src/include/pool.h
index 549aed30f..7ead2e926 100644
--- a/src/include/pool.h
+++ b/src/include/pool.h
@@ -345,8 +345,8 @@ extern int	pool_get_major_version(void);
 extern bool pool_is_node_to_be_sent_in_current_query(int node_id);
 extern int	pool_virtual_main_db_node_id(void);
 
-extern BACKEND_STATUS *my_backend_status[];
-extern int	my_main_node_id;
+extern volatile BACKEND_STATUS *my_backend_status[];
+extern volatile int	my_main_node_id;
 
 #define VALID_BACKEND(backend_id) \
 	((RAW_MODE && (backend_id) == REAL_MAIN_NODE_ID) ||		\
@@ -662,7 +662,7 @@ extern volatile sig_atomic_t ignore_sigusr1;
 #define QUERY_STRING_BUFFER_LEN 1024
 extern char query_string_buffer[];	/* last query string sent to simpleQuery() */
 
-extern BACKEND_STATUS private_backend_status[MAX_NUM_BACKENDS];
+extern volatile BACKEND_STATUS private_backend_status[MAX_NUM_BACKENDS];
 
 extern char remote_host[];		/* client host */
 extern char remote_port[];		/* client port */
diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c
index 19d89cb79..a99b1b5b5 100644
--- a/src/main/pgpool_main.c
+++ b/src/main/pgpool_main.c
@@ -226,7 +226,7 @@ static pid_t health_check_pids[MAX_NUM_BACKENDS];
 /*
  * Private copy of backend status
  */
-BACKEND_STATUS private_backend_status[MAX_NUM_BACKENDS];
+volatile BACKEND_STATUS private_backend_status[MAX_NUM_BACKENDS];
 
 /*
  * shmem connection info table
@@ -278,7 +278,7 @@ static pid_t wd_lifecheck_pid = 0;	/* pid for child process handling watchdog
 									 * lifecheck */
 
 BACKEND_STATUS *my_backend_status[MAX_NUM_BACKENDS];	/* Backend status buffer */
-int			my_main_node_id;	/* Main node id buffer */
+volatile int			my_main_node_id;	/* Main node id buffer */
 
 /*
  * Dummy variable to suppress compiler warnings by discarding return values
diff --git a/src/protocol/child.c b/src/protocol/child.c
index 4a527c84c..67a3c6c9a 100644
--- a/src/protocol/child.c
+++ b/src/protocol/child.c
@@ -1483,7 +1483,20 @@ pool_initialize_private_backend_status(void)
 		my_backend_status[i] = &private_backend_status[i];
 	}
 
-	my_main_node_id = REAL_MAIN_NODE_ID;
+ 	my_main_node_id = REAL_MAIN_NODE_ID;
+
+	/*
+	 * REAL_MAIN_NODE_ID and the per-node status are read from shared memory
+	 * non-atomically, so a concurrent failover can leave my_main_node_id
+	 * pointing at a node we just captured as down. Re-point it at the first
+	 * node that is up in our private snapshot; otherwise MAIN_CONNECTION()
+	 * would dereference a connection slot that is never created for a down
+	 * node.
+	 */
+	if (my_main_node_id < 0 || my_main_node_id >= NUM_BACKENDS ||
+		(private_backend_status[my_main_node_id] != CON_UP &&
+		 private_backend_status[my_main_node_id] != CON_CONNECT_WAIT))
+		my_main_node_id = get_next_main_node();
 }
 
 static void
diff --git a/src/protocol/pool_connection_pool.c b/src/protocol/pool_connection_pool.c
index b16ccc39e..a939b08a7 100644
--- a/src/protocol/pool_connection_pool.c
+++ b/src/protocol/pool_connection_pool.c
@@ -1035,6 +1035,26 @@ new_connection(POOL_CONNECTION_POOL *p)
 
 	if (active_backend_count > 0)
 	{
+		/*
+		 * A concurrent failover may have changed backend status while we were
+		 * creating connections, leaving my_main_node_id pointing at a node we
+		 * skipped (its slot is NULL). Re-point it at a node we did connect to
+		 * so that MAIN_CONNECTION() never dereferences a NULL slot (e.g. in
+		 * pool_do_auth()).
+		 */
+		if (my_main_node_id < 0 || my_main_node_id >= NUM_BACKENDS ||
+			p->slots[my_main_node_id] == NULL)
+		{
+			for (i = 0; i < NUM_BACKENDS; i++)
+			{
+				if (p->slots[i] != NULL)
+				{
+					my_main_node_id = i;
+					break;
+				}
+			}
+		}
+
 		return p;
 	}
 
