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 1pVJTe-00041x-B0 for pgsql-bugs@arkaria.postgresql.org; Thu, 23 Feb 2023 21:50:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pVJTd-0004Ee-83 for pgsql-bugs@arkaria.postgresql.org; Thu, 23 Feb 2023 21:50:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVJTc-0004EV-Vy for pgsql-bugs@lists.postgresql.org; Thu, 23 Feb 2023 21:50:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVJTa-0006iS-SF for pgsql-bugs@lists.postgresql.org; Thu, 23 Feb 2023 21:50:08 +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 31NLo4n5716958; Thu, 23 Feb 2023 16:50:04 -0500 From: Tom Lane To: Dean Rasheed cc: Alexander Lakhin , 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> <3953951.1676991950@sss.pgh.pa.us> <3977948.1677002558@sss.pgh.pa.us> <7f8f1bbb-9fb3-80fb-a400-5c7e755b5aa2@gmail.com> <60780f8c-3132-133e-8286-538b732bff19@gmail.com> <5e09da43-aaba-7ea7-0a51-a2eb981b058b@gmail.com> Comments: In-reply-to Dean Rasheed message dated "Thu, 23 Feb 2023 12:00:30 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <716956.1677189004.1@sss.pgh.pa.us> Date: Thu, 23 Feb 2023 16:50:04 -0500 Message-ID: <716957.1677189004@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dean Rasheed writes: > This is definitely an independent bug, nothing to do with DEFAULTs, > and also not specific to multi-row VALUES lists either, since it also > fails with INSERT INTO v SELECT 1. > Apparently the rewriter is generating a query that the planner is not > able to handle, though I'm not clear on the precise details. It looks to me like the rewriter is failing to set the rte->lateral flag on the sub-select, or maybe the fault is even earlier in the parser. That NEW reference sure looks like a lateral ref to me: CREATE RULE vr AS ON INSERT TO v DO ALSO INSERT INTO t SELECT * FROM (SELECT a FROM t WHERE NEW.a = t.a) tt; The planner is Asserting because it's seeing a reference to RTE 4 (the VALUES RTE) in a place where it'd only expect to see a reference to RTE 8 (the sub-select's "t" rel) unless the query uses LATERAL. Everything goes through fine if I manually add LATERAL: regression=# CREATE or replace RULE vr AS ON INSERT TO v DO ALSO INSERT INTO t SELECT * FROM LATERAL (SELECT a FROM t WHERE NEW.a = t.a) tt; CREATE RULE regression=# INSERT INTO v VALUES (1), (2); INSERT 0 2 Arguably, the user should have written LATERAL on that sub-select in the first place, but we probably can't start enforcing that ex post facto. We'll have to do something that causes NEW (and OLD?) references in sub-selects to generate a LATERAL marking silently. Kinda surprising nobody noticed this before, because I'm sure it's been busted a long time. regards, tom lane