pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Alexander Lakhin <exclusion@gmail.com>
Cc: Richard Guo <guofenglinux@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: Killing off removed rels properly
Date: Mon, 20 Feb 2023 14:13:35 -0500
Message-ID: <3636145.1676920415@sss.pgh.pa.us> (raw)
In-Reply-To: <3628050.1676916703@sss.pgh.pa.us>
References: <229905.1676062220@sss.pgh.pa.us>
	<CAMbWs4_zhe22XqurMdcTx0PpSovHnCJSii3LXiutF_Hpdb9bxg@mail.gmail.com>
	<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>

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

Attachments:

dont-elide-MERGE-target-rel.patch (text/x-diff, 731B) download diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/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 build + * a join tree that looks exactly like that. + */ + if (innerrelid == root->parse->resultRelation) + return false; + innerrel = find_base_rel(root, innerrelid); /*

view thread (9+ messages)  latest in thread

Message-ID: <3636145.1676920415@sss.pgh.pa.us>
Permalink:  ../3636145.1676920415@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3636145.1676920415@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, dgrowleyml@gmail.com, exclusion@gmail.com, guofenglinux@gmail.com
  Subject: Re: Killing off removed rels properly
  In-Reply-To: <3636145.1676920415@sss.pgh.pa.us>
  References: <229905.1676062220@sss.pgh.pa.us> <CAMbWs4_zhe22XqurMdcTx0PpSovHnCJSii3LXiutF_Hpdb9bxg@mail.gmail.com> <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> <3636145.1676920415@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox