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 1pUUDQ-0005yP-Kk for pgsql-bugs@arkaria.postgresql.org; Tue, 21 Feb 2023 15:06:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUUDO-00081f-VZ for pgsql-bugs@arkaria.postgresql.org; Tue, 21 Feb 2023 15:05:58 +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 1pUUDO-00081P-Mm for pgsql-bugs@lists.postgresql.org; Tue, 21 Feb 2023 15:05:58 +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 1pUUDI-0002qT-8n for pgsql-bugs@lists.postgresql.org; Tue, 21 Feb 2023 15:05:57 +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 31LF5of03953952; Tue, 21 Feb 2023 10:05:50 -0500 From: Tom Lane To: Dean Rasheed cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17803: Rule "ALSO INSERT ... SELECT ..." fails to substitute default values In-reply-to: References: <17803-53c63ed4ecb4eac6@postgresql.org> Comments: In-reply-to Dean Rasheed message dated "Tue, 21 Feb 2023 11:56:00 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3953950.1676991950.1@sss.pgh.pa.us> Date: Tue, 21 Feb 2023 10:05:50 -0500 Message-ID: <3953951.1676991950@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dean Rasheed writes: > It looks like we need something like the attached, to deal with > product queries that are INSERT ... SELECT queries. In that case the > VALUES RTE will be at the same index, but in the SELECT part of the > product query, not the top-level product query itself. It seems like this bit: + rtr = (RangeTblRef *) linitial(pt->jointree->fromlist); + selectrte = rt_fetch(rtr->rtindex, pt->rtable); + selectquery = selectrte->subquery; is missing several essential checks. Is the node extracted from jointree->fromlist actually a RangeTblRef? Seems like it could be a JoinExpr or FromExpr instead; even if it can't be that today, an IsA check is cheap future-proofing. Likewise, once you've got your hands on the RTE, you should check rtekind == RTE_SUBQUERY rather than assuming it's safe to touch the subquery field. (I see that getInsertSelectQuery isn't much better about this, but we should fix that while we're at it.) regards, tom lane