From: Tatsuo Ishii Date: Fri, 21 Jun 2024 05:21:15 +0000 (+0900) Subject: Fix MAIN_NODE macro (actually pool_virtual_main_db_node_id()). X-Git-Tag: V4_4_8~16 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=152c65d6f142a92b70b3c04a21a701baa6a36e21;p=pgpool2.git Fix MAIN_NODE macro (actually pool_virtual_main_db_node_id()). The macro used to REAL_MAIN_NODE_ID if there's no session context. This is wrong since REAL_MAIN_NODE_ID can be changed any time when failover/failback happens. Suppose REAL_MAIN_NODE_ID == my_main_node_id == 1. Then due to failback, REAL_MAIN_NODE_ID is changed to 0. Then MAIN_CONNECTION(cp) will return NULL and any reference to it will cause segmentation fault. To prevent the issue we should return my_main_node_id instead. Discussion: https://www.pgpool.net/pipermail/pgpool-general/2024-June/009205.html Backpatch-through: V4.1 --- diff --git a/src/context/pool_query_context.c b/src/context/pool_query_context.c index 230a116b7..1ba02c712 100644 --- a/src/context/pool_query_context.c +++ b/src/context/pool_query_context.c @@ -361,7 +361,16 @@ pool_virtual_main_db_node_id(void) sc = pool_get_session_context(true); if (!sc) { - return REAL_MAIN_NODE_ID; + /* + * We used to return REAL_MAIN_NODE_ID here. Problem with it is, it + * is possible that REAL_MAIN_NODE_ID could be changed + * anytime. Suppose REAL_MAIN_NODE_ID == my_main_node_id == 1. Then + * due to failback, REAL_MAIN_NODE_ID is changed to 0. Then + * MAIN_CONNECTION(cp) will return NULL and any reference to it will + * cause segmentation fault. To prevent the issue we should return + * my_main_node_id instead. + */ + return my_main_node_id; } if (sc->in_progress && sc->query_context)