mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Rework planning and execution of UPDATE and DELETE.
This patch makes two closely related sets of changes: 1. For UPDATE, the subplan of the ModifyTable node now only delivers the new values of the changed columns (i.e., the expressions computed in the query's SET clause) plus row identity information such as CTID. ModifyTable must re-fetch the original tuple to merge in the old values of any unchanged columns. The core advantage of this is that the changed columns are uniform across all tables of an inherited or partitioned target relation, whereas the other columns might not be. A secondary advantage, when the UPDATE involves joins, is that less data needs to pass through the plan tree. The disadvantage of course is an extra fetch of each tuple to be updated. However, that seems to be very nearly free in context; even worst-case tests don't show it to add more than a couple percent to the total query cost. At some point it might be interesting to combine the re-fetch with the tuple access that ModifyTable must do anyway to mark the old tuple dead; but that would require a good deal of refactoring and it seems it wouldn't buy all that much, so this patch doesn't attempt it. 2. For inherited UPDATE/DELETE, instead of generating a separate subplan for each target relation, we now generate a single subplan that is just exactly like a SELECT's plan, then stick ModifyTable on top of that. To let ModifyTable know which target relation a given incoming row refers to, a tableoid junk column is added to the row identity information. This gets rid of the horrid hack that was inheritance_planner(), eliminating O(N^2) planning cost and memory consumption in cases where there were many unprunable target relations. Point 2 of course requires point 1, so that there is a uniform definition of the non-junk columns to be returned by the subplan. We can't insist on uniform definition of the row identity junk columns however, if we want to keep the ability to have both plain and foreign tables in a partitioning hierarchy. Since it wouldn't scale very far to have every child table have its own row identity column, this patch includes provisions to merge similar row identity columns into one column of the subplan result. In particular, we can merge the whole-row Vars typically used as row identity by FDWs into one column by pretending they are type RECORD. (It's still okay for the actual composite Datums to be labeled with the table's rowtype OID, though.) There is more that can be done to file down residual inefficiencies in this patch, but it seems to be committable now. FDW authors should note several API changes: * The argument list for AddForeignUpdateTargets() has changed, and so has the method it must use for adding junk columns to the query. Call add_row_identity_var() instead of manipulating the parse tree directly. You might want to reconsider exactly what you're adding, too. * PlanDirectModify() must now work a little harder to find the ForeignScan plan node; if the foreign table is part of a partitioning hierarchy then the ForeignScan might not be the direct child of ModifyTable. See postgres_fdw for sample code. * To check whether a relation is a target relation, it's no longer sufficient to compare its relid to root->parse->resultRelation. Instead, check it against all_result_relids or leaf_result_relids, as appropriate. Amit Langote and Tom Lane Discussion: https://postgr.es/m/CA+HiwqHpHdqdDn48yCEhynnniahH78rwcrv1rEX65-fsZGBOLQ@mail.gmail.com
This commit is contained in:
@ -868,6 +868,29 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
|
||||
set_upper_references(root, plan, rtoffset);
|
||||
else
|
||||
{
|
||||
/*
|
||||
* The tlist of a childless Result could contain
|
||||
* unresolved ROWID_VAR Vars, in case it's representing a
|
||||
* target relation which is completely empty because of
|
||||
* constraint exclusion. Replace any such Vars by null
|
||||
* constants, as though they'd been resolved for a leaf
|
||||
* scan node that doesn't support them. We could have
|
||||
* fix_scan_expr do this, but since the case is only
|
||||
* expected to occur here, it seems safer to special-case
|
||||
* it here and keep the assertions that ROWID_VARs
|
||||
* shouldn't be seen by fix_scan_expr.
|
||||
*/
|
||||
foreach(l, splan->plan.targetlist)
|
||||
{
|
||||
TargetEntry *tle = (TargetEntry *) lfirst(l);
|
||||
Var *var = (Var *) tle->expr;
|
||||
|
||||
if (var && IsA(var, Var) && var->varno == ROWID_VAR)
|
||||
tle->expr = (Expr *) makeNullConst(var->vartype,
|
||||
var->vartypmod,
|
||||
var->varcollid);
|
||||
}
|
||||
|
||||
splan->plan.targetlist =
|
||||
fix_scan_list(root, splan->plan.targetlist,
|
||||
rtoffset, NUM_EXEC_TLIST(plan));
|
||||
@ -897,23 +920,20 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
|
||||
if (splan->returningLists)
|
||||
{
|
||||
List *newRL = NIL;
|
||||
Plan *subplan = outerPlan(splan);
|
||||
ListCell *lcrl,
|
||||
*lcrr,
|
||||
*lcp;
|
||||
*lcrr;
|
||||
|
||||
/*
|
||||
* Pass each per-subplan returningList through
|
||||
* Pass each per-resultrel returningList through
|
||||
* set_returning_clause_references().
|
||||
*/
|
||||
Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
|
||||
Assert(list_length(splan->returningLists) == list_length(splan->plans));
|
||||
forthree(lcrl, splan->returningLists,
|
||||
lcrr, splan->resultRelations,
|
||||
lcp, splan->plans)
|
||||
forboth(lcrl, splan->returningLists,
|
||||
lcrr, splan->resultRelations)
|
||||
{
|
||||
List *rlist = (List *) lfirst(lcrl);
|
||||
Index resultrel = lfirst_int(lcrr);
|
||||
Plan *subplan = (Plan *) lfirst(lcp);
|
||||
|
||||
rlist = set_returning_clause_references(root,
|
||||
rlist,
|
||||
@ -983,12 +1003,6 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
|
||||
rc->rti += rtoffset;
|
||||
rc->prti += rtoffset;
|
||||
}
|
||||
foreach(l, splan->plans)
|
||||
{
|
||||
lfirst(l) = set_plan_refs(root,
|
||||
(Plan *) lfirst(l),
|
||||
rtoffset);
|
||||
}
|
||||
|
||||
/*
|
||||
* Append this ModifyTable node's final result relation RT
|
||||
@ -1792,6 +1806,13 @@ fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan,
|
||||
* choosing the best implementation for AlternativeSubPlans,
|
||||
* looking up operator opcode info for OpExpr and related nodes,
|
||||
* and adding OIDs from regclass Const nodes into root->glob->relationOids.
|
||||
*
|
||||
* 'node': the expression to be modified
|
||||
* 'rtoffset': how much to increment varnos by
|
||||
* 'num_exec': estimated number of executions of expression
|
||||
*
|
||||
* The expression tree is either copied-and-modified, or modified in-place
|
||||
* if that seems safe.
|
||||
*/
|
||||
static Node *
|
||||
fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset, double num_exec)
|
||||
@ -1840,11 +1861,12 @@ fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
|
||||
Assert(var->varlevelsup == 0);
|
||||
|
||||
/*
|
||||
* We should not see any Vars marked INNER_VAR or OUTER_VAR. But an
|
||||
* indexqual expression could contain INDEX_VAR Vars.
|
||||
* We should not see Vars marked INNER_VAR, OUTER_VAR, or ROWID_VAR.
|
||||
* But an indexqual expression could contain INDEX_VAR Vars.
|
||||
*/
|
||||
Assert(var->varno != INNER_VAR);
|
||||
Assert(var->varno != OUTER_VAR);
|
||||
Assert(var->varno != ROWID_VAR);
|
||||
if (!IS_SPECIAL_VARNO(var->varno))
|
||||
var->varno += context->rtoffset;
|
||||
if (var->varnosyn > 0)
|
||||
@ -1907,6 +1929,7 @@ fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
|
||||
{
|
||||
if (node == NULL)
|
||||
return false;
|
||||
Assert(!(IsA(node, Var) && ((Var *) node)->varno == ROWID_VAR));
|
||||
Assert(!IsA(node, PlaceHolderVar));
|
||||
Assert(!IsA(node, AlternativeSubPlan));
|
||||
fix_expr_common(context->root, node);
|
||||
|
Reference in New Issue
Block a user