diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2019-02-22 12:23:00 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2019-02-22 12:23:21 -0500 |
commit | 58947fbd56d1481a86a03087c81f728fdf0be866 (patch) | |
tree | 56ada5d3f825aec386a8003b69ef3770e4a76eb7 /src/backend | |
parent | 398cc6fb93814423f205dee435d1a174068c041a (diff) | |
download | postgresql-58947fbd56d1481a86a03087c81f728fdf0be866.tar.gz postgresql-58947fbd56d1481a86a03087c81f728fdf0be866.zip |
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.
Amit Langote and Tom Lane, per a report from Petr Fedorov.
Discussion: https://postgr.es/m/5da6f0f0-1364-1876-6978-907678f89a3e@phystech.edu
Diffstat (limited to 'src/backend')
-rw-r--r-- | src/backend/optimizer/plan/planner.c | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index b3f3ffca7f0..2da7678a798 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1398,32 +1398,57 @@ inheritance_planner(PlannerInfo *root) * to get control here. */ - /* - * If we managed to exclude every child rel, return a dummy plan; it - * doesn't even need a ModifyTable node. - */ if (subpaths == NIL) { - set_dummy_rel_pathlist(final_rel); - return; - } + /* + * We managed to exclude every child rel, so generate a dummy path + * representing the empty set. Although it's clear that no data will + * be updated or deleted, we will still need to have a ModifyTable + * node so that any statement triggers are executed. (This could be + * cleaner if we fixed nodeModifyTable.c to support zero child nodes, + * but that probably wouldn't be a net win.) + */ + List *tlist; + Path *dummy_path; - /* - * Put back the final adjusted rtable into the master copy of the Query. - * (We mustn't do this if we found no non-excluded children.) - */ - parse->rtable = final_rtable; - root->simple_rel_array_size = save_rel_array_size; - root->simple_rel_array = save_rel_array; - /* Must reconstruct master's simple_rte_array, too */ - root->simple_rte_array = (RangeTblEntry **) - palloc0((list_length(final_rtable) + 1) * sizeof(RangeTblEntry *)); - rti = 1; - foreach(lc, final_rtable) + /* tlist processing never got done, either */ + tlist = root->processed_tlist = preprocess_targetlist(root); + final_rel->reltarget = create_pathtarget(root, tlist); + + /* Make a dummy path, cf set_dummy_rel_pathlist() */ + dummy_path = (Path *) create_append_path(final_rel, NIL, + NULL, 0, NIL); + + /* These lists must be nonempty to make a valid ModifyTable node */ + subpaths = list_make1(dummy_path); + subroots = list_make1(root); + resultRelations = list_make1_int(parse->resultRelation); + if (parse->withCheckOptions) + withCheckOptionLists = list_make1(parse->withCheckOptions); + if (parse->returningList) + returningLists = list_make1(parse->returningList); + } + else { - RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc); + /* + * Put back the final adjusted rtable into the master copy of the + * Query. (We mustn't do this if we found no non-excluded children, + * since we never saved an adjusted rtable at all.) + */ + parse->rtable = final_rtable; + root->simple_rel_array_size = save_rel_array_size; + root->simple_rel_array = save_rel_array; + + /* Must reconstruct master's simple_rte_array, too */ + root->simple_rte_array = (RangeTblEntry **) + palloc0((list_length(final_rtable) + 1) * sizeof(RangeTblEntry *)); + rti = 1; + foreach(lc, final_rtable) + { + RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc); - root->simple_rte_array[rti++] = rte; + root->simple_rte_array[rti++] = rte; + } } /* |