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 1pVbKV-0007EF-3Y for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Feb 2023 16:53:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pVbKT-0000hx-7J for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Feb 2023 16:53:53 +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 1pVbKS-0000ho-VA for pgsql-bugs@lists.postgresql.org; Fri, 24 Feb 2023 16:53:52 +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 1pVbKO-0003Vf-OT for pgsql-bugs@lists.postgresql.org; Fri, 24 Feb 2023 16:53:52 +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 31OGrjqL847248; Fri, 24 Feb 2023 11:53:45 -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> <716957.1677189004@sss.pgh.pa.us> Comments: In-reply-to Dean Rasheed message dated "Fri, 24 Feb 2023 08:50:45 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <847246.1677257625.1@sss.pgh.pa.us> Date: Fri, 24 Feb 2023 11:53:45 -0500 Message-ID: <847247.1677257625@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dean Rasheed writes: > Here's an updated patch, now with test cases involving both OLD and > NEW references. It might be worth doing + if (rte->rtekind == RTE_SUBQUERY && !rte->lateral && + contain_vars_of_level((Node *) rte->subquery, 1)) + rte->lateral = true; so as to save the expense of contain_vars_of_level() when the flag is already set. However, it's arguable that no users would bother with writing LATERAL, so this might be pointless. More importantly, I think the comment could do with a bit more information. Maybe like /* + * Mark any subquery RTEs in the rule action as LATERAL if they contain + * Vars referring to the current query level (references to NEW/OLD). + * Those really are lateral references, but we've historically not + * required users to mark such subqueries with LATERAL explicitly. + * But the planner will complain if such Vars exist in a non-LATERAL + * subquery, so we have to fix things up here. + */ That might be overly verbose, but I think it's good to memorialize this sort of info. LGTM otherwise. regards, tom lane