1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

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
This commit is contained in:
Tom Lane
2019-02-22 12:23:00 -05:00
parent 545928bbbf
commit 0fe3f6dd45
5 changed files with 152 additions and 17 deletions

View File

@@ -1191,29 +1191,47 @@ inheritance_planner(PlannerInfo *root)
/* Mark result as unordered (probably unnecessary) */
root->query_pathkeys = NIL;
/*
* If we managed to exclude every child rel, return a dummy plan; it
* doesn't even need a ModifyTable node.
*/
if (subplans == NIL)
{
/* although dummy, it must have a valid tlist for executor */
/*
* 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;
Plan *subplan;
/* tlist processing never got done, either */
tlist = preprocess_targetlist(root);
return (Plan *) make_result(root,
tlist,
(Node *) list_make1(makeBoolConst(false,
false)),
NULL);
}
/*
* Put back the final adjusted rtable into the master copy of the Query.
*/
parse->rtable = final_rtable;
root->simple_rel_array_size = save_rel_array_size;
root->simple_rel_array = save_rel_array;
subplan = (Plan *) make_result(root,
tlist,
(Node *) list_make1(makeBoolConst(false,
false)),
NULL);
/* These lists must be nonempty to make a valid ModifyTable node */
subplans = list_make1(subplan);
resultRelations = list_make1_int(parse->resultRelation);
if (parse->withCheckOptions)
withCheckOptionLists = list_make1(parse->withCheckOptions);
if (parse->returningList)
returningLists = list_make1(parse->returningList);
}
else
{
/*
* 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;
}
/*
* If there was a FOR [KEY] UPDATE/SHARE clause, the LockRows node will