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 1pUBbg-0004Ip-U4 for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Feb 2023 19:13:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pUBbZ-0007xK-Sw for pgsql-hackers@arkaria.postgresql.org; Mon, 20 Feb 2023 19:13:41 +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 1pUBbZ-0007xA-JH for pgsql-hackers@lists.postgresql.org; Mon, 20 Feb 2023 19:13:41 +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 1pUBbV-0000gJ-3P for pgsql-hackers@lists.postgresql.org; Mon, 20 Feb 2023 19:13:38 +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 31KJDZQQ3636146; Mon, 20 Feb 2023 14:13:35 -0500 From: Tom Lane To: David Rowley cc: Alexander Lakhin , Richard Guo , pgsql-hackers@lists.postgresql.org Subject: Re: Killing off removed rels properly In-reply-to: <3628050.1676916703@sss.pgh.pa.us> References: <229905.1676062220@sss.pgh.pa.us> <1343881.1676313553@sss.pgh.pa.us> <36bee393-b351-16ac-93b2-d46d83637e45@gmail.com> <3582657.1676910808@sss.pgh.pa.us> <3587195.1676912023@sss.pgh.pa.us> <3628050.1676916703@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 20 Feb 2023 13:11:43 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3636052.1676920325.0@sss.pgh.pa.us> Date: Mon, 20 Feb 2023 14:13:35 -0500 Message-ID: <3636145.1676920415@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3636052.1676920325.1@sss.pgh.pa.us> I wrote: >> It looks like we're somehow triggering the elide-a-left-join code >> when we shouldn't? So the reason why we see this with a partitioned target table and not with a regular target table reduces to this bit in preprocess_targetlist: /* * For non-inherited UPDATE/DELETE/MERGE, register any junk column(s) * needed to allow the executor to identify the rows to be updated or * deleted. In the inheritance case, we do nothing now, leaving this to * be dealt with when expand_inherited_rtentry() makes the leaf target * relations. (But there might not be any leaf target relations, in which * case we must do this in distribute_row_identity_vars().) */ if ((command_type == CMD_UPDATE || command_type == CMD_DELETE || command_type == CMD_MERGE) && !target_rte->inh) { /* row-identity logic expects to add stuff to processed_tlist */ root->processed_tlist = tlist; add_row_identity_columns(root, result_relation, target_rte, target_relation); tlist = root->processed_tlist; } This happens before join removal, so that we see use of the row identity columns of a regular table as a reason not to do join removal. But expand_inherited_rtentry will happen after that, too late to stop join removal. I think the best fix is just to teach join removal that it mustn't remove the result relation, as attached (needs a regression test). I'm a little inclined to back-patch this, even though I think it's probably unreachable in v15. It's a cheap enough safety measure, and at the very least it will save a few cycles deciding that we can't remove the target table of a MERGE. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="dont-elide-MERGE-target-rel.patch"; charset="us-ascii" Content-ID: <3636052.1676920325.2@sss.pgh.pa.us> Content-Description: dont-elide-MERGE-target-rel.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optim= izer/plan/analyzejoins.c index 0dfefd71f2..f79bc4430c 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -183,6 +183,14 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo = *sjinfo) if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid)) return false; = + /* + * Never try to eliminate a left join to the query result rel. Although + * the case is syntactically impossible in standard SQL, MERGE will buil= d + * a join tree that looks exactly like that. + */ + if (innerrelid =3D=3D root->parse->resultRelation) + return false; + innerrel =3D find_base_rel(root, innerrelid); = /* ------- =_aaaaaaaaaa0--