pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Cc: Pg Hackers <pgsql-hackers@postgresql.org>
Subject: Re: pgsql: Fix plan created for inherited UPDATE/DELETE with all tables exc
Date: Thu, 18 Apr 2019 11:42:49 +0900
Message-ID: <73719c21-7d66-8add-1f5e-f8cbf9e4a0be@lab.ntt.co.jp> (raw)
In-Reply-To: <E1gxEY7-0002xr-BF@gemulon.postgresql.org>
References: <E1gxEY7-0002xr-BF@gemulon.postgresql.org>

On 2019/02/23 2:23, Tom Lane wrote:
> Fix plan created for inherited UPDATE/DELETE with all tables excluded.
> 
> In the case where inheritance_planner() finds that every table has
> been excluded by constraints, it thought it could get away with
> making a plan consisting of just a dummy Result node.  While certainly
> there's no updating or deleting to be done, this had two user-visible
> problems: the plan did not report the correct set of output columns
> when a RETURNING clause was present, and if there were any
> statement-level triggers that should be fired, it didn't fire them.
> 
> Hence, rather than only generating the dummy Result, we need to
> stick a valid ModifyTable node on top, which requires a tad more
> effort here.
> 
> It's been broken this way for as long as inheritance_planner() has
> known about deleting excluded subplans at all (cf commit 635d42e9c),
> so back-patch to all supported branches.

I noticed that we may have forgotten to fix one more thing in this commit
-- nominalRelation may refer to a range table entry that's not referenced
elsewhere in the finished plan:

create table parent (a int);
create table child () inherits (parent);
explain verbose update parent set a = a where false;
                        QUERY PLAN
───────────────────────────────────────────────────────────
 Update on public.parent  (cost=0.00..0.00 rows=0 width=0)
   Update on public.parent
   ->  Result  (cost=0.00..0.00 rows=0 width=0)
         Output: a, ctid
         One-Time Filter: false
(5 rows)

I think the "Update on public.parent" shown in the 2nd row is unnecessary,
because it won't really be updated.  It's shown because nominalRelation
doesn't match resultRelation, which prompts explain.c to to print the
child target relations separately per this code in show_modifytable_info():

    /* Should we explicitly label target relations? */
    labeltargets = (mtstate->mt_nplans > 1 ||
                    (mtstate->mt_nplans == 1 &&
                     mtstate->resultRelInfo->ri_RangeTableIndex !=
node->nominalRelation));

Attached a patch to adjust nominalRelation in this case so that "parent"
won't be shown a second time, as follows:

explain verbose update parent set a = a where false;
                        QUERY PLAN
───────────────────────────────────────────────────────────
 Update on public.parent  (cost=0.00..0.00 rows=0 width=0)
   ->  Result  (cost=0.00..0.00 rows=0 width=0)
         Output: parent.a, parent.ctid
         One-Time Filter: false
(4 rows)

As you may notice, Result node's targetlist is shown differently than
before, that is, columns are shown prefixed with table name.  In the old
output, the prefix or "refname"  ends up NULL, because the Result node's
targetlist uses resultRelation (1) as varno, which is not referenced
anywhere in the plan tree (for ModifyTable, explain.c prefers to use
nominalRelation instead of resultRelation).  With patch applied,
nominalRelation == resultRelation, so it is referenced and hence its
"refname" non-NULL.  Maybe this change is fine though?

Thanks,
Amit

Attachments:

empty-ModifyTable-nominalRelation-fix.patch (text/plain, 2.0K) download diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 5ed691c2e3..ac1cc51cb4 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1728,6 +1728,7 @@ inheritance_planner(PlannerInfo *root) subpaths = list_make1(dummy_path); subroots = list_make1(root); resultRelations = list_make1_int(parse->resultRelation); + nominalRelation = parse->resultRelation; if (parse->withCheckOptions) withCheckOptionLists = list_make1(parse->withCheckOptions); if (parse->returningList) diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index c6abcfc3cb..1a8cbd4e6f 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -545,27 +545,25 @@ create table some_tab_child () inherits (some_tab); insert into some_tab_child values(1,2); explain (verbose, costs off) update some_tab set a = a + 1 where false; - QUERY PLAN ----------------------------------- + QUERY PLAN +------------------------------------------------------------- Update on public.some_tab - Update on public.some_tab -> Result - Output: (a + 1), b, ctid + Output: (some_tab.a + 1), some_tab.b, some_tab.ctid One-Time Filter: false -(5 rows) +(4 rows) update some_tab set a = a + 1 where false; explain (verbose, costs off) update some_tab set a = a + 1 where false returning b, a; - QUERY PLAN ----------------------------------- + QUERY PLAN +------------------------------------------------------------- Update on public.some_tab - Output: b, a - Update on public.some_tab + Output: some_tab.b, some_tab.a -> Result - Output: (a + 1), b, ctid + Output: (some_tab.a + 1), some_tab.b, some_tab.ctid One-Time Filter: false -(6 rows) +(5 rows) update some_tab set a = a + 1 where false returning b, a; b | a

view thread (9+ messages)  latest in thread

Message-ID: <73719c21-7d66-8add-1f5e-f8cbf9e4a0be@lab.ntt.co.jp>
Permalink:  ../73719c21-7d66-8add-1f5e-f8cbf9e4a0be@lab.ntt.co.jp/
Also on:    postgresql.org/message-id/73719c21-7d66-8add-1f5e-f8cbf9e4a0be@lab.ntt.co.jp

 ·  · 

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-committers@postgresql.org
  Cc: Langote_Amit_f8@lab.ntt.co.jp, tgl@sss.pgh.pa.us, pgsql-hackers@postgresql.org
  Subject: Re: pgsql: Fix plan created for inherited UPDATE/DELETE with all tables exc
  In-Reply-To: <73719c21-7d66-8add-1f5e-f8cbf9e4a0be@lab.ntt.co.jp>
  References: <E1gxEY7-0002xr-BF@gemulon.postgresql.org> <73719c21-7d66-8add-1f5e-f8cbf9e4a0be@lab.ntt.co.jp>

* 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