mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +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:
@ -77,18 +77,6 @@ typedef enum UpperRelationKind
|
||||
/* NB: UPPERREL_FINAL must be last enum entry; it's used to size arrays */
|
||||
} UpperRelationKind;
|
||||
|
||||
/*
|
||||
* This enum identifies which type of relation is being planned through the
|
||||
* inheritance planner. INHKIND_NONE indicates the inheritance planner
|
||||
* was not used.
|
||||
*/
|
||||
typedef enum InheritanceKind
|
||||
{
|
||||
INHKIND_NONE,
|
||||
INHKIND_INHERITED,
|
||||
INHKIND_PARTITIONED
|
||||
} InheritanceKind;
|
||||
|
||||
/*----------
|
||||
* PlannerGlobal
|
||||
* Global information for planning/optimization
|
||||
@ -276,6 +264,17 @@ struct PlannerInfo
|
||||
|
||||
List *join_info_list; /* list of SpecialJoinInfos */
|
||||
|
||||
/*
|
||||
* all_result_relids is empty for SELECT, otherwise it contains at least
|
||||
* parse->resultRelation. For UPDATE/DELETE across an inheritance or
|
||||
* partitioning tree, the result rel's child relids are added. When using
|
||||
* multi-level partitioning, intermediate partitioned rels are included.
|
||||
* leaf_result_relids is similar except that only actual result tables,
|
||||
* not partitioned tables, are included in it.
|
||||
*/
|
||||
Relids all_result_relids; /* set of all result relids */
|
||||
Relids leaf_result_relids; /* set of all leaf relids */
|
||||
|
||||
/*
|
||||
* Note: for AppendRelInfos describing partitions of a partitioned table,
|
||||
* we guarantee that partitions that come earlier in the partitioned
|
||||
@ -283,6 +282,8 @@ struct PlannerInfo
|
||||
*/
|
||||
List *append_rel_list; /* list of AppendRelInfos */
|
||||
|
||||
List *row_identity_vars; /* list of RowIdentityVarInfos */
|
||||
|
||||
List *rowMarks; /* list of PlanRowMarks */
|
||||
|
||||
List *placeholder_list; /* list of PlaceHolderInfos */
|
||||
@ -309,15 +310,23 @@ struct PlannerInfo
|
||||
|
||||
/*
|
||||
* The fully-processed targetlist is kept here. It differs from
|
||||
* parse->targetList in that (for INSERT and UPDATE) it's been reordered
|
||||
* to match the target table, and defaults have been filled in. Also,
|
||||
* additional resjunk targets may be present. preprocess_targetlist()
|
||||
* does most of this work, but note that more resjunk targets can get
|
||||
* added during appendrel expansion. (Hence, upper_targets mustn't get
|
||||
* set up till after that.)
|
||||
* parse->targetList in that (for INSERT) it's been reordered to match the
|
||||
* target table, and defaults have been filled in. Also, additional
|
||||
* resjunk targets may be present. preprocess_targetlist() does most of
|
||||
* that work, but note that more resjunk targets can get added during
|
||||
* appendrel expansion. (Hence, upper_targets mustn't get set up till
|
||||
* after that.)
|
||||
*/
|
||||
List *processed_tlist;
|
||||
|
||||
/*
|
||||
* For UPDATE, this list contains the target table's attribute numbers to
|
||||
* which the first N entries of processed_tlist are to be assigned. (Any
|
||||
* additional entries in processed_tlist must be resjunk.) DO NOT use the
|
||||
* resnos in processed_tlist to identify the UPDATE target columns.
|
||||
*/
|
||||
List *update_colnos;
|
||||
|
||||
/* Fields filled during create_plan() for use in setrefs.c */
|
||||
AttrNumber *grouping_map; /* for GroupingFunc fixup */
|
||||
List *minmax_aggs; /* List of MinMaxAggInfos */
|
||||
@ -333,9 +342,6 @@ struct PlannerInfo
|
||||
Index qual_security_level; /* minimum security_level for quals */
|
||||
/* Note: qual_security_level is zero if there are no securityQuals */
|
||||
|
||||
InheritanceKind inhTargetKind; /* indicates if the target relation is an
|
||||
* inheritance child or partition or a
|
||||
* partitioned table */
|
||||
bool hasJoinRTEs; /* true if any RTEs are RTE_JOIN kind */
|
||||
bool hasLateralRTEs; /* true if any RTEs are marked LATERAL */
|
||||
bool hasHavingQual; /* true if havingQual was non-null */
|
||||
@ -1839,20 +1845,20 @@ typedef struct LockRowsPath
|
||||
* ModifyTablePath represents performing INSERT/UPDATE/DELETE modifications
|
||||
*
|
||||
* We represent most things that will be in the ModifyTable plan node
|
||||
* literally, except we have child Path(s) not Plan(s). But analysis of the
|
||||
* literally, except we have a child Path not Plan. But analysis of the
|
||||
* OnConflictExpr is deferred to createplan.c, as is collection of FDW data.
|
||||
*/
|
||||
typedef struct ModifyTablePath
|
||||
{
|
||||
Path path;
|
||||
Path *subpath; /* Path producing source data */
|
||||
CmdType operation; /* INSERT, UPDATE, or DELETE */
|
||||
bool canSetTag; /* do we set the command tag/es_processed? */
|
||||
Index nominalRelation; /* Parent RT index for use of EXPLAIN */
|
||||
Index rootRelation; /* Root RT index, if target is partitioned */
|
||||
bool partColsUpdated; /* some part key in hierarchy updated */
|
||||
bool partColsUpdated; /* some part key in hierarchy updated? */
|
||||
List *resultRelations; /* integer list of RT indexes */
|
||||
List *subpaths; /* Path(s) producing source data */
|
||||
List *subroots; /* per-target-table PlannerInfos */
|
||||
List *updateColnosLists; /* per-target-table update_colnos lists */
|
||||
List *withCheckOptionLists; /* per-target-table WCO lists */
|
||||
List *returningLists; /* per-target-table RETURNING tlists */
|
||||
List *rowMarks; /* PlanRowMarks (non-locking only) */
|
||||
@ -2311,6 +2317,34 @@ typedef struct AppendRelInfo
|
||||
Oid parent_reloid; /* OID of parent relation */
|
||||
} AppendRelInfo;
|
||||
|
||||
/*
|
||||
* Information about a row-identity "resjunk" column in UPDATE/DELETE.
|
||||
*
|
||||
* In partitioned UPDATE/DELETE it's important for child partitions to share
|
||||
* row-identity columns whenever possible, so as not to chew up too many
|
||||
* targetlist columns. We use these structs to track which identity columns
|
||||
* have been requested. In the finished plan, each of these will give rise
|
||||
* to one resjunk entry in the targetlist of the ModifyTable's subplan node.
|
||||
*
|
||||
* All the Vars stored in RowIdentityVarInfos must have varno ROWID_VAR, for
|
||||
* convenience of detecting duplicate requests. We'll replace that, in the
|
||||
* final plan, with the varno of the generating rel.
|
||||
*
|
||||
* Outside this list, a Var with varno ROWID_VAR and varattno k is a reference
|
||||
* to the k-th element of the row_identity_vars list (k counting from 1).
|
||||
* We add such a reference to root->processed_tlist when creating the entry,
|
||||
* and it propagates into the plan tree from there.
|
||||
*/
|
||||
typedef struct RowIdentityVarInfo
|
||||
{
|
||||
NodeTag type;
|
||||
|
||||
Var *rowidvar; /* Var to be evaluated (but varno=ROWID_VAR) */
|
||||
int32 rowidwidth; /* estimated average width */
|
||||
char *rowidname; /* name of the resjunk column */
|
||||
Relids rowidrels; /* RTE indexes of target rels using this */
|
||||
} RowIdentityVarInfo;
|
||||
|
||||
/*
|
||||
* For each distinct placeholder expression generated during planning, we
|
||||
* store a PlaceHolderInfo node in the PlannerInfo node's placeholder_list.
|
||||
|
Reference in New Issue
Block a user