Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o4r66-0002hZ-Qt for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Jun 2022 21:44:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o4r65-0005bD-4f for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Jun 2022 21:44:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o4r64-0005b4-Ny for pgsql-hackers@lists.postgresql.org; Fri, 24 Jun 2022 21:44:12 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o4r5y-0000ll-Lc for pgsql-hackers@lists.postgresql.org; Fri, 24 Jun 2022 21:44:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 25OLi5MY545570 for ; Fri, 24 Jun 2022 17:44:05 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: Core dump in range_table_mutator() MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <545568.1656107045.1@sss.pgh.pa.us> Date: Fri, 24 Jun 2022 17:44:05 -0400 Message-ID: <545569.1656107045@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Commit 64919aaab made pull_up_simple_subquery set rte->subquery = NULL after doing the deed, so that we don't waste cycles copying a now-useless subquery tree around. I discovered today while working on another patch that if you invoke query_tree_mutator or range_table_mutator on the whole Query after that point, range_table_mutator dumps core, because it's expecting subquery links to never be NULL. There's apparently noplace in our core code that does that today, but I'm a bit surprised we've not heard complaints from anyone else. I propose to do this to harden it: diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index 876f84dd39..8d58265010 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -3788,7 +3788,9 @@ range_table_mutator(List *rtable, /* we don't bother to copy eref, aliases, etc; OK? */ break; case RTE_SUBQUERY: - if (!(flags & QTW_IGNORE_RT_SUBQUERIES)) + /* In the planner, subquery is null if it's been flattened */ + if (!(flags & QTW_IGNORE_RT_SUBQUERIES) && + rte->subquery != NULL) { CHECKFLATCOPY(newrte->subquery, rte->subquery, Query); MUTATE(newrte->subquery, newrte->subquery, Query *); regards, tom lane