From bcd345a8738c412bb0626e34cd6c34857d5a1924 Mon Sep 17 00:00:00 2001 From: Andreas Karlsson Date: Thu, 20 Nov 2025 18:19:31 +0100 Subject: [PATCH] PG-2014 Initalize nested query stack to fix crash on DDL The pgsm_ProcessUtility() which handles DDL increments nesting_level but does not put a query text on the next_queries stack while pgsm_ExecutorRun() does both. It is unclear to me if this is a mistake or by design but since readers of the query check for is the query text pointer is NULL and pgsm_ExecutorRun() reset the pointer to NULL before returning it is safe as long as we initialize the stack to all NULL pointers, which we did not. This bug was found by our test suite in Jenkins on some RHEL based distro version and seems to mostly happen when the first query of a backend is CREATE EXTENSION and we have enabled query normalization but it is entirely possible that it could happen under other circumstances too. The use of calloc() over palloc0() is to keep the patch small since the previous code used malloc(). --- pg_stat_monitor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index 0d270cf..6630326 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -343,7 +343,7 @@ _PG_init(void) ExecutorCheckPerms_hook = HOOK(pgsm_ExecutorCheckPerms); nested_queryids = (int64 *) malloc(sizeof(int64) * max_stack_depth); - nested_query_txts = (char **) malloc(sizeof(char *) * max_stack_depth); + nested_query_txts = (char **) calloc(max_stack_depth, sizeof(char *)); system_init = true; }