Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * nodeModifyTable.c
4 : : * routines to handle ModifyTable nodes.
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/executor/nodeModifyTable.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : /*
16 : : * INTERFACE ROUTINES
17 : : * ExecInitModifyTable - initialize the ModifyTable node
18 : : * ExecModifyTable - retrieve the next tuple from the node
19 : : * ExecEndModifyTable - shut down the ModifyTable node
20 : : * ExecReScanModifyTable - rescan the ModifyTable node
21 : : *
22 : : * NOTES
23 : : * The ModifyTable node receives input from its outerPlan, which is
24 : : * the data to insert for INSERT cases, the changed columns' new
25 : : * values plus row-locating info for UPDATE and MERGE cases, or just the
26 : : * row-locating info for DELETE cases.
27 : : *
28 : : * The relation to modify can be an ordinary table, a foreign table, or a
29 : : * view. If it's a view, either it has sufficient INSTEAD OF triggers or
30 : : * this node executes only MERGE ... DO NOTHING. If the original MERGE
31 : : * targeted a view not in one of those two categories, earlier processing
32 : : * already pointed the ModifyTable result relation to an underlying
33 : : * relation of that other view. This node does process
34 : : * ri_WithCheckOptions, which may have expressions from those other,
35 : : * automatically updatable views.
36 : : *
37 : : * MERGE runs a join between the source relation and the target table.
38 : : * If any WHEN NOT MATCHED [BY TARGET] clauses are present, then the join
39 : : * is an outer join that might output tuples without a matching target
40 : : * tuple. In this case, any unmatched target tuples will have NULL
41 : : * row-locating info, and only INSERT can be run. But for matched target
42 : : * tuples, the row-locating info is used to determine the tuple to UPDATE
43 : : * or DELETE. When all clauses are WHEN MATCHED or WHEN NOT MATCHED BY
44 : : * SOURCE, all tuples produced by the join will include a matching target
45 : : * tuple, so all tuples contain row-locating info.
46 : : *
47 : : * If the query specifies RETURNING, then the ModifyTable returns a
48 : : * RETURNING tuple after completing each row insert, update, or delete.
49 : : * It must be called again to continue the operation. Without RETURNING,
50 : : * we just loop within the node until all the work is done, then
51 : : * return NULL. This avoids useless call/return overhead.
52 : : */
53 : :
54 : : #include "postgres.h"
55 : :
56 : : #include "access/htup_details.h"
57 : : #include "access/tableam.h"
58 : : #include "access/tupconvert.h"
59 : : #include "access/xact.h"
60 : : #include "commands/trigger.h"
61 : : #include "executor/execPartition.h"
62 : : #include "executor/executor.h"
63 : : #include "executor/instrument.h"
64 : : #include "executor/nodeModifyTable.h"
65 : : #include "foreign/fdwapi.h"
66 : : #include "miscadmin.h"
67 : : #include "nodes/nodeFuncs.h"
68 : : #include "optimizer/optimizer.h"
69 : : #include "pgstat.h"
70 : : #include "rewrite/rewriteHandler.h"
71 : : #include "rewrite/rewriteManip.h"
72 : : #include "storage/lmgr.h"
73 : : #include "utils/builtins.h"
74 : : #include "utils/datum.h"
75 : : #include "utils/injection_point.h"
76 : : #include "utils/rangetypes.h"
77 : : #include "utils/rel.h"
78 : : #include "utils/snapmgr.h"
79 : :
80 : :
81 : : typedef struct MTTargetRelLookup
82 : : {
83 : : Oid relationOid; /* hash key, must be first */
84 : : int relationIndex; /* rel's index in resultRelInfo[] array */
85 : : } MTTargetRelLookup;
86 : :
87 : : /*
88 : : * Context struct for a ModifyTable operation, containing basic execution
89 : : * state and some output variables populated by ExecUpdateAct() and
90 : : * ExecDeleteAct() to report the result of their actions to callers.
91 : : */
92 : : typedef struct ModifyTableContext
93 : : {
94 : : /* Operation state */
95 : : ModifyTableState *mtstate;
96 : : EPQState *epqstate;
97 : : EState *estate;
98 : :
99 : : /*
100 : : * Slot containing tuple obtained from ModifyTable's subplan. Used to
101 : : * access "junk" columns that are not going to be stored.
102 : : */
103 : : TupleTableSlot *planSlot;
104 : :
105 : : /*
106 : : * Information about the changes that were made concurrently to a tuple
107 : : * being updated or deleted
108 : : */
109 : : TM_FailureData tmfd;
110 : :
111 : : /*
112 : : * The tuple deleted when doing a cross-partition UPDATE with a RETURNING
113 : : * clause that refers to OLD columns (converted to the root's tuple
114 : : * descriptor).
115 : : */
116 : : TupleTableSlot *cpDeletedSlot;
117 : :
118 : : /*
119 : : * The tuple projected by the INSERT's RETURNING clause, when doing a
120 : : * cross-partition UPDATE
121 : : */
122 : : TupleTableSlot *cpUpdateReturningSlot;
123 : : } ModifyTableContext;
124 : :
125 : : /*
126 : : * Context struct containing output data specific to UPDATE operations.
127 : : */
128 : : typedef struct UpdateContext
129 : : {
130 : : bool crossPartUpdate; /* was it a cross-partition update? */
131 : : TU_UpdateIndexes updateIndexes; /* Which index updates are required? */
132 : :
133 : : /*
134 : : * Lock mode to acquire on the latest tuple version before performing
135 : : * EvalPlanQual on it
136 : : */
137 : : LockTupleMode lockmode;
138 : : } UpdateContext;
139 : :
140 : :
141 : : static void ExecBatchInsert(ModifyTableState *mtstate,
142 : : ResultRelInfo *resultRelInfo,
143 : : TupleTableSlot **slots,
144 : : TupleTableSlot **planSlots,
145 : : int numSlots,
146 : : EState *estate,
147 : : bool canSetTag);
148 : : static void ExecPendingInserts(EState *estate);
149 : : static void ExecCrossPartitionUpdateForeignKey(ModifyTableContext *context,
150 : : ResultRelInfo *sourcePartInfo,
151 : : ResultRelInfo *destPartInfo,
152 : : ItemPointer tupleid,
153 : : TupleTableSlot *oldslot,
154 : : TupleTableSlot *newslot);
155 : : static bool ExecOnConflictLockRow(ModifyTableContext *context,
156 : : TupleTableSlot *existing,
157 : : ItemPointer conflictTid,
158 : : Relation relation,
159 : : LockTupleMode lockmode,
160 : : bool isUpdate);
161 : : static bool ExecOnConflictUpdate(ModifyTableContext *context,
162 : : ResultRelInfo *resultRelInfo,
163 : : ItemPointer conflictTid,
164 : : TupleTableSlot *excludedSlot,
165 : : bool canSetTag,
166 : : TupleTableSlot **returning);
167 : : static bool ExecOnConflictSelect(ModifyTableContext *context,
168 : : ResultRelInfo *resultRelInfo,
169 : : ItemPointer conflictTid,
170 : : TupleTableSlot *excludedSlot,
171 : : bool canSetTag,
172 : : TupleTableSlot **returning);
173 : : static void ExecForPortionOfLeftovers(ModifyTableContext *context,
174 : : EState *estate,
175 : : ResultRelInfo *resultRelInfo,
176 : : ItemPointer tupleid);
177 : : static TupleTableSlot *ExecPrepareTupleRouting(ModifyTableState *mtstate,
178 : : EState *estate,
179 : : PartitionTupleRouting *proute,
180 : : ResultRelInfo *targetRelInfo,
181 : : TupleTableSlot *slot,
182 : : ResultRelInfo **partRelInfo);
183 : :
184 : : static TupleTableSlot *ExecMerge(ModifyTableContext *context,
185 : : ResultRelInfo *resultRelInfo,
186 : : ItemPointer tupleid,
187 : : HeapTuple oldtuple,
188 : : bool canSetTag);
189 : : static void ExecInitMerge(ModifyTableState *mtstate, EState *estate);
190 : : static TupleTableSlot *ExecMergeMatched(ModifyTableContext *context,
191 : : ResultRelInfo *resultRelInfo,
192 : : ItemPointer tupleid,
193 : : HeapTuple oldtuple,
194 : : bool canSetTag,
195 : : bool *matched);
196 : : static TupleTableSlot *ExecMergeNotMatched(ModifyTableContext *context,
197 : : ResultRelInfo *resultRelInfo,
198 : : bool canSetTag);
199 : : static void ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate);
200 : : static void fireBSTriggers(ModifyTableState *node);
201 : : static void fireASTriggers(ModifyTableState *node);
202 : : static void ExecInitForPortionOf(ModifyTableState *mtstate, EState *estate,
203 : : ResultRelInfo *resultRelInfo);
204 : :
205 : :
206 : : /*
207 : : * Verify that the tuples to be produced by INSERT match the
208 : : * target relation's rowtype
209 : : *
210 : : * We do this to guard against stale plans. If plan invalidation is
211 : : * functioning properly then we should never get a failure here, but better
212 : : * safe than sorry. Note that this is called after we have obtained lock
213 : : * on the target rel, so the rowtype can't change underneath us.
214 : : *
215 : : * The plan output is represented by its targetlist, because that makes
216 : : * handling the dropped-column case easier.
217 : : *
218 : : * We used to use this for UPDATE as well, but now the equivalent checks
219 : : * are done in ExecBuildUpdateProjection.
220 : : */
221 : : static void
6107 tgl@sss.pgh.pa.us 222 :CBC 55011 : ExecCheckPlanOutput(Relation resultRel, List *targetList)
223 : : {
224 : 55011 : TupleDesc resultDesc = RelationGetDescr(resultRel);
225 : 55011 : int attno = 0;
226 : : ListCell *lc;
227 : :
228 [ + + + + : 171443 : foreach(lc, targetList)
+ + ]
229 : : {
230 : 116432 : TargetEntry *tle = (TargetEntry *) lfirst(lc);
231 : : Form_pg_attribute attr;
232 : :
1917 233 [ - + ]: 116432 : Assert(!tle->resjunk); /* caller removed junk items already */
234 : :
6107 235 [ - + ]: 116432 : if (attno >= resultDesc->natts)
6107 tgl@sss.pgh.pa.us 236 [ # # ]:UBC 0 : ereport(ERROR,
237 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
238 : : errmsg("table row type and query-specified row type do not match"),
239 : : errdetail("Query has too many columns.")));
3236 andres@anarazel.de 240 :CBC 116432 : attr = TupleDescAttr(resultDesc, attno);
241 : 116432 : attno++;
242 : :
243 : : /*
244 : : * Special cases here should match planner's expand_insert_targetlist.
245 : : */
441 tgl@sss.pgh.pa.us 246 [ + + ]: 116432 : if (attr->attisdropped)
247 : : {
248 : : /*
249 : : * For a dropped column, we can't check atttypid (it's likely 0).
250 : : * In any case the planner has most likely inserted an INT4 null.
251 : : * What we insist on is just *some* NULL constant.
252 : : */
253 [ + - ]: 443 : if (!IsA(tle->expr, Const) ||
254 [ - + ]: 443 : !((Const *) tle->expr)->constisnull)
6107 tgl@sss.pgh.pa.us 255 [ # # ]:UBC 0 : ereport(ERROR,
256 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
257 : : errmsg("table row type and query-specified row type do not match"),
258 : : errdetail("Query provides a value for a dropped column at ordinal position %d.",
259 : : attno)));
260 : : }
441 tgl@sss.pgh.pa.us 261 [ + + ]:CBC 115989 : else if (attr->attgenerated)
262 : : {
263 : : /*
264 : : * For a generated column, the planner will have inserted a null
265 : : * of the column's base type (to avoid possibly failing on domain
266 : : * not-null constraints). It doesn't seem worth insisting on that
267 : : * exact type though, since a null value is type-independent. As
268 : : * above, just insist on *some* NULL constant.
269 : : */
6107 270 [ + - ]: 896 : if (!IsA(tle->expr, Const) ||
271 [ - + ]: 896 : !((Const *) tle->expr)->constisnull)
6107 tgl@sss.pgh.pa.us 272 [ # # ]:UBC 0 : ereport(ERROR,
273 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
274 : : errmsg("table row type and query-specified row type do not match"),
275 : : errdetail("Query provides a value for a generated column at ordinal position %d.",
276 : : attno)));
277 : : }
278 : : else
279 : : {
280 : : /* Normal case: demand type match */
441 tgl@sss.pgh.pa.us 281 [ - + ]:CBC 115093 : if (exprType((Node *) tle->expr) != attr->atttypid)
441 tgl@sss.pgh.pa.us 282 [ # # ]:UBC 0 : ereport(ERROR,
283 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
284 : : errmsg("table row type and query-specified row type do not match"),
285 : : errdetail("Table has type %s at ordinal position %d, but query expects %s.",
286 : : format_type_be(attr->atttypid),
287 : : attno,
288 : : format_type_be(exprType((Node *) tle->expr)))));
289 : : }
290 : : }
6107 tgl@sss.pgh.pa.us 291 [ - + ]:CBC 55011 : if (attno != resultDesc->natts)
6107 tgl@sss.pgh.pa.us 292 [ # # ]:UBC 0 : ereport(ERROR,
293 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
294 : : errmsg("table row type and query-specified row type do not match"),
295 : : errdetail("Query has too few columns.")));
6107 tgl@sss.pgh.pa.us 296 :CBC 55011 : }
297 : :
298 : : /*
299 : : * ExecProcessReturning --- evaluate a RETURNING list
300 : : *
301 : : * context: context for the ModifyTable operation
302 : : * resultRelInfo: current result rel
303 : : * isDelete: true if the operation/merge action is a DELETE
304 : : * oldSlot: slot holding old tuple deleted or updated
305 : : * newSlot: slot holding new tuple inserted or updated
306 : : * planSlot: slot holding tuple returned by top subplan node
307 : : *
308 : : * Note: If oldSlot and newSlot are NULL, the FDW should have already provided
309 : : * econtext's scan tuple and its old & new tuples are not needed (FDW direct-
310 : : * modify is disabled if the RETURNING list refers to any OLD/NEW values).
311 : : *
312 : : * Note: For the SELECT path of INSERT ... ON CONFLICT DO SELECT, oldSlot and
313 : : * newSlot are both the existing tuple, since it's not changed.
314 : : *
315 : : * Returns a slot holding the result tuple
316 : : */
317 : : static TupleTableSlot *
530 dean.a.rasheed@gmail 318 : 5914 : ExecProcessReturning(ModifyTableContext *context,
319 : : ResultRelInfo *resultRelInfo,
320 : : bool isDelete,
321 : : TupleTableSlot *oldSlot,
322 : : TupleTableSlot *newSlot,
323 : : TupleTableSlot *planSlot)
324 : : {
325 : 5914 : EState *estate = context->estate;
3756 rhaas@postgresql.org 326 : 5914 : ProjectionInfo *projectReturning = resultRelInfo->ri_projectReturning;
6107 tgl@sss.pgh.pa.us 327 : 5914 : ExprContext *econtext = projectReturning->pi_exprContext;
328 : :
329 : : /* Make tuple and any needed join variables available to ExecProject */
138 dean.a.rasheed@gmail 330 [ + + ]:GNC 5914 : if (isDelete)
331 : : {
332 : : /* return old tuple by default */
333 [ + + ]: 881 : if (oldSlot)
334 : 762 : econtext->ecxt_scantuple = oldSlot;
335 : : }
336 : : else
337 : : {
338 : : /* return new tuple by default */
339 [ + + ]: 5033 : if (newSlot)
340 : 4804 : econtext->ecxt_scantuple = newSlot;
341 : : }
6107 tgl@sss.pgh.pa.us 342 :CBC 5914 : econtext->ecxt_outertuple = planSlot;
343 : :
344 : : /* Make old/new tuples available to ExecProject, if required */
530 dean.a.rasheed@gmail 345 [ + + ]: 5914 : if (oldSlot)
346 : 2584 : econtext->ecxt_oldtuple = oldSlot;
347 [ + + ]: 3330 : else if (projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD)
348 : 144 : econtext->ecxt_oldtuple = ExecGetAllNullSlot(estate, resultRelInfo);
349 : : else
350 : 3186 : econtext->ecxt_oldtuple = NULL; /* No references to OLD columns */
351 : :
352 [ + + ]: 5914 : if (newSlot)
353 : 4804 : econtext->ecxt_newtuple = newSlot;
354 [ + + ]: 1110 : else if (projectReturning->pi_state.flags & EEO_FLAG_HAS_NEW)
355 : 100 : econtext->ecxt_newtuple = ExecGetAllNullSlot(estate, resultRelInfo);
356 : : else
357 : 1010 : econtext->ecxt_newtuple = NULL; /* No references to NEW columns */
358 : :
359 : : /*
360 : : * Tell ExecProject whether or not the OLD/NEW rows actually exist. This
361 : : * information is required to evaluate ReturningExpr nodes and also in
362 : : * ExecEvalSysVar() and ExecEvalWholeRowVar().
363 : : */
364 [ + + ]: 5914 : if (oldSlot == NULL)
365 : 3330 : projectReturning->pi_state.flags |= EEO_FLAG_OLD_IS_NULL;
366 : : else
367 : 2584 : projectReturning->pi_state.flags &= ~EEO_FLAG_OLD_IS_NULL;
368 : :
369 [ + + ]: 5914 : if (newSlot == NULL)
370 : 1110 : projectReturning->pi_state.flags |= EEO_FLAG_NEW_IS_NULL;
371 : : else
372 : 4804 : projectReturning->pi_state.flags &= ~EEO_FLAG_NEW_IS_NULL;
373 : :
374 : : /* Compute the RETURNING expressions */
3449 andres@anarazel.de 375 : 5914 : return ExecProject(projectReturning);
376 : : }
377 : :
378 : : /*
379 : : * ExecCheckTupleVisible -- verify tuple is visible
380 : : *
381 : : * It would not be consistent with guarantees of the higher isolation levels to
382 : : * proceed with avoiding insertion (taking speculative insertion's alternative
383 : : * path) on the basis of another tuple that is not visible to MVCC snapshot.
384 : : * Check for the need to raise a serialization failure, and do so as necessary.
385 : : */
386 : : static void
2656 387 : 2956 : ExecCheckTupleVisible(EState *estate,
388 : : Relation rel,
389 : : TupleTableSlot *slot)
390 : : {
4071 391 [ + + ]: 2956 : if (!IsolationUsesXactSnapshot())
392 : 2914 : return;
393 : :
2656 394 [ + + ]: 42 : if (!table_tuple_satisfies_snapshot(rel, slot, estate->es_snapshot))
395 : : {
396 : : Datum xminDatum;
397 : : TransactionId xmin;
398 : : bool isnull;
399 : :
400 : 30 : xminDatum = slot_getsysattr(slot, MinTransactionIdAttributeNumber, &isnull);
401 [ - + ]: 30 : Assert(!isnull);
402 : 30 : xmin = DatumGetTransactionId(xminDatum);
403 : :
404 : : /*
405 : : * We should not raise a serialization failure if the conflict is
406 : : * against a tuple inserted by our own transaction, even if it's not
407 : : * visible to our snapshot. (This would happen, for example, if
408 : : * conflicting keys are proposed for insertion in a single command.)
409 : : */
410 [ + + ]: 30 : if (!TransactionIdIsCurrentTransactionId(xmin))
3537 tgl@sss.pgh.pa.us 411 [ + - ]: 10 : ereport(ERROR,
412 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
413 : : errmsg("could not serialize access due to concurrent update")));
414 : : }
415 : : }
416 : :
417 : : /*
418 : : * ExecCheckTIDVisible -- convenience variant of ExecCheckTupleVisible()
419 : : */
420 : : static void
4071 andres@anarazel.de 421 : 139 : ExecCheckTIDVisible(EState *estate,
422 : : ResultRelInfo *relinfo,
423 : : ItemPointer tid,
424 : : TupleTableSlot *tempSlot)
425 : : {
426 : 139 : Relation rel = relinfo->ri_RelationDesc;
427 : :
428 : : /* Redundantly check isolation level */
429 [ + + ]: 139 : if (!IsolationUsesXactSnapshot())
430 : 105 : return;
431 : :
2595 432 [ - + ]: 34 : if (!table_tuple_fetch_row_version(rel, tid, SnapshotAny, tempSlot))
4071 andres@anarazel.de 433 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch conflicting tuple for ON CONFLICT");
2656 andres@anarazel.de 434 :CBC 34 : ExecCheckTupleVisible(estate, rel, tempSlot);
435 : 24 : ExecClearTuple(tempSlot);
436 : : }
437 : :
438 : : /*
439 : : * Initialize generated columns handling for a tuple
440 : : *
441 : : * This fills the resultRelInfo's ri_GeneratedExprsI/ri_NumGeneratedNeededI or
442 : : * ri_GeneratedExprsU/ri_NumGeneratedNeededU fields, depending on cmdtype.
443 : : * This is used only for stored generated columns.
444 : : *
445 : : * If cmdType == CMD_UPDATE, the ri_extraUpdatedCols field is filled too.
446 : : * This is used by both stored and virtual generated columns.
447 : : *
448 : : * Note: usually, a given query would need only one of ri_GeneratedExprsI and
449 : : * ri_GeneratedExprsU per result rel; but MERGE can need both, and so can
450 : : * cross-partition UPDATEs, since a partition might be the target of both
451 : : * UPDATE and INSERT actions.
452 : : */
453 : : void
508 peter@eisentraut.org 454 : 32066 : ExecInitGenerated(ResultRelInfo *resultRelInfo,
455 : : EState *estate,
456 : : CmdType cmdtype)
457 : : {
2649 458 : 32066 : Relation rel = resultRelInfo->ri_RelationDesc;
459 : 32066 : TupleDesc tupdesc = RelationGetDescr(rel);
460 : 32066 : int natts = tupdesc->natts;
461 : : ExprState **ri_GeneratedExprs;
462 : : int ri_NumGeneratedNeeded;
463 : : Bitmapset *updatedCols;
464 : : MemoryContext oldContext;
465 : :
466 : : /* Nothing to do if no generated columns */
508 467 [ + + + + : 32066 : if (!(tupdesc->constr && (tupdesc->constr->has_generated_stored || tupdesc->constr->has_generated_virtual)))
+ + ]
1272 tgl@sss.pgh.pa.us 468 : 31179 : return;
469 : :
470 : : /*
471 : : * In an UPDATE, we can skip computing any generated columns that do not
472 : : * depend on any UPDATE target column. But if there is a BEFORE ROW
473 : : * UPDATE trigger, we cannot skip because the trigger might change more
474 : : * columns.
475 : : */
476 [ + + ]: 887 : if (cmdtype == CMD_UPDATE &&
477 [ + + - + ]: 211 : !(rel->trigdesc && rel->trigdesc->trig_update_before_row))
478 : 183 : updatedCols = ExecGetUpdatedCols(resultRelInfo, estate);
479 : : else
480 : 704 : updatedCols = NULL;
481 : :
482 : : /*
483 : : * Make sure these data structures are built in the per-query memory
484 : : * context so they'll survive throughout the query.
485 : : */
486 : 887 : oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
487 : :
1212 488 : 887 : ri_GeneratedExprs = (ExprState **) palloc0(natts * sizeof(ExprState *));
489 : 887 : ri_NumGeneratedNeeded = 0;
490 : :
1272 491 [ + + ]: 3774 : for (int i = 0; i < natts; i++)
492 : : {
508 peter@eisentraut.org 493 : 2891 : char attgenerated = TupleDescAttr(tupdesc, i)->attgenerated;
494 : :
495 [ + + ]: 2891 : if (attgenerated)
496 : : {
497 : : Expr *expr;
498 : :
499 : : /* Fetch the GENERATED AS expression tree */
1272 tgl@sss.pgh.pa.us 500 : 958 : expr = (Expr *) build_column_default(rel, i + 1);
501 [ - + ]: 958 : if (expr == NULL)
1272 tgl@sss.pgh.pa.us 502 [ # # ]:UBC 0 : elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
503 : : i + 1, RelationGetRelationName(rel));
504 : :
505 : : /*
506 : : * If it's an update with a known set of update target columns,
507 : : * see if we can skip the computation.
508 : : */
1272 tgl@sss.pgh.pa.us 509 [ + + ]:CBC 958 : if (updatedCols)
510 : : {
511 : 199 : Bitmapset *attrs_used = NULL;
512 : :
513 : 199 : pull_varattnos((Node *) expr, 1, &attrs_used);
514 : :
515 [ + + ]: 199 : if (!bms_overlap(updatedCols, attrs_used))
516 : 15 : continue; /* need not update this column */
517 : : }
518 : :
519 : : /* No luck, so prepare the expression for execution */
508 peter@eisentraut.org 520 [ + + ]: 943 : if (attgenerated == ATTRIBUTE_GENERATED_STORED)
521 : : {
522 : 851 : ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
523 : 847 : ri_NumGeneratedNeeded++;
524 : : }
525 : :
526 : : /* If UPDATE, mark column in resultRelInfo->ri_extraUpdatedCols */
1212 tgl@sss.pgh.pa.us 527 [ + + ]: 939 : if (cmdtype == CMD_UPDATE)
528 : 216 : resultRelInfo->ri_extraUpdatedCols =
529 : 216 : bms_add_member(resultRelInfo->ri_extraUpdatedCols,
530 : : i + 1 - FirstLowInvalidHeapAttributeNumber);
531 : : }
532 : : }
533 : :
508 peter@eisentraut.org 534 [ + + ]: 883 : if (ri_NumGeneratedNeeded == 0)
535 : : {
536 : : /* didn't need it after all */
537 : 47 : pfree(ri_GeneratedExprs);
538 : 47 : ri_GeneratedExprs = NULL;
539 : : }
540 : :
541 : : /* Save in appropriate set of fields */
1212 tgl@sss.pgh.pa.us 542 [ + + ]: 883 : if (cmdtype == CMD_UPDATE)
543 : : {
544 : : /* Don't call twice */
545 [ - + ]: 211 : Assert(resultRelInfo->ri_GeneratedExprsU == NULL);
546 : :
547 : 211 : resultRelInfo->ri_GeneratedExprsU = ri_GeneratedExprs;
548 : 211 : resultRelInfo->ri_NumGeneratedNeededU = ri_NumGeneratedNeeded;
549 : :
508 peter@eisentraut.org 550 : 211 : resultRelInfo->ri_extraUpdatedCols_valid = true;
551 : : }
552 : : else
553 : : {
554 : : /* Don't call twice */
1212 tgl@sss.pgh.pa.us 555 [ - + ]: 672 : Assert(resultRelInfo->ri_GeneratedExprsI == NULL);
556 : :
557 : 672 : resultRelInfo->ri_GeneratedExprsI = ri_GeneratedExprs;
558 : 672 : resultRelInfo->ri_NumGeneratedNeededI = ri_NumGeneratedNeeded;
559 : : }
560 : :
1272 561 : 883 : MemoryContextSwitchTo(oldContext);
562 : : }
563 : :
564 : : /*
565 : : * Compute stored generated columns for a tuple
566 : : */
567 : : void
568 : 1209 : ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo,
569 : : EState *estate, TupleTableSlot *slot,
570 : : CmdType cmdtype)
571 : : {
572 : 1209 : Relation rel = resultRelInfo->ri_RelationDesc;
573 : 1209 : TupleDesc tupdesc = RelationGetDescr(rel);
574 : 1209 : int natts = tupdesc->natts;
575 [ + + ]: 1209 : ExprContext *econtext = GetPerTupleExprContext(estate);
576 : : ExprState **ri_GeneratedExprs;
577 : : MemoryContext oldContext;
578 : : Datum *values;
579 : : bool *nulls;
580 : :
581 : : /* We should not be called unless this is true */
582 [ + - - + ]: 1209 : Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
583 : :
584 : : /*
585 : : * Initialize the expressions if we didn't already, and check whether we
586 : : * can exit early because nothing needs to be computed.
587 : : */
1212 588 [ + + ]: 1209 : if (cmdtype == CMD_UPDATE)
589 : : {
590 [ + + ]: 200 : if (resultRelInfo->ri_GeneratedExprsU == NULL)
508 peter@eisentraut.org 591 : 159 : ExecInitGenerated(resultRelInfo, estate, cmdtype);
1212 tgl@sss.pgh.pa.us 592 [ + + ]: 200 : if (resultRelInfo->ri_NumGeneratedNeededU == 0)
593 : 11 : return;
594 : 189 : ri_GeneratedExprs = resultRelInfo->ri_GeneratedExprsU;
595 : : }
596 : : else
597 : : {
598 [ + + ]: 1009 : if (resultRelInfo->ri_GeneratedExprsI == NULL)
508 peter@eisentraut.org 599 : 676 : ExecInitGenerated(resultRelInfo, estate, cmdtype);
600 : : /* Early exit is impossible given the prior Assert */
1212 tgl@sss.pgh.pa.us 601 [ - + ]: 1005 : Assert(resultRelInfo->ri_NumGeneratedNeededI > 0);
602 : 1005 : ri_GeneratedExprs = resultRelInfo->ri_GeneratedExprsI;
603 : : }
604 : :
2649 peter@eisentraut.org 605 [ + - ]: 1194 : oldContext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
606 : :
202 michael@paquier.xyz 607 :GNC 1194 : values = palloc_array(Datum, natts);
608 : 1194 : nulls = palloc_array(bool, natts);
609 : :
2603 peter@eisentraut.org 610 :CBC 1194 : slot_getallattrs(slot);
611 : 1194 : memcpy(nulls, slot->tts_isnull, sizeof(*nulls) * natts);
612 : :
2649 613 [ + + ]: 5097 : for (int i = 0; i < natts; i++)
614 : : {
557 drowley@postgresql.o 615 : 3919 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
616 : :
1212 tgl@sss.pgh.pa.us 617 [ + + ]: 3919 : if (ri_GeneratedExprs[i])
618 : : {
619 : : Datum val;
620 : : bool isnull;
621 : :
557 drowley@postgresql.o 622 [ - + ]: 1207 : Assert(TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED);
623 : :
2649 peter@eisentraut.org 624 : 1207 : econtext->ecxt_scantuple = slot;
625 : :
1212 tgl@sss.pgh.pa.us 626 : 1207 : val = ExecEvalExpr(ri_GeneratedExprs[i], econtext, &isnull);
627 : :
628 : : /*
629 : : * We must make a copy of val as we have no guarantees about where
630 : : * memory for a pass-by-reference Datum is located.
631 : : */
2264 drowley@postgresql.o 632 [ + + ]: 1191 : if (!isnull)
633 : 1135 : val = datumCopy(val, attr->attbyval, attr->attlen);
634 : :
2649 peter@eisentraut.org 635 : 1191 : values[i] = val;
636 : 1191 : nulls[i] = isnull;
637 : : }
638 : : else
639 : : {
2603 640 [ + + ]: 2712 : if (!nulls[i])
641 : 2394 : values[i] = datumCopy(slot->tts_values[i], attr->attbyval, attr->attlen);
642 : : }
643 : : }
644 : :
645 : 1178 : ExecClearTuple(slot);
646 : 1178 : memcpy(slot->tts_values, values, sizeof(*values) * natts);
647 : 1178 : memcpy(slot->tts_isnull, nulls, sizeof(*nulls) * natts);
648 : 1178 : ExecStoreVirtualTuple(slot);
649 : 1178 : ExecMaterializeSlot(slot);
650 : :
2649 651 : 1178 : MemoryContextSwitchTo(oldContext);
652 : : }
653 : :
654 : : /*
655 : : * ExecInitInsertProjection
656 : : * Do one-time initialization of projection data for INSERT tuples.
657 : : *
658 : : * INSERT queries may need a projection to filter out junk attrs in the tlist.
659 : : *
660 : : * This is also a convenient place to verify that the
661 : : * output of an INSERT matches the target table.
662 : : */
663 : : static void
1911 tgl@sss.pgh.pa.us 664 : 54307 : ExecInitInsertProjection(ModifyTableState *mtstate,
665 : : ResultRelInfo *resultRelInfo)
666 : : {
667 : 54307 : ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
668 : 54307 : Plan *subplan = outerPlan(node);
669 : 54307 : EState *estate = mtstate->ps.state;
670 : 54307 : List *insertTargetList = NIL;
671 : 54307 : bool need_projection = false;
672 : : ListCell *l;
673 : :
674 : : /* Extract non-junk columns of the subplan's result tlist. */
675 [ + + + + : 168906 : foreach(l, subplan->targetlist)
+ + ]
676 : : {
677 : 114599 : TargetEntry *tle = (TargetEntry *) lfirst(l);
678 : :
679 [ + - ]: 114599 : if (!tle->resjunk)
680 : 114599 : insertTargetList = lappend(insertTargetList, tle);
681 : : else
1911 tgl@sss.pgh.pa.us 682 :UBC 0 : need_projection = true;
683 : : }
684 : :
685 : : /*
686 : : * The junk-free list must produce a tuple suitable for the result
687 : : * relation.
688 : : */
1911 tgl@sss.pgh.pa.us 689 :CBC 54307 : ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc, insertTargetList);
690 : :
691 : : /* We'll need a slot matching the table's format. */
692 : 54307 : resultRelInfo->ri_newTupleSlot =
693 : 54307 : table_slot_create(resultRelInfo->ri_RelationDesc,
694 : : &estate->es_tupleTable);
695 : :
696 : : /* Build ProjectionInfo if needed (it probably isn't). */
697 [ - + ]: 54307 : if (need_projection)
698 : : {
1911 tgl@sss.pgh.pa.us 699 :UBC 0 : TupleDesc relDesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
700 : :
701 : : /* need an expression context to do the projection */
702 [ # # ]: 0 : if (mtstate->ps.ps_ExprContext == NULL)
703 : 0 : ExecAssignExprContext(estate, &mtstate->ps);
704 : :
705 : 0 : resultRelInfo->ri_projectNew =
706 : 0 : ExecBuildProjectionInfo(insertTargetList,
707 : : mtstate->ps.ps_ExprContext,
708 : : resultRelInfo->ri_newTupleSlot,
709 : : &mtstate->ps,
710 : : relDesc);
711 : : }
712 : :
1911 tgl@sss.pgh.pa.us 713 :CBC 54307 : resultRelInfo->ri_projectNewInfoValid = true;
714 : 54307 : }
715 : :
716 : : /*
717 : : * ExecInitUpdateProjection
718 : : * Do one-time initialization of projection data for UPDATE tuples.
719 : : *
720 : : * UPDATE always needs a projection, because (1) there's always some junk
721 : : * attrs, and (2) we may need to merge values of not-updated columns from
722 : : * the old tuple into the final tuple. In UPDATE, the tuple arriving from
723 : : * the subplan contains only new values for the changed columns, plus row
724 : : * identity info in the junk attrs.
725 : : *
726 : : * This is "one-time" for any given result rel, but we might touch more than
727 : : * one result rel in the course of an inherited UPDATE, and each one needs
728 : : * its own projection due to possible column order variation.
729 : : *
730 : : * This is also a convenient place to verify that the output of an UPDATE
731 : : * matches the target table (ExecBuildUpdateProjection does that).
732 : : */
733 : : static void
734 : 8735 : ExecInitUpdateProjection(ModifyTableState *mtstate,
735 : : ResultRelInfo *resultRelInfo)
736 : : {
737 : 8735 : ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
738 : 8735 : Plan *subplan = outerPlan(node);
739 : 8735 : EState *estate = mtstate->ps.state;
740 : 8735 : TupleDesc relDesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
741 : : int whichrel;
742 : : List *updateColnos;
743 : :
744 : : /*
745 : : * Usually, mt_lastResultIndex matches the target rel. If it happens not
746 : : * to, we can get the index the hard way with an integer division.
747 : : */
748 : 8735 : whichrel = mtstate->mt_lastResultIndex;
749 [ - + ]: 8735 : if (resultRelInfo != mtstate->resultRelInfo + whichrel)
750 : : {
1911 tgl@sss.pgh.pa.us 751 :UBC 0 : whichrel = resultRelInfo - mtstate->resultRelInfo;
752 [ # # # # ]: 0 : Assert(whichrel >= 0 && whichrel < mtstate->mt_nrels);
753 : : }
754 : :
508 amitlan@postgresql.o 755 :CBC 8735 : updateColnos = (List *) list_nth(mtstate->mt_updateColnosLists, whichrel);
756 : :
757 : : /*
758 : : * For UPDATE, we use the old tuple to fill up missing values in the tuple
759 : : * produced by the subplan to get the new tuple. We need two slots, both
760 : : * matching the table's desired format.
761 : : */
1911 tgl@sss.pgh.pa.us 762 : 8735 : resultRelInfo->ri_oldTupleSlot =
763 : 8735 : table_slot_create(resultRelInfo->ri_RelationDesc,
764 : : &estate->es_tupleTable);
765 : 8735 : resultRelInfo->ri_newTupleSlot =
766 : 8735 : table_slot_create(resultRelInfo->ri_RelationDesc,
767 : : &estate->es_tupleTable);
768 : :
769 : : /* need an expression context to do the projection */
770 [ + + ]: 8735 : if (mtstate->ps.ps_ExprContext == NULL)
771 : 7419 : ExecAssignExprContext(estate, &mtstate->ps);
772 : :
773 : 8735 : resultRelInfo->ri_projectNew =
774 : 8735 : ExecBuildUpdateProjection(subplan->targetlist,
775 : : false, /* subplan did the evaluation */
776 : : updateColnos,
777 : : relDesc,
778 : : mtstate->ps.ps_ExprContext,
779 : : resultRelInfo->ri_newTupleSlot,
780 : : &mtstate->ps);
781 : :
782 : 8735 : resultRelInfo->ri_projectNewInfoValid = true;
783 : 8735 : }
784 : :
785 : : /*
786 : : * ExecGetInsertNewTuple
787 : : * This prepares a "new" tuple ready to be inserted into given result
788 : : * relation, by removing any junk columns of the plan's output tuple
789 : : * and (if necessary) coercing the tuple to the right tuple format.
790 : : */
791 : : static TupleTableSlot *
1917 792 : 7934548 : ExecGetInsertNewTuple(ResultRelInfo *relinfo,
793 : : TupleTableSlot *planSlot)
794 : : {
795 : 7934548 : ProjectionInfo *newProj = relinfo->ri_projectNew;
796 : : ExprContext *econtext;
797 : :
798 : : /*
799 : : * If there's no projection to be done, just make sure the slot is of the
800 : : * right type for the target rel. If the planSlot is the right type we
801 : : * can use it as-is, else copy the data into ri_newTupleSlot.
802 : : */
803 [ + - ]: 7934548 : if (newProj == NULL)
804 : : {
805 [ + + ]: 7934548 : if (relinfo->ri_newTupleSlot->tts_ops != planSlot->tts_ops)
806 : : {
807 : 7421442 : ExecCopySlot(relinfo->ri_newTupleSlot, planSlot);
808 : 7421442 : return relinfo->ri_newTupleSlot;
809 : : }
810 : : else
811 : 513106 : return planSlot;
812 : : }
813 : :
814 : : /*
815 : : * Else project; since the projection output slot is ri_newTupleSlot, this
816 : : * will also fix any slot-type problem.
817 : : *
818 : : * Note: currently, this is dead code, because INSERT cases don't receive
819 : : * any junk columns so there's never a projection to be done.
820 : : */
1917 tgl@sss.pgh.pa.us 821 :UBC 0 : econtext = newProj->pi_exprContext;
822 : 0 : econtext->ecxt_outertuple = planSlot;
823 : 0 : return ExecProject(newProj);
824 : : }
825 : :
826 : : /*
827 : : * ExecGetUpdateNewTuple
828 : : * This prepares a "new" tuple by combining an UPDATE subplan's output
829 : : * tuple (which contains values of changed columns) with unchanged
830 : : * columns taken from the old tuple.
831 : : *
832 : : * The subplan tuple might also contain junk columns, which are ignored.
833 : : * Note that the projection also ensures we have a slot of the right type.
834 : : */
835 : : TupleTableSlot *
1917 tgl@sss.pgh.pa.us 836 :CBC 2209392 : ExecGetUpdateNewTuple(ResultRelInfo *relinfo,
837 : : TupleTableSlot *planSlot,
838 : : TupleTableSlot *oldSlot)
839 : : {
1205 dean.a.rasheed@gmail 840 : 2209392 : ProjectionInfo *newProj = relinfo->ri_projectNew;
841 : : ExprContext *econtext;
842 : :
843 : : /* Use a few extra Asserts to protect against outside callers */
1911 tgl@sss.pgh.pa.us 844 [ - + ]: 2209392 : Assert(relinfo->ri_projectNewInfoValid);
1917 845 [ + - - + ]: 2209392 : Assert(planSlot != NULL && !TTS_EMPTY(planSlot));
846 [ + - - + ]: 2209392 : Assert(oldSlot != NULL && !TTS_EMPTY(oldSlot));
847 : :
848 : 2209392 : econtext = newProj->pi_exprContext;
849 : 2209392 : econtext->ecxt_outertuple = planSlot;
850 : 2209392 : econtext->ecxt_scantuple = oldSlot;
851 : 2209392 : return ExecProject(newProj);
852 : : }
853 : :
854 : : /* ----------------------------------------------------------------
855 : : * ExecInsert
856 : : *
857 : : * For INSERT, we have to insert the tuple into the target relation
858 : : * (or partition thereof) and insert appropriate tuples into the index
859 : : * relations.
860 : : *
861 : : * slot contains the new tuple value to be stored.
862 : : *
863 : : * Returns RETURNING result if any, otherwise NULL.
864 : : * *inserted_tuple is the tuple that's effectively inserted;
865 : : * *insert_destrel is the relation where it was inserted.
866 : : * These are only set on success.
867 : : *
868 : : * This may change the currently active tuple conversion map in
869 : : * mtstate->mt_transition_capture, so the callers must take care to
870 : : * save the previous value to avoid losing track of it.
871 : : * ----------------------------------------------------------------
872 : : */
873 : : static TupleTableSlot *
1566 alvherre@alvh.no-ip. 874 : 7937565 : ExecInsert(ModifyTableContext *context,
875 : : ResultRelInfo *resultRelInfo,
876 : : TupleTableSlot *slot,
877 : : bool canSetTag,
878 : : TupleTableSlot **inserted_tuple,
879 : : ResultRelInfo **insert_destrel)
880 : : {
881 : 7937565 : ModifyTableState *mtstate = context->mtstate;
882 : 7937565 : EState *estate = context->estate;
883 : : Relation resultRelationDesc;
6107 tgl@sss.pgh.pa.us 884 : 7937565 : List *recheckIndexes = NIL;
1566 alvherre@alvh.no-ip. 885 : 7937565 : TupleTableSlot *planSlot = context->planSlot;
3368 rhaas@postgresql.org 886 : 7937565 : TupleTableSlot *result = NULL;
887 : : TransitionCaptureState *ar_insert_trig_tcs;
3025 alvherre@alvh.no-ip. 888 : 7937565 : ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
889 : 7937565 : OnConflictAction onconflict = node->onConflictAction;
2085 heikki.linnakangas@i 890 : 7937565 : PartitionTupleRouting *proute = mtstate->mt_partition_tuple_routing;
891 : : MemoryContext oldContext;
892 : :
893 : : /*
894 : : * If the input result relation is a partitioned table, find the leaf
895 : : * partition to insert the tuple into.
896 : : */
897 [ + + ]: 7937565 : if (proute)
898 : : {
899 : : ResultRelInfo *partRelInfo;
900 : :
901 : 481061 : slot = ExecPrepareTupleRouting(mtstate, estate, proute,
902 : : resultRelInfo, slot,
903 : : &partRelInfo);
904 : 480917 : resultRelInfo = partRelInfo;
905 : : }
906 : :
907 : 7937421 : ExecMaterializeSlot(slot);
908 : :
6107 tgl@sss.pgh.pa.us 909 : 7937421 : resultRelationDesc = resultRelInfo->ri_RelationDesc;
910 : :
911 : : /*
912 : : * Open the table's indexes, if we have not done so already, so that we
913 : : * can add new index entries for the inserted tuple.
914 : : */
1911 915 [ + + ]: 7937421 : if (resultRelationDesc->rd_rel->relhasindex &&
916 [ + + ]: 2418767 : resultRelInfo->ri_IndexRelationDescs == NULL)
917 : 21986 : ExecOpenIndices(resultRelInfo, onconflict != ONCONFLICT_NONE);
918 : :
919 : : /*
920 : : * BEFORE ROW INSERT Triggers.
921 : : *
922 : : * Note: We fire BEFORE ROW TRIGGERS for every attempted insertion in an
923 : : * INSERT ... ON CONFLICT statement. We cannot check for constraint
924 : : * violations before firing these triggers, because they can change the
925 : : * values to insert. Also, they can run arbitrary user-defined code with
926 : : * side-effects that we can't cancel by just not inserting the tuple.
927 : : */
6107 928 [ + + ]: 7937421 : if (resultRelInfo->ri_TrigDesc &&
5742 929 [ + + ]: 454118 : resultRelInfo->ri_TrigDesc->trig_insert_before_row)
930 : : {
931 : : /* Flush any pending inserts, so rows are visible to the triggers */
1313 efujita@postgresql.o 932 [ + + ]: 1428 : if (estate->es_insert_pending_result_relations != NIL)
933 : 3 : ExecPendingInserts(estate);
934 : :
2681 andres@anarazel.de 935 [ + + ]: 1428 : if (!ExecBRInsertTriggers(estate, resultRelInfo, slot))
936 : 131 : return NULL; /* "do nothing" */
937 : : }
938 : :
939 : : /* INSTEAD OF ROW INSERT Triggers */
5742 tgl@sss.pgh.pa.us 940 [ + + ]: 7937228 : if (resultRelInfo->ri_TrigDesc &&
941 [ + + ]: 453925 : resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
942 : : {
2681 andres@anarazel.de 943 [ + + ]: 111 : if (!ExecIRInsertTriggers(estate, resultRelInfo, slot))
944 : 4 : return NULL; /* "do nothing" */
945 : : }
4860 tgl@sss.pgh.pa.us 946 [ + + ]: 7937117 : else if (resultRelInfo->ri_FdwRoutine)
947 : : {
948 : : /*
949 : : * GENERATED expressions might reference the tableoid column, so
950 : : * (re-)initialize tts_tableOid before evaluating them.
951 : : */
1866 952 : 1010 : slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
953 : :
954 : : /*
955 : : * Compute stored generated columns
956 : : */
2649 peter@eisentraut.org 957 [ + + ]: 1010 : if (resultRelationDesc->rd_att->constr &&
958 [ + + ]: 179 : resultRelationDesc->rd_att->constr->has_generated_stored)
2085 heikki.linnakangas@i 959 : 4 : ExecComputeStoredGenerated(resultRelInfo, estate, slot,
960 : : CMD_INSERT);
961 : :
962 : : /*
963 : : * If the FDW supports batching, and batching is requested, accumulate
964 : : * rows and insert them in batches. Otherwise use the per-row inserts.
965 : : */
1987 tomas.vondra@postgre 966 [ + + ]: 1010 : if (resultRelInfo->ri_BatchSize > 1)
967 : : {
1313 efujita@postgresql.o 968 : 145 : bool flushed = false;
969 : :
970 : : /*
971 : : * When we've reached the desired batch size, perform the
972 : : * insertion.
973 : : */
1987 tomas.vondra@postgre 974 [ + + ]: 145 : if (resultRelInfo->ri_NumSlots == resultRelInfo->ri_BatchSize)
975 : : {
976 : 10 : ExecBatchInsert(mtstate, resultRelInfo,
977 : : resultRelInfo->ri_Slots,
978 : : resultRelInfo->ri_PlanSlots,
979 : : resultRelInfo->ri_NumSlots,
980 : : estate, canSetTag);
1313 efujita@postgresql.o 981 : 10 : flushed = true;
982 : : }
983 : :
1987 tomas.vondra@postgre 984 : 145 : oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
985 : :
986 [ + + ]: 145 : if (resultRelInfo->ri_Slots == NULL)
987 : : {
202 michael@paquier.xyz 988 :GNC 15 : resultRelInfo->ri_Slots = palloc_array(TupleTableSlot *, resultRelInfo->ri_BatchSize);
989 : 15 : resultRelInfo->ri_PlanSlots = palloc_array(TupleTableSlot *, resultRelInfo->ri_BatchSize);
990 : : }
991 : :
992 : : /*
993 : : * Initialize the batch slots. We don't know how many slots will
994 : : * be needed, so we initialize them as the batch grows, and we
995 : : * keep them across batches. To mitigate an inefficiency in how
996 : : * resource owner handles objects with many references (as with
997 : : * many slots all referencing the same tuple descriptor) we copy
998 : : * the appropriate tuple descriptor for each slot.
999 : : */
1845 tomas.vondra@postgre 1000 [ + + ]:CBC 145 : if (resultRelInfo->ri_NumSlots >= resultRelInfo->ri_NumSlotsInitialized)
1001 : : {
1828 andrew@dunslane.net 1002 : 72 : TupleDesc tdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor);
1003 : : TupleDesc plan_tdesc =
1138 tgl@sss.pgh.pa.us 1004 : 72 : CreateTupleDescCopy(planSlot->tts_tupleDescriptor);
1005 : :
1845 tomas.vondra@postgre 1006 : 144 : resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots] =
1007 : 72 : MakeSingleTupleTableSlot(tdesc, slot->tts_ops);
1008 : :
1009 : 144 : resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots] =
1783 1010 : 72 : MakeSingleTupleTableSlot(plan_tdesc, planSlot->tts_ops);
1011 : :
1012 : : /* remember how many batch slots we initialized */
1845 1013 : 72 : resultRelInfo->ri_NumSlotsInitialized++;
1014 : : }
1015 : :
1840 1016 : 145 : ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots],
1017 : : slot);
1018 : :
1019 : 145 : ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots],
1020 : : planSlot);
1021 : :
1022 : : /*
1023 : : * If these are the first tuples stored in the buffers, add the
1024 : : * target rel and the mtstate to the
1025 : : * es_insert_pending_result_relations and
1026 : : * es_insert_pending_modifytables lists respectively, except in
1027 : : * the case where flushing was done above, in which case they
1028 : : * would already have been added to the lists, so no need to do
1029 : : * this.
1030 : : */
1313 efujita@postgresql.o 1031 [ + + + + ]: 145 : if (resultRelInfo->ri_NumSlots == 0 && !flushed)
1032 : : {
1033 [ - + ]: 19 : Assert(!list_member_ptr(estate->es_insert_pending_result_relations,
1034 : : resultRelInfo));
1035 : 19 : estate->es_insert_pending_result_relations =
1036 : 19 : lappend(estate->es_insert_pending_result_relations,
1037 : : resultRelInfo);
1300 1038 : 19 : estate->es_insert_pending_modifytables =
1039 : 19 : lappend(estate->es_insert_pending_modifytables, mtstate);
1040 : : }
1313 1041 [ - + ]: 145 : Assert(list_member_ptr(estate->es_insert_pending_result_relations,
1042 : : resultRelInfo));
1043 : :
1987 tomas.vondra@postgre 1044 : 145 : resultRelInfo->ri_NumSlots++;
1045 : :
1046 : 145 : MemoryContextSwitchTo(oldContext);
1047 : :
1048 : 145 : return NULL;
1049 : : }
1050 : :
1051 : : /*
1052 : : * insert into foreign table: let the FDW do it
1053 : : */
4860 tgl@sss.pgh.pa.us 1054 : 865 : slot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
1055 : : resultRelInfo,
1056 : : slot,
1057 : : planSlot);
1058 : :
1059 [ + + ]: 862 : if (slot == NULL) /* "do nothing" */
1060 : 2 : return NULL;
1061 : :
1062 : : /*
1063 : : * AFTER ROW Triggers or RETURNING expressions might reference the
1064 : : * tableoid column, so (re-)initialize tts_tableOid before evaluating
1065 : : * them. (This covers the case where the FDW replaced the slot.)
1066 : : */
2681 andres@anarazel.de 1067 : 860 : slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
1068 : : }
1069 : : else
1070 : : {
1071 : : WCOKind wco_kind;
1072 : :
1073 : : /*
1074 : : * Constraints and GENERATED expressions might reference the tableoid
1075 : : * column, so (re-)initialize tts_tableOid before evaluating them.
1076 : : */
1077 : 7936107 : slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
1078 : :
1079 : : /*
1080 : : * Compute stored generated columns
1081 : : */
2649 peter@eisentraut.org 1082 [ + + ]: 7936107 : if (resultRelationDesc->rd_att->constr &&
1083 [ + + ]: 2425387 : resultRelationDesc->rd_att->constr->has_generated_stored)
2085 heikki.linnakangas@i 1084 : 980 : ExecComputeStoredGenerated(resultRelInfo, estate, slot,
1085 : : CMD_INSERT);
1086 : :
1087 : : /*
1088 : : * Check any RLS WITH CHECK policies.
1089 : : *
1090 : : * Normally we should check INSERT policies. But if the insert is the
1091 : : * result of a partition key update that moved the tuple to a new
1092 : : * partition, we should instead check UPDATE policies, because we are
1093 : : * executing policies defined on the target table, and not those
1094 : : * defined on the child partitions.
1095 : : *
1096 : : * If we're running MERGE, we refer to the action that we're executing
1097 : : * to know if we're doing an INSERT or UPDATE to a partition table.
1098 : : */
1555 alvherre@alvh.no-ip. 1099 [ + + ]: 7936087 : if (mtstate->operation == CMD_UPDATE)
1100 : 549 : wco_kind = WCO_RLS_UPDATE_CHECK;
1101 [ + + ]: 7935538 : else if (mtstate->operation == CMD_MERGE)
835 dean.a.rasheed@gmail 1102 : 1181 : wco_kind = (mtstate->mt_merge_action->mas_action->commandType == CMD_UPDATE) ?
1555 alvherre@alvh.no-ip. 1103 [ + + ]: 1181 : WCO_RLS_UPDATE_CHECK : WCO_RLS_INSERT_CHECK;
1104 : : else
1105 : 7934357 : wco_kind = WCO_RLS_INSERT_CHECK;
1106 : :
1107 : : /*
1108 : : * ExecWithCheckOptions() will skip any WCOs which are not of the kind
1109 : : * we are looking for at this point.
1110 : : */
4085 sfrost@snowman.net 1111 [ + + ]: 7936087 : if (resultRelInfo->ri_WithCheckOptions != NIL)
3084 rhaas@postgresql.org 1112 : 474 : ExecWithCheckOptions(wco_kind, resultRelInfo, slot, estate);
1113 : :
1114 : : /*
1115 : : * Check the constraints of the tuple.
1116 : : */
2941 alvherre@alvh.no-ip. 1117 [ + + ]: 7935959 : if (resultRelationDesc->rd_att->constr)
1118 : 2425295 : ExecConstraints(resultRelInfo, slot, estate);
1119 : :
1120 : : /*
1121 : : * Also check the tuple against the partition constraint, if there is
1122 : : * one; except that if we got here via tuple-routing, we don't need to
1123 : : * if there's no BR trigger defined on the partition.
1124 : : */
2113 tgl@sss.pgh.pa.us 1125 [ + + ]: 7935442 : if (resultRelationDesc->rd_rel->relispartition &&
1968 heikki.linnakangas@i 1126 [ + + ]: 482415 : (resultRelInfo->ri_RootResultRelInfo == NULL ||
2941 alvherre@alvh.no-ip. 1127 [ + + ]: 480557 : (resultRelInfo->ri_TrigDesc &&
1128 [ + + ]: 1113 : resultRelInfo->ri_TrigDesc->trig_insert_before_row)))
1129 : 1996 : ExecPartitionCheck(resultRelInfo, slot, estate, true);
1130 : :
4071 andres@anarazel.de 1131 [ + + + - ]: 7935330 : if (onconflict != ONCONFLICT_NONE && resultRelInfo->ri_NumIndices > 0)
1132 : 2216 : {
1133 : : /* Perform a speculative insertion. */
1134 : : uint32 specToken;
1135 : : ItemPointerData conflictTid;
1136 : : ItemPointerData invalidItemPtr;
1137 : : bool specConflict;
1138 : : List *arbiterIndexes;
1139 : :
679 akapila@postgresql.o 1140 : 5313 : ItemPointerSetInvalid(&invalidItemPtr);
3018 alvherre@alvh.no-ip. 1141 : 5313 : arbiterIndexes = resultRelInfo->ri_onConflictArbiterIndexes;
1142 : :
1143 : : /*
1144 : : * Do a non-conclusive check for conflicts first.
1145 : : *
1146 : : * We're not holding any locks yet, so this doesn't guarantee that
1147 : : * the later insert won't conflict. But it avoids leaving behind
1148 : : * a lot of canceled speculative insertions, if you run a lot of
1149 : : * INSERT ON CONFLICT statements that do conflict.
1150 : : *
1151 : : * We loop back here if we find a conflict below, either during
1152 : : * the pre-check, or when we re-check after inserting the tuple
1153 : : * speculatively. Better allow interrupts in case some bug makes
1154 : : * this an infinite loop.
1155 : : */
4071 andres@anarazel.de 1156 : 14 : vlock:
1426 tgl@sss.pgh.pa.us 1157 [ - + ]: 5327 : CHECK_FOR_INTERRUPTS();
4071 andres@anarazel.de 1158 : 5327 : specConflict = false;
2085 heikki.linnakangas@i 1159 [ + + ]: 5327 : if (!ExecCheckIndexConstraints(resultRelInfo, slot, estate,
1160 : : &conflictTid, &invalidItemPtr,
1161 : : arbiterIndexes))
1162 : : {
1163 : : /* committed conflict tuple found */
4071 andres@anarazel.de 1164 [ + + ]: 3092 : if (onconflict == ONCONFLICT_UPDATE)
1165 : : {
1166 : : /*
1167 : : * In case of ON CONFLICT DO UPDATE, execute the UPDATE
1168 : : * part. Be prepared to retry if the UPDATE fails because
1169 : : * of another concurrent UPDATE/DELETE to the conflict
1170 : : * tuple.
1171 : : */
1172 : 2761 : TupleTableSlot *returning = NULL;
1173 : :
1566 alvherre@alvh.no-ip. 1174 [ + + ]: 2761 : if (ExecOnConflictUpdate(context, resultRelInfo,
1175 : : &conflictTid, slot, canSetTag,
1176 : : &returning))
1177 : : {
3003 1178 [ - + ]: 2706 : InstrCountTuples2(&mtstate->ps, 1);
4071 andres@anarazel.de 1179 : 2706 : return returning;
1180 : : }
1181 : : else
1182 : 3 : goto vlock;
1183 : : }
138 dean.a.rasheed@gmail 1184 [ + + ]:GNC 331 : else if (onconflict == ONCONFLICT_SELECT)
1185 : : {
1186 : : /*
1187 : : * In case of ON CONFLICT DO SELECT, optionally lock the
1188 : : * conflicting tuple, fetch it and project RETURNING on
1189 : : * it. Be prepared to retry if locking fails because of a
1190 : : * concurrent UPDATE/DELETE to the conflict tuple.
1191 : : */
1192 : 192 : TupleTableSlot *returning = NULL;
1193 : :
1194 [ + - ]: 192 : if (ExecOnConflictSelect(context, resultRelInfo,
1195 : : &conflictTid, slot, canSetTag,
1196 : : &returning))
1197 : : {
1198 [ - + ]: 176 : InstrCountTuples2(&mtstate->ps, 1);
1199 : 176 : return returning;
1200 : : }
1201 : : else
138 dean.a.rasheed@gmail 1202 :UNC 0 : goto vlock;
1203 : : }
1204 : : else
1205 : : {
1206 : : /*
1207 : : * In case of ON CONFLICT DO NOTHING, do nothing. However,
1208 : : * verify that the tuple is visible to the executor's MVCC
1209 : : * snapshot at higher isolation levels.
1210 : : *
1211 : : * Using ExecGetReturningSlot() to store the tuple for the
1212 : : * recheck isn't that pretty, but we can't trivially use
1213 : : * the input slot, because it might not be of a compatible
1214 : : * type. As there's no conflicting usage of
1215 : : * ExecGetReturningSlot() in the DO NOTHING case...
1216 : : */
4071 andres@anarazel.de 1217 [ - + ]:CBC 139 : Assert(onconflict == ONCONFLICT_NOTHING);
2656 1218 : 139 : ExecCheckTIDVisible(estate, resultRelInfo, &conflictTid,
1219 : : ExecGetReturningSlot(estate, resultRelInfo));
3003 alvherre@alvh.no-ip. 1220 [ - + ]: 129 : InstrCountTuples2(&mtstate->ps, 1);
4071 andres@anarazel.de 1221 : 129 : return NULL;
1222 : : }
1223 : : }
1224 : :
1225 : : /*
1226 : : * Before we start insertion proper, acquire our "speculative
1227 : : * insertion lock". Others can use that to wait for us to decide
1228 : : * if we're going to go ahead with the insertion, instead of
1229 : : * waiting for the whole transaction to complete.
1230 : : */
218 alvherre@kurilemu.de 1231 :GNC 2231 : INJECTION_POINT("exec-insert-before-insert-speculative", NULL);
4071 andres@anarazel.de 1232 :CBC 2231 : specToken = SpeculativeInsertionLockAcquire(GetCurrentTransactionId());
1233 : :
1234 : : /* insert the tuple, with the speculative token */
2595 1235 : 2231 : table_tuple_insert_speculative(resultRelationDesc, slot,
1236 : : estate->es_output_cid,
1237 : : 0,
1238 : : NULL,
1239 : : specToken);
1240 : :
1241 : : /* insert index entries for tuple */
2085 heikki.linnakangas@i 1242 : 2231 : recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
1243 : : estate, EIIT_NO_DUPE_ERROR,
1244 : : slot, arbiterIndexes,
1245 : : &specConflict);
1246 : :
1247 : : /* adjust the tuple's state accordingly */
2595 andres@anarazel.de 1248 : 2227 : table_tuple_complete_speculative(resultRelationDesc, slot,
1249 : 2227 : specToken, !specConflict);
1250 : :
1251 : : /*
1252 : : * Wake up anyone waiting for our decision. They will re-check
1253 : : * the tuple, see that it's no longer speculative, and wait on our
1254 : : * XID as if this was a regularly inserted tuple all along. Or if
1255 : : * we killed the tuple, they will see it's dead, and proceed as if
1256 : : * the tuple never existed.
1257 : : */
4071 1258 : 2227 : SpeculativeInsertionLockRelease(GetCurrentTransactionId());
1259 : :
1260 : : /*
1261 : : * If there was a conflict, start from the beginning. We'll do
1262 : : * the pre-check again, which will now find the conflicting tuple
1263 : : * (unless it aborts before we get there).
1264 : : */
1265 [ + + ]: 2227 : if (specConflict)
1266 : : {
1267 : 11 : list_free(recheckIndexes);
1268 : 11 : goto vlock;
1269 : : }
1270 : :
1271 : : /* Since there was no insertion conflict, we're done */
1272 : : }
1273 : : else
1274 : : {
1275 : : /* insert the tuple normally */
810 akorotkov@postgresql 1276 : 7930017 : table_tuple_insert(resultRelationDesc, slot,
1277 : : estate->es_output_cid,
1278 : : 0, NULL);
1279 : :
1280 : : /* insert index entries for tuple */
1281 [ + + ]: 7930004 : if (resultRelInfo->ri_NumIndices > 0)
133 alvherre@kurilemu.de 1282 :GNC 2413073 : recheckIndexes = ExecInsertIndexTuples(resultRelInfo, estate,
1283 : : 0, slot, NIL,
1284 : : NULL);
1285 : : }
1286 : : }
1287 : :
5604 tgl@sss.pgh.pa.us 1288 [ + + ]:CBC 7932807 : if (canSetTag)
1289 : 7930872 : (estate->es_processed)++;
1290 : :
1291 : : /*
1292 : : * If this insert is the result of a partition key update that moved the
1293 : : * tuple to a new partition, put this row into the transition NEW TABLE,
1294 : : * if there is one. We need to do this separately for DELETE and INSERT
1295 : : * because they happen on different tables.
1296 : : */
3084 rhaas@postgresql.org 1297 : 7932807 : ar_insert_trig_tcs = mtstate->mt_transition_capture;
1298 [ + + + + ]: 7932807 : if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture
1299 [ + + ]: 36 : && mtstate->mt_transition_capture->tcs_update_new_table)
1300 : : {
1563 alvherre@alvh.no-ip. 1301 : 32 : ExecARUpdateTriggers(estate, resultRelInfo,
1302 : : NULL, NULL,
1303 : : NULL,
1304 : : NULL,
1305 : : slot,
1306 : : NULL,
1307 : 32 : mtstate->mt_transition_capture,
1308 : : false);
1309 : :
1310 : : /*
1311 : : * We've already captured the NEW TABLE row, so make sure any AR
1312 : : * INSERT trigger fired below doesn't capture it again.
1313 : : */
3084 rhaas@postgresql.org 1314 : 32 : ar_insert_trig_tcs = NULL;
1315 : : }
1316 : :
1317 : : /* AFTER ROW INSERT Triggers */
2681 andres@anarazel.de 1318 : 7932807 : ExecARInsertTriggers(estate, resultRelInfo, slot, recheckIndexes,
1319 : : ar_insert_trig_tcs);
1320 : :
5994 tgl@sss.pgh.pa.us 1321 : 7932806 : list_free(recheckIndexes);
1322 : :
1323 : : /*
1324 : : * Check any WITH CHECK OPTION constraints from parent views. We are
1325 : : * required to do this after testing all constraints and uniqueness
1326 : : * violations per the SQL spec, so we do it after actually inserting the
1327 : : * record into the heap and all indexes.
1328 : : *
1329 : : * ExecWithCheckOptions will elog(ERROR) if a violation is found, so the
1330 : : * tuple will never be seen, if it violates the WITH CHECK OPTION.
1331 : : *
1332 : : * ExecWithCheckOptions() will skip any WCOs which are not of the kind we
1333 : : * are looking for at this point.
1334 : : */
4730 sfrost@snowman.net 1335 [ + + ]: 7932806 : if (resultRelInfo->ri_WithCheckOptions != NIL)
4085 1336 : 294 : ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);
1337 : :
1338 : : /* Process RETURNING if present */
6107 tgl@sss.pgh.pa.us 1339 [ + + ]: 7932710 : if (resultRelInfo->ri_projectReturning)
1340 : : {
530 dean.a.rasheed@gmail 1341 : 3012 : TupleTableSlot *oldSlot = NULL;
1342 : :
1343 : : /*
1344 : : * If this is part of a cross-partition UPDATE, and the RETURNING list
1345 : : * refers to any OLD columns, ExecDelete() will have saved the tuple
1346 : : * deleted from the original partition, which we must use here to
1347 : : * compute the OLD column values. Otherwise, all OLD column values
1348 : : * will be NULL.
1349 : : */
1350 [ + + ]: 3012 : if (context->cpDeletedSlot)
1351 : : {
1352 : : TupleConversionMap *tupconv_map;
1353 : :
1354 : : /*
1355 : : * Convert the OLD tuple to the new partition's format/slot, if
1356 : : * needed. Note that ExecDelete() already converted it to the
1357 : : * root's partition's format/slot.
1358 : : */
1359 : 30 : oldSlot = context->cpDeletedSlot;
1360 : 30 : tupconv_map = ExecGetRootToChildMap(resultRelInfo, estate);
1361 [ + + ]: 30 : if (tupconv_map != NULL)
1362 : : {
1363 : 10 : oldSlot = execute_attr_map_slot(tupconv_map->attrMap,
1364 : : oldSlot,
1365 : : ExecGetReturningSlot(estate,
1366 : : resultRelInfo));
1367 : :
1368 : 10 : oldSlot->tts_tableOid = context->cpDeletedSlot->tts_tableOid;
1369 : 10 : ItemPointerCopy(&context->cpDeletedSlot->tts_tid, &oldSlot->tts_tid);
1370 : : }
1371 : : }
1372 : :
138 dean.a.rasheed@gmail 1373 :GNC 3012 : result = ExecProcessReturning(context, resultRelInfo, false,
1374 : : oldSlot, slot, planSlot);
1375 : :
1376 : : /*
1377 : : * For a cross-partition UPDATE, release the old tuple, first making
1378 : : * sure that the result slot has a local copy of any pass-by-reference
1379 : : * values.
1380 : : */
530 dean.a.rasheed@gmail 1381 [ + + ]:CBC 3004 : if (context->cpDeletedSlot)
1382 : : {
1383 : 30 : ExecMaterializeSlot(result);
1384 : 30 : ExecClearTuple(oldSlot);
1385 [ + + ]: 30 : if (context->cpDeletedSlot != oldSlot)
1386 : 10 : ExecClearTuple(context->cpDeletedSlot);
1387 : 30 : context->cpDeletedSlot = NULL;
1388 : : }
1389 : : }
1390 : :
1563 alvherre@alvh.no-ip. 1391 [ + + ]: 7932702 : if (inserted_tuple)
1392 : 565 : *inserted_tuple = slot;
1393 [ + + ]: 7932702 : if (insert_destrel)
1394 : 565 : *insert_destrel = resultRelInfo;
1395 : :
3449 rhaas@postgresql.org 1396 : 7932702 : return result;
1397 : : }
1398 : :
1399 : : /* ----------------------------------------------------------------
1400 : : * ExecForPortionOfLeftovers
1401 : : *
1402 : : * Insert tuples for the untouched portion of a row in a FOR
1403 : : * PORTION OF UPDATE/DELETE
1404 : : * ----------------------------------------------------------------
1405 : : */
1406 : : static void
90 peter@eisentraut.org 1407 :GNC 849 : ExecForPortionOfLeftovers(ModifyTableContext *context,
1408 : : EState *estate,
1409 : : ResultRelInfo *resultRelInfo,
1410 : : ItemPointer tupleid)
1411 : : {
1412 : 849 : ModifyTableState *mtstate = context->mtstate;
1413 : 849 : ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
1414 : 849 : ForPortionOfExpr *forPortionOf = (ForPortionOfExpr *) node->forPortionOf;
1415 : : Datum oldRange;
1416 : : TypeCacheEntry *typcache;
1417 : : ForPortionOfState *fpoState;
1418 : : TupleTableSlot *oldtupleSlot;
1419 : : TupleTableSlot *leftoverSlot;
1420 : 849 : TupleConversionMap *map = NULL;
1421 : 849 : HeapTuple oldtuple = NULL;
1422 : : CmdType oldOperation;
1423 : : TransitionCaptureState *oldTcs;
1424 : : FmgrInfo flinfo;
1425 : : PgStat_FunctionCallUsage fcusage;
1426 : : ReturnSetInfo rsi;
1427 : 849 : bool didInit = false;
1428 : 849 : bool shouldFree = false;
22 1429 : 849 : ResultRelInfo *rootRelInfo = mtstate->rootResultRelInfo;
1430 : 849 : bool partitionRouting =
1431 [ + - ]: 1698 : rootRelInfo &&
1432 [ + + ]: 849 : rootRelInfo->ri_RelationDesc->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
1433 : :
90 1434 : 849 : LOCAL_FCINFO(fcinfo, 2);
1435 : :
1436 : 849 : fpoState = resultRelInfo->ri_forPortionOf;
1437 : 849 : oldtupleSlot = fpoState->fp_Existing;
1438 : 849 : leftoverSlot = fpoState->fp_Leftover;
1439 : :
1440 : : /*
1441 : : * Get the old pre-UPDATE/DELETE tuple. We will use its range to compute
1442 : : * untouched parts of history, and if necessary we will insert copies with
1443 : : * truncated start/end times.
1444 : : *
1445 : : * We have already locked the tuple in ExecUpdate/ExecDelete, and it has
1446 : : * passed EvalPlanQual. This ensures that concurrent updates in READ
1447 : : * COMMITTED can't insert conflicting temporal leftovers.
1448 : : *
1449 : : * It does *not* protect against concurrent update/deletes overlooking
1450 : : * each others' leftovers though. See our isolation tests for details
1451 : : * about that and a viable workaround.
1452 : : */
1453 [ - + ]: 849 : if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc, tupleid, SnapshotAny, oldtupleSlot))
90 peter@eisentraut.org 1454 [ # # ]:UNC 0 : elog(ERROR, "failed to fetch tuple for FOR PORTION OF");
1455 : :
90 peter@eisentraut.org 1456 :GNC 849 : slot_getallattrs(oldtupleSlot);
1457 : :
1458 : : /* Get the old range of the record being updated/deleted. */
22 1459 [ - + ]: 849 : if (oldtupleSlot->tts_isnull[fpoState->fp_rangeAttno - 1])
90 peter@eisentraut.org 1460 [ # # ]:UNC 0 : elog(ERROR, "found a NULL range in a temporal table");
22 peter@eisentraut.org 1461 :GNC 849 : oldRange = oldtupleSlot->tts_values[fpoState->fp_rangeAttno - 1];
1462 : :
1463 : : /*
1464 : : * Get the range's type cache entry. This is worth caching for the whole
1465 : : * UPDATE/DELETE as range functions do.
1466 : : */
1467 : :
90 1468 : 849 : typcache = fpoState->fp_leftoverstypcache;
1469 [ + + ]: 849 : if (typcache == NULL)
1470 : : {
1471 : 710 : typcache = lookup_type_cache(forPortionOf->rangeType, 0);
1472 : 710 : fpoState->fp_leftoverstypcache = typcache;
1473 : : }
1474 : :
1475 : : /*
1476 : : * Get the ranges to the left/right of the targeted range. We call a SETOF
1477 : : * support function and insert as many temporal leftovers as it gives us.
1478 : : * Although rangetypes have 0/1/2 leftovers, multiranges have 0/1, and
1479 : : * other types may have more.
1480 : : */
1481 : :
1482 : 849 : fmgr_info(forPortionOf->withoutPortionProc, &flinfo);
1483 : 849 : rsi.type = T_ReturnSetInfo;
1484 : 849 : rsi.econtext = mtstate->ps.ps_ExprContext;
1485 : 849 : rsi.expectedDesc = NULL;
1486 : 849 : rsi.allowedModes = (int) (SFRM_ValuePerCall);
1487 : 849 : rsi.returnMode = SFRM_ValuePerCall;
1488 : : /* isDone is filled below */
1489 : 849 : rsi.setResult = NULL;
1490 : 849 : rsi.setDesc = NULL;
1491 : :
1492 : 849 : InitFunctionCallInfoData(*fcinfo, &flinfo, 2, InvalidOid, NULL, (Node *) &rsi);
1493 : 849 : fcinfo->args[0].value = oldRange;
1494 : 849 : fcinfo->args[0].isnull = false;
1495 : 849 : fcinfo->args[1].value = fpoState->fp_targetRange;
1496 : 849 : fcinfo->args[1].isnull = false;
1497 : :
1498 : : /*
1499 : : * For partitioned tables, we must read leftovers with the tuple
1500 : : * descriptor of the child table, but insert into the root table to enable
1501 : : * tuple routing. So leftoverSlot is configured with the root's tuple
1502 : : * descriptor. But for traditional table inheritance, we don't need tuple
1503 : : * routing and just insert directly into the child table to preserve
1504 : : * child-specific columns. In that case, leftoverSlot uses the child's
1505 : : * (resultRelInfo) tuple descriptor.
1506 : : */
22 1507 [ + + ]: 849 : if (partitionRouting)
1508 : : {
1509 : 66 : map = ExecGetChildToRootMap(resultRelInfo);
90 1510 : 66 : resultRelInfo = resultRelInfo->ri_RootResultRelInfo;
1511 : : }
1512 : :
1513 : : /*
1514 : : * Insert a leftover for each value returned by the without_portion helper
1515 : : * function
1516 : : */
1517 : : while (true)
1518 : 1115 : {
1519 : : Datum leftover;
1520 : :
1521 : : /* Call the function one time */
71 tgl@sss.pgh.pa.us 1522 : 1964 : pgstat_init_function_usage(fcinfo, &fcusage);
1523 : :
1524 : 1964 : fcinfo->isnull = false;
1525 : 1964 : rsi.isDone = ExprSingleResult;
1526 : 1964 : leftover = FunctionCallInvoke(fcinfo);
1527 : :
1528 : 1964 : pgstat_end_function_usage(&fcusage,
1529 : 1964 : rsi.isDone != ExprMultipleResult);
1530 : :
1531 [ - + ]: 1964 : if (rsi.returnMode != SFRM_ValuePerCall)
71 tgl@sss.pgh.pa.us 1532 [ # # ]:UNC 0 : elog(ERROR, "without_portion function violated function call protocol");
1533 : :
1534 : : /* Are we done? */
90 peter@eisentraut.org 1535 [ + + ]:GNC 1964 : if (rsi.isDone == ExprEndResult)
1536 : 817 : break;
1537 : :
1538 [ - + ]: 1147 : if (fcinfo->isnull)
71 tgl@sss.pgh.pa.us 1539 [ # # ]:UNC 0 : elog(ERROR, "got a null from without_portion function");
1540 : :
1541 : : /*
1542 : : * Does the new Datum violate domain checks? Row-level CHECK
1543 : : * constraints are validated by ExecInsert, so we don't need to do
1544 : : * anything here for those.
1545 : : */
90 peter@eisentraut.org 1546 [ + + ]:GNC 1147 : if (forPortionOf->isDomain)
1547 : 80 : domain_check(leftover, false, forPortionOf->rangeVar->vartype, NULL, NULL);
1548 : :
1549 [ + + ]: 1131 : if (!didInit)
1550 : : {
1551 : : /*
1552 : : * Make a copy of the pre-UPDATE row. Then we'll overwrite the
1553 : : * range column below. Only partitioned targets need conversion to
1554 : : * the root table's format, because they reinsert through the root
1555 : : * relation for tuple routing.
1556 : : */
1557 [ + + ]: 733 : if (map != NULL)
1558 : : {
1559 : 16 : leftoverSlot = execute_attr_map_slot(map->attrMap,
1560 : : oldtupleSlot,
1561 : : leftoverSlot);
1562 : : }
1563 : : else
1564 : : {
1565 : 717 : oldtuple = ExecFetchSlotHeapTuple(oldtupleSlot, false, &shouldFree);
1566 : 717 : ExecForceStoreHeapTuple(oldtuple, leftoverSlot, false);
1567 : : }
1568 : :
1569 : : /*
1570 : : * Save some mtstate things so we can restore them below. XXX:
1571 : : * Should we create our own ModifyTableState instead?
1572 : : */
1573 : 733 : oldOperation = mtstate->operation;
1574 : 733 : mtstate->operation = CMD_INSERT;
1575 : 733 : oldTcs = mtstate->mt_transition_capture;
1576 : :
1577 : 733 : didInit = true;
1578 : : }
1579 : : else
1580 : : {
1581 : : /*
1582 : : * Re-copy the original row into leftoverSlot because ExecInsert
1583 : : * might pass leftoverSlot to BEFORE ROW INSERT triggers, which
1584 : : * can modify the slot contents.
1585 : : */
26 1586 [ + + ]: 398 : if (map != NULL)
1587 : 16 : execute_attr_map_slot(map->attrMap, oldtupleSlot, leftoverSlot);
1588 : : else
1589 : 382 : ExecForceStoreHeapTuple(oldtuple, leftoverSlot, false);
1590 : : }
1591 : :
22 1592 : 1131 : leftoverSlot->tts_values[resultRelInfo->ri_forPortionOf->fp_rangeAttno - 1] = leftover;
1593 : 1131 : leftoverSlot->tts_isnull[resultRelInfo->ri_forPortionOf->fp_rangeAttno - 1] = false;
90 1594 : 1131 : ExecMaterializeSlot(leftoverSlot);
1595 : :
1596 : : /*
1597 : : * The standard says that each temporal leftover should execute its
1598 : : * own INSERT statement, firing all statement and row triggers, but
1599 : : * skipping insert permission checks. Therefore we give each insert
1600 : : * its own transition table. If we just push & pop a new trigger level
1601 : : * for each insert, we get exactly what we need.
1602 : : *
1603 : : * We have to make sure that the inserts don't add to the ROW_COUNT
1604 : : * diagnostic or the command tag, so we pass false for canSetTag.
1605 : : */
1606 : 1131 : AfterTriggerBeginQuery();
1607 : 1131 : ExecSetupTransitionCaptureState(mtstate, estate);
1608 : 1131 : fireBSTriggers(mtstate);
1609 : 1131 : ExecInsert(context, resultRelInfo, leftoverSlot, false, NULL, NULL);
1610 : 1115 : fireASTriggers(mtstate);
1611 : 1115 : AfterTriggerEndQuery(estate);
1612 : : }
1613 : :
1614 [ + + ]: 817 : if (didInit)
1615 : : {
1616 : 717 : mtstate->operation = oldOperation;
1617 : 717 : mtstate->mt_transition_capture = oldTcs;
1618 : :
1619 [ - + ]: 717 : if (shouldFree)
90 peter@eisentraut.org 1620 :UNC 0 : heap_freetuple(oldtuple);
1621 : : }
90 peter@eisentraut.org 1622 :GNC 817 : }
1623 : :
1624 : : /* ----------------------------------------------------------------
1625 : : * ExecBatchInsert
1626 : : *
1627 : : * Insert multiple tuples in an efficient way.
1628 : : * Currently, this handles inserting into a foreign table without
1629 : : * RETURNING clause.
1630 : : * ----------------------------------------------------------------
1631 : : */
1632 : : static void
1987 tomas.vondra@postgre 1633 :CBC 29 : ExecBatchInsert(ModifyTableState *mtstate,
1634 : : ResultRelInfo *resultRelInfo,
1635 : : TupleTableSlot **slots,
1636 : : TupleTableSlot **planSlots,
1637 : : int numSlots,
1638 : : EState *estate,
1639 : : bool canSetTag)
1640 : : {
1641 : : int i;
1642 : 29 : int numInserted = numSlots;
1643 : 29 : TupleTableSlot *slot = NULL;
1644 : : TupleTableSlot **rslots;
1645 : :
1646 : : /*
1647 : : * insert into foreign table: let the FDW do it
1648 : : */
1649 : 29 : rslots = resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert(estate,
1650 : : resultRelInfo,
1651 : : slots,
1652 : : planSlots,
1653 : : &numInserted);
1654 : :
1655 [ + + ]: 173 : for (i = 0; i < numInserted; i++)
1656 : : {
1657 : 145 : slot = rslots[i];
1658 : :
1659 : : /*
1660 : : * AFTER ROW Triggers might reference the tableoid column, so
1661 : : * (re-)initialize tts_tableOid before evaluating them.
1662 : : */
1663 : 145 : slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
1664 : :
1665 : : /* AFTER ROW INSERT Triggers */
1666 : 145 : ExecARInsertTriggers(estate, resultRelInfo, slot, NIL,
1667 : 145 : mtstate->mt_transition_capture);
1668 : :
1669 : : /*
1670 : : * Check any WITH CHECK OPTION constraints from parent views. See the
1671 : : * comment in ExecInsert.
1672 : : */
1673 [ - + ]: 144 : if (resultRelInfo->ri_WithCheckOptions != NIL)
1987 tomas.vondra@postgre 1674 :UBC 0 : ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);
1675 : : }
1676 : :
1987 tomas.vondra@postgre 1677 [ + - + - ]:CBC 28 : if (canSetTag && numInserted > 0)
1678 : 28 : estate->es_processed += numInserted;
1679 : :
1680 : : /* Clean up all the slots, ready for the next batch */
1162 michael@paquier.xyz 1681 [ + + ]: 172 : for (i = 0; i < numSlots; i++)
1682 : : {
1683 : 144 : ExecClearTuple(slots[i]);
1684 : 144 : ExecClearTuple(planSlots[i]);
1685 : : }
1686 : 28 : resultRelInfo->ri_NumSlots = 0;
1987 tomas.vondra@postgre 1687 : 28 : }
1688 : :
1689 : : /*
1690 : : * ExecPendingInserts -- flushes all pending inserts to the foreign tables
1691 : : */
1692 : : static void
1313 efujita@postgresql.o 1693 : 18 : ExecPendingInserts(EState *estate)
1694 : : {
1695 : : ListCell *l1,
1696 : : *l2;
1697 : :
1300 1698 [ + - + + : 36 : forboth(l1, estate->es_insert_pending_result_relations,
+ - + + +
+ + - +
+ ]
1699 : : l2, estate->es_insert_pending_modifytables)
1700 : : {
1701 : 19 : ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l1);
1702 : 19 : ModifyTableState *mtstate = (ModifyTableState *) lfirst(l2);
1703 : :
1313 1704 [ - + ]: 19 : Assert(mtstate);
1705 : 19 : ExecBatchInsert(mtstate, resultRelInfo,
1706 : : resultRelInfo->ri_Slots,
1707 : : resultRelInfo->ri_PlanSlots,
1708 : : resultRelInfo->ri_NumSlots,
1709 : 19 : estate, mtstate->canSetTag);
1710 : : }
1711 : :
1712 : 17 : list_free(estate->es_insert_pending_result_relations);
1300 1713 : 17 : list_free(estate->es_insert_pending_modifytables);
1313 1714 : 17 : estate->es_insert_pending_result_relations = NIL;
1300 1715 : 17 : estate->es_insert_pending_modifytables = NIL;
1313 1716 : 17 : }
1717 : :
1718 : : /*
1719 : : * ExecDeletePrologue -- subroutine for ExecDelete
1720 : : *
1721 : : * Prepare executor state for DELETE. Actually, the only thing we have to do
1722 : : * here is execute BEFORE ROW triggers. We return false if one of them makes
1723 : : * the delete a no-op; otherwise, return true.
1724 : : */
1725 : : static bool
1566 alvherre@alvh.no-ip. 1726 : 985170 : ExecDeletePrologue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
1727 : : ItemPointer tupleid, HeapTuple oldtuple,
1728 : : TupleTableSlot **epqreturnslot, TM_Result *result)
1729 : : {
1205 dean.a.rasheed@gmail 1730 [ + + ]: 985170 : if (result)
1731 : 1066 : *result = TM_Ok;
1732 : :
1733 : : /* BEFORE ROW DELETE triggers */
1566 alvherre@alvh.no-ip. 1734 [ + + ]: 985170 : if (resultRelInfo->ri_TrigDesc &&
1735 [ + + ]: 4712 : resultRelInfo->ri_TrigDesc->trig_delete_before_row)
1736 : : {
1737 : : /* Flush any pending inserts, so rows are visible to the triggers */
1313 efujita@postgresql.o 1738 [ + + ]: 217 : if (context->estate->es_insert_pending_result_relations != NIL)
1739 : 1 : ExecPendingInserts(context->estate);
1740 : :
1566 alvherre@alvh.no-ip. 1741 : 207 : return ExecBRDeleteTriggers(context->estate, context->epqstate,
1742 : : resultRelInfo, tupleid, oldtuple,
1743 : : epqreturnslot, result, &context->tmfd,
347 dean.a.rasheed@gmail 1744 : 217 : context->mtstate->operation == CMD_MERGE);
1745 : : }
1746 : :
1566 alvherre@alvh.no-ip. 1747 : 984953 : return true;
1748 : : }
1749 : :
1750 : : /*
1751 : : * ExecDeleteAct -- subroutine for ExecDelete
1752 : : *
1753 : : * Actually delete the tuple from a plain table.
1754 : : *
1755 : : * Caller is in charge of doing EvalPlanQual as necessary
1756 : : */
1757 : : static TM_Result
1758 : 985064 : ExecDeleteAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
1759 : : ItemPointer tupleid, bool changingPart)
1760 : : {
1761 : 985064 : EState *estate = context->estate;
90 alvherre@kurilemu.de 1762 :GNC 985064 : uint32 options = 0;
1763 : :
1764 [ + + ]: 985064 : if (changingPart)
1765 : 696 : options |= TABLE_DELETE_CHANGING_PARTITION;
1766 : :
1566 alvherre@alvh.no-ip. 1767 :CBC 985064 : return table_tuple_delete(resultRelInfo->ri_RelationDesc, tupleid,
1768 : : estate->es_output_cid,
1769 : : options,
1770 : : estate->es_snapshot,
1771 : : estate->es_crosscheck_snapshot,
1772 : : true /* wait for commit */ ,
1773 : : &context->tmfd);
1774 : : }
1775 : :
1776 : : /*
1777 : : * ExecDeleteEpilogue -- subroutine for ExecDelete
1778 : : *
1779 : : * Closing steps of tuple deletion; this invokes AFTER FOR EACH ROW triggers,
1780 : : * including the UPDATE triggers if the deletion is being done as part of a
1781 : : * cross-partition tuple move. It also inserts temporal leftovers from a
1782 : : * DELETE FOR PORTION OF.
1783 : : */
1784 : : static void
1785 : 985003 : ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
1786 : : ItemPointer tupleid, HeapTuple oldtuple, bool changingPart)
1787 : : {
1788 : 985003 : ModifyTableState *mtstate = context->mtstate;
1789 : 985003 : EState *estate = context->estate;
1790 : : TransitionCaptureState *ar_delete_trig_tcs;
1791 : :
1792 : : /*
1793 : : * If this delete is the result of a partition key update that moved the
1794 : : * tuple to a new partition, put this row into the transition OLD TABLE,
1795 : : * if there is one. We need to do this separately for DELETE and INSERT
1796 : : * because they happen on different tables.
1797 : : */
1798 : 985003 : ar_delete_trig_tcs = mtstate->mt_transition_capture;
1799 [ + + + + ]: 985003 : if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture &&
1800 [ + + ]: 36 : mtstate->mt_transition_capture->tcs_update_old_table)
1801 : : {
1563 1802 : 32 : ExecARUpdateTriggers(estate, resultRelInfo,
1803 : : NULL, NULL,
1804 : : tupleid, oldtuple,
810 akorotkov@postgresql 1805 : 32 : NULL, NULL, mtstate->mt_transition_capture,
1806 : : false);
1807 : :
1808 : : /*
1809 : : * We've already captured the OLD TABLE row, so make sure any AR
1810 : : * DELETE trigger fired below doesn't capture it again.
1811 : : */
1566 alvherre@alvh.no-ip. 1812 : 32 : ar_delete_trig_tcs = NULL;
1813 : : }
1814 : :
1815 : : /* Compute temporal leftovers in FOR PORTION OF */
90 peter@eisentraut.org 1816 [ + + ]:GNC 985003 : if (((ModifyTable *) context->mtstate->ps.plan)->forPortionOf)
1817 : 378 : ExecForPortionOfLeftovers(context, estate, resultRelInfo, tupleid);
1818 : :
1819 : : /* AFTER ROW DELETE Triggers */
810 akorotkov@postgresql 1820 :CBC 984987 : ExecARDeleteTriggers(estate, resultRelInfo, tupleid, oldtuple,
1821 : : ar_delete_trig_tcs, changingPart);
1566 alvherre@alvh.no-ip. 1822 : 984985 : }
1823 : :
1824 : : /* ----------------------------------------------------------------
1825 : : * ExecDelete
1826 : : *
1827 : : * DELETE is like UPDATE, except that we delete the tuple and no
1828 : : * index modifications are needed.
1829 : : *
1830 : : * When deleting from a table, tupleid identifies the tuple to delete and
1831 : : * oldtuple is NULL. When deleting through a view INSTEAD OF trigger,
1832 : : * oldtuple is passed to the triggers and identifies what to delete, and
1833 : : * tupleid is invalid. When deleting from a foreign table, tupleid is
1834 : : * invalid; the FDW has to figure out which row to delete using data from
1835 : : * the planSlot. oldtuple is passed to foreign table triggers; it is
1836 : : * NULL when the foreign table has no relevant triggers. We use
1837 : : * tupleDeleted to indicate whether the tuple is actually deleted,
1838 : : * callers can use it to decide whether to continue the operation. When
1839 : : * this DELETE is a part of an UPDATE of partition-key, then the slot
1840 : : * returned by EvalPlanQual() is passed back using output parameter
1841 : : * epqreturnslot.
1842 : : *
1843 : : * Returns RETURNING result if any, otherwise NULL.
1844 : : * ----------------------------------------------------------------
1845 : : */
1846 : : static TupleTableSlot *
1847 : 984822 : ExecDelete(ModifyTableContext *context,
1848 : : ResultRelInfo *resultRelInfo,
1849 : : ItemPointer tupleid,
1850 : : HeapTuple oldtuple,
1851 : : bool processReturning,
1852 : : bool changingPart,
1853 : : bool canSetTag,
1854 : : TM_Result *tmresult,
1855 : : bool *tupleDeleted,
1856 : : TupleTableSlot **epqreturnslot)
1857 : : {
1858 : 984822 : EState *estate = context->estate;
2085 heikki.linnakangas@i 1859 : 984822 : Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
4860 tgl@sss.pgh.pa.us 1860 : 984822 : TupleTableSlot *slot = NULL;
1861 : : TM_Result result;
1862 : : bool saveOld;
1863 : :
3084 rhaas@postgresql.org 1864 [ + + ]: 984822 : if (tupleDeleted)
1865 : 718 : *tupleDeleted = false;
1866 : :
1867 : : /*
1868 : : * Prepare for the delete. This includes BEFORE ROW triggers, so we're
1869 : : * done if it says we are.
1870 : : */
1566 alvherre@alvh.no-ip. 1871 [ + + ]: 984822 : if (!ExecDeletePrologue(context, resultRelInfo, tupleid, oldtuple,
1872 : : epqreturnslot, tmresult))
1873 : 33 : return NULL;
1874 : :
1875 : : /* INSTEAD OF ROW DELETE Triggers */
5742 tgl@sss.pgh.pa.us 1876 [ + + ]: 984779 : if (resultRelInfo->ri_TrigDesc &&
1877 [ + + ]: 4625 : resultRelInfo->ri_TrigDesc->trig_delete_instead_row)
6107 1878 : 31 : {
1879 : : bool dodelete;
1880 : :
5742 1881 [ - + ]: 35 : Assert(oldtuple != NULL);
4482 noah@leadboat.com 1882 : 35 : dodelete = ExecIRDeleteTriggers(estate, resultRelInfo, oldtuple);
1883 : :
5742 tgl@sss.pgh.pa.us 1884 [ + + ]: 35 : if (!dodelete) /* "do nothing" */
6107 1885 : 4 : return NULL;
1886 : : }
4860 1887 [ + + ]: 984744 : else if (resultRelInfo->ri_FdwRoutine)
1888 : : {
1889 : : /*
1890 : : * delete from foreign table: let the FDW do it
1891 : : *
1892 : : * We offer the returning slot as a place to store RETURNING data,
1893 : : * although the FDW can return some other slot if it wants.
1894 : : */
2681 andres@anarazel.de 1895 : 23 : slot = ExecGetReturningSlot(estate, resultRelInfo);
4860 tgl@sss.pgh.pa.us 1896 : 23 : slot = resultRelInfo->ri_FdwRoutine->ExecForeignDelete(estate,
1897 : : resultRelInfo,
1898 : : slot,
1899 : : context->planSlot);
1900 : :
1901 [ - + ]: 23 : if (slot == NULL) /* "do nothing" */
4860 tgl@sss.pgh.pa.us 1902 :UBC 0 : return NULL;
1903 : :
1904 : : /*
1905 : : * RETURNING expressions might reference the tableoid column, so
1906 : : * (re)initialize tts_tableOid before evaluating them.
1907 : : */
2815 andres@anarazel.de 1908 [ + + ]:CBC 23 : if (TTS_EMPTY(slot))
3799 rhaas@postgresql.org 1909 : 5 : ExecStoreAllNullTuple(slot);
1910 : :
2681 andres@anarazel.de 1911 : 23 : slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
1912 : : }
1913 : : else
1914 : : {
1915 : : /*
1916 : : * delete the tuple
1917 : : *
1918 : : * Note: if context->estate->es_crosscheck_snapshot isn't
1919 : : * InvalidSnapshot, we check that the row to be deleted is visible to
1920 : : * that snapshot, and throw a can't-serialize error if not. This is a
1921 : : * special-case behavior needed for referential integrity updates in
1922 : : * transaction-snapshot mode transactions.
1923 : : */
1359 john.naylor@postgres 1924 : 984721 : ldelete:
810 akorotkov@postgresql 1925 : 984727 : result = ExecDeleteAct(context, resultRelInfo, tupleid, changingPart);
1926 : :
922 dean.a.rasheed@gmail 1927 [ + + ]: 984709 : if (tmresult)
1928 : 696 : *tmresult = result;
1929 : :
5742 tgl@sss.pgh.pa.us 1930 [ + + + + : 984709 : switch (result)
- ]
1931 : : {
2656 andres@anarazel.de 1932 : 28 : case TM_SelfModified:
1933 : :
1934 : : /*
1935 : : * The target tuple was already updated or deleted by the
1936 : : * current command, or by a later command in the current
1937 : : * transaction. The former case is possible in a join DELETE
1938 : : * where multiple tuples join to the same target tuple. This
1939 : : * is somewhat questionable, but Postgres has always allowed
1940 : : * it: we just ignore additional deletion attempts.
1941 : : *
1942 : : * The latter case arises if the tuple is modified by a
1943 : : * command in a BEFORE trigger, or perhaps by a command in a
1944 : : * volatile function used in the query. In such situations we
1945 : : * should not ignore the deletion, but it is equally unsafe to
1946 : : * proceed. We don't want to discard the original DELETE
1947 : : * while keeping the triggered actions based on its deletion;
1948 : : * and it would be no better to allow the original DELETE
1949 : : * while discarding updates that it triggered. The row update
1950 : : * carries some information that might be important according
1951 : : * to business rules; so throwing an error is the only safe
1952 : : * course.
1953 : : *
1954 : : * If a trigger actually intends this type of interaction, it
1955 : : * can re-execute the DELETE and then return NULL to cancel
1956 : : * the outer delete.
1957 : : */
1566 alvherre@alvh.no-ip. 1958 [ + + ]: 28 : if (context->tmfd.cmax != estate->es_output_cid)
4995 kgrittn@postgresql.o 1959 [ + - ]: 4 : ereport(ERROR,
1960 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
1961 : : errmsg("tuple to be deleted was already modified by an operation triggered by the current command"),
1962 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
1963 : :
1964 : : /* Else, already deleted by self; nothing to do */
5742 tgl@sss.pgh.pa.us 1965 : 24 : return NULL;
1966 : :
2656 andres@anarazel.de 1967 : 984619 : case TM_Ok:
5742 tgl@sss.pgh.pa.us 1968 : 984619 : break;
1969 : :
2656 andres@anarazel.de 1970 : 47 : case TM_Updated:
1971 : : {
1972 : : TupleTableSlot *inputslot;
1973 : : TupleTableSlot *epqslot;
1974 : :
1975 [ + + ]: 47 : if (IsolationUsesXactSnapshot())
1976 [ + - ]: 10 : ereport(ERROR,
1977 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1978 : : errmsg("could not serialize access due to concurrent update")));
1979 : :
1980 : : /*
1981 : : * Already know that we're going to need to do EPQ, so
1982 : : * fetch tuple directly into the right slot.
1983 : : */
810 akorotkov@postgresql 1984 : 37 : EvalPlanQualBegin(context->epqstate);
1985 : 37 : inputslot = EvalPlanQualSlot(context->epqstate, resultRelationDesc,
1986 : : resultRelInfo->ri_RangeTableIndex);
1987 : :
1988 : 37 : result = table_tuple_lock(resultRelationDesc, tupleid,
1989 : : estate->es_snapshot,
1990 : : inputslot, estate->es_output_cid,
1991 : : LockTupleExclusive, LockWaitBlock,
1992 : : TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
1993 : : &context->tmfd);
1994 : :
1995 [ + + + - ]: 33 : switch (result)
1996 : : {
1997 : 30 : case TM_Ok:
1998 [ - + ]: 30 : Assert(context->tmfd.traversed);
1999 : 30 : epqslot = EvalPlanQual(context->epqstate,
2000 : : resultRelationDesc,
2001 : : resultRelInfo->ri_RangeTableIndex,
2002 : : inputslot);
2003 [ + - + + ]: 30 : if (TupIsNull(epqslot))
2004 : : /* Tuple not passing quals anymore, exiting... */
2005 : 16 : return NULL;
2006 : :
2007 : : /*
2008 : : * If requested, skip delete and pass back the
2009 : : * updated row.
2010 : : */
2011 [ + + ]: 14 : if (epqreturnslot)
2012 : : {
2013 : 8 : *epqreturnslot = epqslot;
2014 : 8 : return NULL;
2015 : : }
2016 : : else
2017 : 6 : goto ldelete;
2018 : :
2019 : 2 : case TM_SelfModified:
2020 : :
2021 : : /*
2022 : : * This can be reached when following an update
2023 : : * chain from a tuple updated by another session,
2024 : : * reaching a tuple that was already updated in
2025 : : * this transaction. If previously updated by this
2026 : : * command, ignore the delete, otherwise error
2027 : : * out.
2028 : : *
2029 : : * See also TM_SelfModified response to
2030 : : * table_tuple_delete() above.
2031 : : */
2032 [ + + ]: 2 : if (context->tmfd.cmax != estate->es_output_cid)
2033 [ + - ]: 1 : ereport(ERROR,
2034 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
2035 : : errmsg("tuple to be deleted was already modified by an operation triggered by the current command"),
2036 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
2037 : 1 : return NULL;
2038 : :
2039 : 1 : case TM_Deleted:
2040 : : /* tuple already deleted; nothing to do */
2041 : 1 : return NULL;
2042 : :
810 akorotkov@postgresql 2043 :UBC 0 : default:
2044 : :
2045 : : /*
2046 : : * TM_Invisible should be impossible because we're
2047 : : * waiting for updated row versions, and would
2048 : : * already have errored out if the first version
2049 : : * is invisible.
2050 : : *
2051 : : * TM_Updated should be impossible, because we're
2052 : : * locking the latest version via
2053 : : * TUPLE_LOCK_FLAG_FIND_LAST_VERSION.
2054 : : */
2055 [ # # ]: 0 : elog(ERROR, "unexpected table_tuple_lock status: %u",
2056 : : result);
2057 : : return NULL;
2058 : : }
2059 : :
2060 : : Assert(false);
2061 : : break;
2062 : : }
2063 : :
2656 andres@anarazel.de 2064 :CBC 15 : case TM_Deleted:
2065 [ + + ]: 15 : if (IsolationUsesXactSnapshot())
2656 andres@anarazel.de 2066 [ + - ]:GBC 9 : ereport(ERROR,
2067 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2068 : : errmsg("could not serialize access due to concurrent delete")));
2069 : : /* tuple already deleted; nothing to do */
5742 tgl@sss.pgh.pa.us 2070 :CBC 6 : return NULL;
2071 : :
5742 tgl@sss.pgh.pa.us 2072 :UBC 0 : default:
2595 andres@anarazel.de 2073 [ # # ]: 0 : elog(ERROR, "unrecognized table_tuple_delete status: %u",
2074 : : result);
2075 : : return NULL;
2076 : : }
2077 : :
2078 : : /*
2079 : : * Note: Normally one would think that we have to delete index tuples
2080 : : * associated with the heap tuple now...
2081 : : *
2082 : : * ... but in POSTGRES, we have no need to do this because VACUUM will
2083 : : * take care of it later. We can't delete index tuples immediately
2084 : : * anyway, since the tuple is still visible to other transactions.
2085 : : */
2086 : : }
2087 : :
5604 tgl@sss.pgh.pa.us 2088 [ + + ]:CBC 984673 : if (canSetTag)
2089 : 983840 : (estate->es_processed)++;
2090 : :
2091 : : /* Tell caller that the delete actually happened. */
3084 rhaas@postgresql.org 2092 [ + + ]: 984673 : if (tupleDeleted)
2093 : 666 : *tupleDeleted = true;
2094 : :
810 akorotkov@postgresql 2095 : 984673 : ExecDeleteEpilogue(context, resultRelInfo, tupleid, oldtuple, changingPart);
2096 : :
2097 : : /*
2098 : : * Process RETURNING if present and if requested.
2099 : : *
2100 : : * If this is part of a cross-partition UPDATE, and the RETURNING list
2101 : : * refers to any OLD column values, save the old tuple here for later
2102 : : * processing of the RETURNING list by ExecInsert().
2103 : : */
530 dean.a.rasheed@gmail 2104 [ + + + + ]: 984750 : saveOld = changingPart && resultRelInfo->ri_projectReturning &&
2105 [ + + ]: 95 : resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD;
2106 : :
2107 [ + + + + : 984655 : if (resultRelInfo->ri_projectReturning && (processReturning || saveOld))
+ + ]
2108 : : {
2109 : : /*
2110 : : * We have to put the target tuple into a slot, which means first we
2111 : : * gotta fetch it. We can use the trigger tuple slot.
2112 : : */
2113 : : TupleTableSlot *rslot;
2114 : :
4860 tgl@sss.pgh.pa.us 2115 [ + + ]: 628 : if (resultRelInfo->ri_FdwRoutine)
2116 : : {
2117 : : /* FDW must have provided a slot containing the deleted row */
2118 [ + - - + ]: 7 : Assert(!TupIsNull(slot));
2119 : : }
2120 : : else
2121 : : {
2681 andres@anarazel.de 2122 : 621 : slot = ExecGetReturningSlot(estate, resultRelInfo);
4860 tgl@sss.pgh.pa.us 2123 [ + + ]: 621 : if (oldtuple != NULL)
2124 : : {
2629 andres@anarazel.de 2125 : 16 : ExecForceStoreHeapTuple(oldtuple, slot, false);
2126 : : }
2127 : : else
2128 : : {
810 akorotkov@postgresql 2129 [ - + ]: 605 : if (!table_tuple_fetch_row_version(resultRelationDesc, tupleid,
2130 : : SnapshotAny, slot))
810 akorotkov@postgresql 2131 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
2132 : : }
2133 : : }
2134 : :
2135 : : /*
2136 : : * If required, save the old tuple for later processing of the
2137 : : * RETURNING list by ExecInsert().
2138 : : */
530 dean.a.rasheed@gmail 2139 [ + + ]:CBC 628 : if (saveOld)
2140 : : {
2141 : : TupleConversionMap *tupconv_map;
2142 : :
2143 : : /*
2144 : : * Convert the tuple into the root partition's format/slot, if
2145 : : * needed. ExecInsert() will then convert it to the new
2146 : : * partition's format/slot, if necessary.
2147 : : */
2148 : 30 : tupconv_map = ExecGetChildToRootMap(resultRelInfo);
2149 [ + + ]: 30 : if (tupconv_map != NULL)
2150 : : {
2151 : 12 : ResultRelInfo *rootRelInfo = context->mtstate->rootResultRelInfo;
2152 : 12 : TupleTableSlot *oldSlot = slot;
2153 : :
2154 : 12 : slot = execute_attr_map_slot(tupconv_map->attrMap,
2155 : : slot,
2156 : : ExecGetReturningSlot(estate,
2157 : : rootRelInfo));
2158 : :
2159 : 12 : slot->tts_tableOid = oldSlot->tts_tableOid;
2160 : 12 : ItemPointerCopy(&oldSlot->tts_tid, &slot->tts_tid);
2161 : : }
2162 : :
2163 : 30 : context->cpDeletedSlot = slot;
2164 : :
2165 : 30 : return NULL;
2166 : : }
2167 : :
138 dean.a.rasheed@gmail 2168 :GNC 598 : rslot = ExecProcessReturning(context, resultRelInfo, true,
2169 : : slot, NULL, context->planSlot);
2170 : :
2171 : : /*
2172 : : * Before releasing the target tuple again, make sure rslot has a
2173 : : * local copy of any pass-by-reference values.
2174 : : */
4860 tgl@sss.pgh.pa.us 2175 :CBC 598 : ExecMaterializeSlot(rslot);
2176 : :
6107 2177 : 598 : ExecClearTuple(slot);
2178 : :
2179 : 598 : return rslot;
2180 : : }
2181 : :
2182 : 984027 : return NULL;
2183 : : }
2184 : :
2185 : : /*
2186 : : * ExecCrossPartitionUpdate --- Move an updated tuple to another partition.
2187 : : *
2188 : : * This works by first deleting the old tuple from the current partition,
2189 : : * followed by inserting the new tuple into the root parent table, that is,
2190 : : * mtstate->rootResultRelInfo. It will be re-routed from there to the
2191 : : * correct partition.
2192 : : *
2193 : : * Returns true if the tuple has been successfully moved, or if it's found
2194 : : * that the tuple was concurrently deleted so there's nothing more to do
2195 : : * for the caller.
2196 : : *
2197 : : * False is returned if the tuple we're trying to move is found to have been
2198 : : * concurrently updated. In that case, the caller must check if the updated
2199 : : * tuple that's returned in *retry_slot still needs to be re-routed, and call
2200 : : * this function again or perform a regular update accordingly. For MERGE,
2201 : : * the updated tuple is not returned in *retry_slot; it has its own retry
2202 : : * logic.
2203 : : */
2204 : : static bool
1566 alvherre@alvh.no-ip. 2205 : 750 : ExecCrossPartitionUpdate(ModifyTableContext *context,
2206 : : ResultRelInfo *resultRelInfo,
2207 : : ItemPointer tupleid, HeapTuple oldtuple,
2208 : : TupleTableSlot *slot,
2209 : : bool canSetTag,
2210 : : UpdateContext *updateCxt,
2211 : : TM_Result *tmresult,
2212 : : TupleTableSlot **retry_slot,
2213 : : TupleTableSlot **inserted_tuple,
2214 : : ResultRelInfo **insert_destrel)
2215 : : {
2216 : 750 : ModifyTableState *mtstate = context->mtstate;
2084 heikki.linnakangas@i 2217 : 750 : EState *estate = mtstate->ps.state;
2218 : : TupleConversionMap *tupconv_map;
2219 : : bool tuple_deleted;
2220 : 750 : TupleTableSlot *epqslot = NULL;
2221 : :
530 dean.a.rasheed@gmail 2222 : 750 : context->cpDeletedSlot = NULL;
1566 alvherre@alvh.no-ip. 2223 : 750 : context->cpUpdateReturningSlot = NULL;
1205 dean.a.rasheed@gmail 2224 : 750 : *retry_slot = NULL;
2225 : :
2226 : : /*
2227 : : * Disallow an INSERT ON CONFLICT DO UPDATE that causes the original row
2228 : : * to migrate to a different partition. Maybe this can be implemented
2229 : : * some day, but it seems a fringe feature with little redeeming value.
2230 : : */
2084 heikki.linnakangas@i 2231 [ - + ]: 750 : if (((ModifyTable *) mtstate->ps.plan)->onConflictAction == ONCONFLICT_UPDATE)
2084 heikki.linnakangas@i 2232 [ # # ]:UBC 0 : ereport(ERROR,
2233 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2234 : : errmsg("invalid ON UPDATE specification"),
2235 : : errdetail("The result tuple would appear in a different partition than the original tuple.")));
2236 : :
2237 : : /*
2238 : : * When an UPDATE is run directly on a leaf partition, simply fail with a
2239 : : * partition constraint violation error.
2240 : : */
1911 tgl@sss.pgh.pa.us 2241 [ + + ]:CBC 750 : if (resultRelInfo == mtstate->rootResultRelInfo)
2084 heikki.linnakangas@i 2242 : 32 : ExecPartitionCheckEmitError(resultRelInfo, slot, estate);
2243 : :
2244 : : /*
2245 : : * Initialize tuple routing info if not already done. Note whatever we do
2246 : : * here must be done in ExecInitModifyTable for FOR PORTION OF as well.
2247 : : */
1911 tgl@sss.pgh.pa.us 2248 [ + + ]: 718 : if (mtstate->mt_partition_tuple_routing == NULL)
2249 : : {
2250 : 439 : Relation rootRel = mtstate->rootResultRelInfo->ri_RelationDesc;
2251 : : MemoryContext oldcxt;
2252 : :
2253 : : /* Things built here have to last for the query duration. */
2254 : 439 : oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
2255 : :
2256 : 439 : mtstate->mt_partition_tuple_routing =
2257 : 439 : ExecSetupPartitionTupleRouting(estate, rootRel);
2258 : :
2259 : : /*
2260 : : * Before a partition's tuple can be re-routed, it must first be
2261 : : * converted to the root's format, so we'll need a slot for storing
2262 : : * such tuples.
2263 : : */
2264 [ - + ]: 439 : Assert(mtstate->mt_root_tuple_slot == NULL);
2265 : 439 : mtstate->mt_root_tuple_slot = table_slot_create(rootRel, NULL);
2266 : :
2267 : 439 : MemoryContextSwitchTo(oldcxt);
2268 : : }
2269 : :
2270 : : /*
2271 : : * Row movement, part 1. Delete the tuple, but skip RETURNING processing.
2272 : : * We want to return rows from INSERT.
2273 : : */
1566 alvherre@alvh.no-ip. 2274 : 718 : ExecDelete(context, resultRelInfo,
2275 : : tupleid, oldtuple,
2276 : : false, /* processReturning */
2277 : : true, /* changingPart */
2278 : : false, /* canSetTag */
2279 : : tmresult, &tuple_deleted, &epqslot);
2280 : :
2281 : : /*
2282 : : * For some reason if DELETE didn't happen (e.g. trigger prevented it, or
2283 : : * it was already deleted by self, or it was concurrently deleted by
2284 : : * another transaction), then we should skip the insert as well;
2285 : : * otherwise, an UPDATE could cause an increase in the total number of
2286 : : * rows across all partitions, which is clearly wrong.
2287 : : *
2288 : : * For a normal UPDATE, the case where the tuple has been the subject of a
2289 : : * concurrent UPDATE or DELETE would be handled by the EvalPlanQual
2290 : : * machinery, but for an UPDATE that we've translated into a DELETE from
2291 : : * this partition and an INSERT into some other partition, that's not
2292 : : * available, because CTID chains can't span relation boundaries. We
2293 : : * mimic the semantics to a limited extent by skipping the INSERT if the
2294 : : * DELETE fails to find a tuple. This ensures that two concurrent
2295 : : * attempts to UPDATE the same tuple at the same time can't turn one tuple
2296 : : * into two, and that an UPDATE of a just-deleted tuple can't resurrect
2297 : : * it.
2298 : : */
2084 heikki.linnakangas@i 2299 [ + + ]: 715 : if (!tuple_deleted)
2300 : : {
2301 : : /*
2302 : : * epqslot will be typically NULL. But when ExecDelete() finds that
2303 : : * another transaction has concurrently updated the same row, it
2304 : : * re-fetches the row, skips the delete, and epqslot is set to the
2305 : : * re-fetched tuple slot. In that case, we need to do all the checks
2306 : : * again. For MERGE, we leave everything to the caller (it must do
2307 : : * additional rechecking, and might end up executing a different
2308 : : * action entirely).
2309 : : */
835 dean.a.rasheed@gmail 2310 [ + + ]: 49 : if (mtstate->operation == CMD_MERGE)
922 2311 : 23 : return *tmresult == TM_Ok;
1205 2312 [ + + - + ]: 26 : else if (TupIsNull(epqslot))
2084 heikki.linnakangas@i 2313 : 23 : return true;
2314 : : else
2315 : : {
2316 : : /* Fetch the most recent version of old tuple. */
2317 : : TupleTableSlot *oldSlot;
2318 : :
2319 : : /* ... but first, make sure ri_oldTupleSlot is initialized. */
810 akorotkov@postgresql 2320 [ - + ]: 3 : if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
810 akorotkov@postgresql 2321 :UBC 0 : ExecInitUpdateProjection(mtstate, resultRelInfo);
810 akorotkov@postgresql 2322 :CBC 3 : oldSlot = resultRelInfo->ri_oldTupleSlot;
2323 [ - + ]: 3 : if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc,
2324 : : tupleid,
2325 : : SnapshotAny,
2326 : : oldSlot))
810 akorotkov@postgresql 2327 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple being updated");
2328 : : /* and project the new tuple to retry the UPDATE with */
1205 dean.a.rasheed@gmail 2329 :CBC 3 : *retry_slot = ExecGetUpdateNewTuple(resultRelInfo, epqslot,
2330 : : oldSlot);
2084 heikki.linnakangas@i 2331 : 3 : return false;
2332 : : }
2333 : : }
2334 : :
2335 : : /*
2336 : : * resultRelInfo is one of the per-relation resultRelInfos. So we should
2337 : : * convert the tuple into root's tuple descriptor if needed, since
2338 : : * ExecInsert() starts the search from root.
2339 : : */
1911 tgl@sss.pgh.pa.us 2340 : 666 : tupconv_map = ExecGetChildToRootMap(resultRelInfo);
2084 heikki.linnakangas@i 2341 [ + + ]: 666 : if (tupconv_map != NULL)
2342 : 217 : slot = execute_attr_map_slot(tupconv_map->attrMap,
2343 : : slot,
2344 : : mtstate->mt_root_tuple_slot);
2345 : :
2346 : : /* Tuple routing starts from the root table. */
1566 alvherre@alvh.no-ip. 2347 : 583 : context->cpUpdateReturningSlot =
1563 2348 : 666 : ExecInsert(context, mtstate->rootResultRelInfo, slot, canSetTag,
2349 : : inserted_tuple, insert_destrel);
2350 : :
2351 : : /*
2352 : : * Reset the transition state that may possibly have been written by
2353 : : * INSERT.
2354 : : */
2084 heikki.linnakangas@i 2355 [ + + ]: 583 : if (mtstate->mt_transition_capture)
2356 : 36 : mtstate->mt_transition_capture->tcs_original_insert_tuple = NULL;
2357 : :
2358 : : /* We're done moving. */
2359 : 583 : return true;
2360 : : }
2361 : :
2362 : : /*
2363 : : * ExecUpdatePrologue -- subroutine for ExecUpdate
2364 : : *
2365 : : * Prepare executor state for UPDATE. This includes running BEFORE ROW
2366 : : * triggers. We return false if one of them makes the update a no-op;
2367 : : * otherwise, return true.
2368 : : */
2369 : : static bool
1566 alvherre@alvh.no-ip. 2370 : 2213450 : ExecUpdatePrologue(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
2371 : : ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
2372 : : TM_Result *result)
2373 : : {
2374 : 2213450 : Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
2375 : :
1205 dean.a.rasheed@gmail 2376 [ + + ]: 2213450 : if (result)
2377 : 1413 : *result = TM_Ok;
2378 : :
1566 alvherre@alvh.no-ip. 2379 : 2213450 : ExecMaterializeSlot(slot);
2380 : :
2381 : : /*
2382 : : * Open the table's indexes, if we have not done so already, so that we
2383 : : * can add new index entries for the updated tuple.
2384 : : */
2385 [ + + ]: 2213450 : if (resultRelationDesc->rd_rel->relhasindex &&
2386 [ + + ]: 146350 : resultRelInfo->ri_IndexRelationDescs == NULL)
2387 : 5774 : ExecOpenIndices(resultRelInfo, false);
2388 : :
2389 : : /* BEFORE ROW UPDATE triggers */
2390 [ + + ]: 2213450 : if (resultRelInfo->ri_TrigDesc &&
2391 [ + + ]: 4003 : resultRelInfo->ri_TrigDesc->trig_update_before_row)
2392 : : {
2393 : : /* Flush any pending inserts, so rows are visible to the triggers */
1313 efujita@postgresql.o 2394 [ + + ]: 1573 : if (context->estate->es_insert_pending_result_relations != NIL)
2395 : 1 : ExecPendingInserts(context->estate);
2396 : :
1566 alvherre@alvh.no-ip. 2397 : 1561 : return ExecBRUpdateTriggers(context->estate, context->epqstate,
2398 : : resultRelInfo, tupleid, oldtuple, slot,
2399 : : result, &context->tmfd,
347 dean.a.rasheed@gmail 2400 : 1573 : context->mtstate->operation == CMD_MERGE);
2401 : : }
2402 : :
1566 alvherre@alvh.no-ip. 2403 : 2211877 : return true;
2404 : : }
2405 : :
2406 : : /*
2407 : : * ExecUpdatePrepareSlot -- subroutine for ExecUpdateAct
2408 : : *
2409 : : * Apply the final modifications to the tuple slot before the update.
2410 : : * (This is split out because we also need it in the foreign-table code path.)
2411 : : */
2412 : : static void
2413 : 2213264 : ExecUpdatePrepareSlot(ResultRelInfo *resultRelInfo,
2414 : : TupleTableSlot *slot,
2415 : : EState *estate)
2416 : : {
2417 : 2213264 : Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
2418 : :
2419 : : /*
2420 : : * Constraints and GENERATED expressions might reference the tableoid
2421 : : * column, so (re-)initialize tts_tableOid before evaluating them.
2422 : : */
2423 : 2213264 : slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
2424 : :
2425 : : /*
2426 : : * Compute stored generated columns
2427 : : */
2428 [ + + ]: 2213264 : if (resultRelationDesc->rd_att->constr &&
2429 [ + + ]: 123401 : resultRelationDesc->rd_att->constr->has_generated_stored)
2430 : 198 : ExecComputeStoredGenerated(resultRelInfo, estate, slot,
2431 : : CMD_UPDATE);
2432 : 2213264 : }
2433 : :
2434 : : /*
2435 : : * ExecUpdateAct -- subroutine for ExecUpdate
2436 : : *
2437 : : * Actually update the tuple, when operating on a plain table. If the
2438 : : * table is a partition, and the command was called referencing an ancestor
2439 : : * partitioned table, this routine migrates the resulting tuple to another
2440 : : * partition.
2441 : : *
2442 : : * The caller is in charge of keeping indexes current as necessary. The
2443 : : * caller is also in charge of doing EvalPlanQual if the tuple is found to
2444 : : * be concurrently updated. However, in case of a cross-partition update,
2445 : : * this routine does it.
2446 : : */
2447 : : static TM_Result
2448 : 2213165 : ExecUpdateAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
2449 : : ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
2450 : : bool canSetTag, UpdateContext *updateCxt)
2451 : : {
2452 : 2213165 : EState *estate = context->estate;
2453 : 2213165 : Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
2454 : : bool partition_constraint_failed;
2455 : : TM_Result result;
2456 : :
2457 : 2213165 : updateCxt->crossPartUpdate = false;
2458 : :
2459 : : /*
2460 : : * If we move the tuple to a new partition, we loop back here to recompute
2461 : : * GENERATED values (which are allowed to be different across partitions)
2462 : : * and recheck any RLS policies and constraints. We do not fire any
2463 : : * BEFORE triggers of the new partition, however.
2464 : : */
1359 john.naylor@postgres 2465 : 2213168 : lreplace:
2466 : : /* Fill in GENERATEd columns */
1212 tgl@sss.pgh.pa.us 2467 : 2213168 : ExecUpdatePrepareSlot(resultRelInfo, slot, estate);
2468 : :
2469 : : /* ensure slot is independent, consider e.g. EPQ */
1566 alvherre@alvh.no-ip. 2470 : 2213168 : ExecMaterializeSlot(slot);
2471 : :
2472 : : /*
2473 : : * If partition constraint fails, this row might get moved to another
2474 : : * partition, in which case we should check the RLS CHECK policy just
2475 : : * before inserting into the new partition, rather than doing it here.
2476 : : * This is because a trigger on that partition might again change the row.
2477 : : * So skip the WCO checks if the partition constraint fails.
2478 : : */
2479 : 2213168 : partition_constraint_failed =
2480 [ + + ]: 2215006 : resultRelationDesc->rd_rel->relispartition &&
2481 [ + + ]: 1838 : !ExecPartitionCheck(resultRelInfo, slot, estate, false);
2482 : :
2483 : : /* Check any RLS UPDATE WITH CHECK policies */
2484 [ + + ]: 2213168 : if (!partition_constraint_failed &&
2485 [ + + ]: 2212418 : resultRelInfo->ri_WithCheckOptions != NIL)
2486 : : {
2487 : : /*
2488 : : * ExecWithCheckOptions() will skip any WCOs which are not of the kind
2489 : : * we are looking for at this point.
2490 : : */
2491 : 356 : ExecWithCheckOptions(WCO_RLS_UPDATE_CHECK,
2492 : : resultRelInfo, slot, estate);
2493 : : }
2494 : :
2495 : : /*
2496 : : * If a partition check failed, try to move the row into the right
2497 : : * partition.
2498 : : */
2499 [ + + ]: 2213132 : if (partition_constraint_failed)
2500 : : {
2501 : : TupleTableSlot *inserted_tuple,
2502 : : *retry_slot;
1563 2503 : 750 : ResultRelInfo *insert_destrel = NULL;
2504 : :
2505 : : /*
2506 : : * ExecCrossPartitionUpdate will first DELETE the row from the
2507 : : * partition it's currently in and then insert it back into the root
2508 : : * table, which will re-route it to the correct partition. However,
2509 : : * if the tuple has been concurrently updated, a retry is needed.
2510 : : */
1566 2511 [ + + ]: 750 : if (ExecCrossPartitionUpdate(context, resultRelInfo,
2512 : : tupleid, oldtuple, slot,
2513 : : canSetTag, updateCxt,
2514 : : &result,
2515 : : &retry_slot,
2516 : : &inserted_tuple,
2517 : : &insert_destrel))
2518 : : {
2519 : : /* success! */
2520 : 622 : updateCxt->crossPartUpdate = true;
2521 : :
2522 : : /*
2523 : : * If the partitioned table being updated is referenced in foreign
2524 : : * keys, queue up trigger events to check that none of them were
2525 : : * violated. No special treatment is needed in
2526 : : * non-cross-partition update situations, because the leaf
2527 : : * partition's AR update triggers will take care of that. During
2528 : : * cross-partition updates implemented as delete on the source
2529 : : * partition followed by insert on the destination partition,
2530 : : * AR-UPDATE triggers of the root table (that is, the table
2531 : : * mentioned in the query) must be fired.
2532 : : *
2533 : : * NULL insert_destrel means that the move failed to occur, that
2534 : : * is, the update failed, so no need to anything in that case.
2535 : : */
1563 2536 [ + + ]: 622 : if (insert_destrel &&
2537 [ + + ]: 565 : resultRelInfo->ri_TrigDesc &&
2538 [ + + ]: 242 : resultRelInfo->ri_TrigDesc->trig_update_after_row)
2539 : 202 : ExecCrossPartitionUpdateForeignKey(context,
2540 : : resultRelInfo,
2541 : : insert_destrel,
2542 : : tupleid, slot,
2543 : : inserted_tuple);
2544 : :
1566 2545 : 625 : return TM_Ok;
2546 : : }
2547 : :
2548 : : /*
2549 : : * No luck, a retry is needed. If running MERGE, we do not do so
2550 : : * here; instead let it handle that on its own rules.
2551 : : */
835 dean.a.rasheed@gmail 2552 [ + + ]: 10 : if (context->mtstate->operation == CMD_MERGE)
922 2553 : 7 : return result;
2554 : :
2555 : : /*
2556 : : * ExecCrossPartitionUpdate installed an updated version of the new
2557 : : * tuple in the retry slot; start over.
2558 : : */
1205 2559 : 3 : slot = retry_slot;
1566 alvherre@alvh.no-ip. 2560 : 3 : goto lreplace;
2561 : : }
2562 : :
2563 : : /*
2564 : : * Check the constraints of the tuple. We've already checked the
2565 : : * partition constraint above; however, we must still ensure the tuple
2566 : : * passes all other constraints, so we will call ExecConstraints() and
2567 : : * have it validate all remaining checks.
2568 : : */
2569 [ + + ]: 2212382 : if (resultRelationDesc->rd_att->constr)
2570 : 122995 : ExecConstraints(resultRelInfo, slot, estate);
2571 : :
2572 : : /*
2573 : : * replace the heap tuple
2574 : : *
2575 : : * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
2576 : : * the row to be updated is visible to that snapshot, and throw a
2577 : : * can't-serialize error if not. This is a special-case behavior needed
2578 : : * for referential integrity updates in transaction-snapshot mode
2579 : : * transactions.
2580 : : */
2581 : 2212326 : result = table_tuple_update(resultRelationDesc, tupleid, slot,
2582 : : estate->es_output_cid,
2583 : : 0,
2584 : : estate->es_snapshot,
2585 : : estate->es_crosscheck_snapshot,
2586 : : true /* wait for commit */ ,
2587 : : &context->tmfd, &updateCxt->lockmode,
2588 : : &updateCxt->updateIndexes);
2589 : :
2590 : 2212314 : return result;
2591 : : }
2592 : :
2593 : : /*
2594 : : * ExecUpdateEpilogue -- subroutine for ExecUpdate
2595 : : *
2596 : : * Closing steps of updating a tuple. Must be called if ExecUpdateAct
2597 : : * returns indicating that the tuple was updated. It also inserts temporal
2598 : : * leftovers from an UPDATE FOR PORTION OF.
2599 : : */
2600 : : static void
2601 : 2212316 : ExecUpdateEpilogue(ModifyTableContext *context, UpdateContext *updateCxt,
2602 : : ResultRelInfo *resultRelInfo, ItemPointer tupleid,
2603 : : HeapTuple oldtuple, TupleTableSlot *slot)
2604 : : {
2605 : 2212316 : ModifyTableState *mtstate = context->mtstate;
1205 dean.a.rasheed@gmail 2606 : 2212316 : List *recheckIndexes = NIL;
2607 : :
2608 : : /* insert index entries for tuple if necessary */
1198 tomas.vondra@postgre 2609 [ + + + + ]: 2212316 : if (resultRelInfo->ri_NumIndices > 0 && (updateCxt->updateIndexes != TU_None))
2610 : : {
92 nathan@postgresql.or 2611 :GNC 115918 : uint32 flags = EIIT_IS_UPDATE;
2612 : :
133 alvherre@kurilemu.de 2613 [ + + ]: 115918 : if (updateCxt->updateIndexes == TU_Summarizing)
2614 : 2188 : flags |= EIIT_ONLY_SUMMARIZING;
2615 : 115918 : recheckIndexes = ExecInsertIndexTuples(resultRelInfo, context->estate,
2616 : : flags, slot, NIL,
2617 : : NULL);
2618 : : }
2619 : :
2620 : : /* Compute temporal leftovers in FOR PORTION OF */
90 peter@eisentraut.org 2621 [ + + ]: 2212255 : if (((ModifyTable *) context->mtstate->ps.plan)->forPortionOf)
2622 : 471 : ExecForPortionOfLeftovers(context, context->estate, resultRelInfo, tupleid);
2623 : :
2624 : : /* AFTER ROW UPDATE Triggers */
1566 alvherre@alvh.no-ip. 2625 :CBC 2212239 : ExecARUpdateTriggers(context->estate, resultRelInfo,
2626 : : NULL, NULL,
2627 : : tupleid, oldtuple, slot,
2628 : : recheckIndexes,
2629 [ + + ]: 2212239 : mtstate->operation == CMD_INSERT ?
2630 : : mtstate->mt_oc_transition_capture :
2631 : : mtstate->mt_transition_capture,
2632 : : false);
2633 : :
1205 dean.a.rasheed@gmail 2634 : 2212237 : list_free(recheckIndexes);
2635 : :
2636 : : /*
2637 : : * Check any WITH CHECK OPTION constraints from parent views. We are
2638 : : * required to do this after testing all constraints and uniqueness
2639 : : * violations per the SQL spec, so we do it after actually updating the
2640 : : * record in the heap and all indexes.
2641 : : *
2642 : : * ExecWithCheckOptions() will skip any WCOs which are not of the kind we
2643 : : * are looking for at this point.
2644 : : */
1566 alvherre@alvh.no-ip. 2645 [ + + ]: 2212237 : if (resultRelInfo->ri_WithCheckOptions != NIL)
2646 : 337 : ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo,
2647 : : slot, context->estate);
2648 : 2212183 : }
2649 : :
2650 : : /*
2651 : : * Queues up an update event using the target root partitioned table's
2652 : : * trigger to check that a cross-partition update hasn't broken any foreign
2653 : : * keys pointing into it.
2654 : : */
2655 : : static void
1563 2656 : 202 : ExecCrossPartitionUpdateForeignKey(ModifyTableContext *context,
2657 : : ResultRelInfo *sourcePartInfo,
2658 : : ResultRelInfo *destPartInfo,
2659 : : ItemPointer tupleid,
2660 : : TupleTableSlot *oldslot,
2661 : : TupleTableSlot *newslot)
2662 : : {
2663 : : ListCell *lc;
2664 : : ResultRelInfo *rootRelInfo;
2665 : : List *ancestorRels;
2666 : :
2667 : 202 : rootRelInfo = sourcePartInfo->ri_RootResultRelInfo;
2668 : 202 : ancestorRels = ExecGetAncestorResultRels(context->estate, sourcePartInfo);
2669 : :
2670 : : /*
2671 : : * For any foreign keys that point directly into a non-root ancestors of
2672 : : * the source partition, we can in theory fire an update event to enforce
2673 : : * those constraints using their triggers, if we could tell that both the
2674 : : * source and the destination partitions are under the same ancestor. But
2675 : : * for now, we simply report an error that those cannot be enforced.
2676 : : */
2677 [ + - + + : 440 : foreach(lc, ancestorRels)
+ + ]
2678 : : {
2679 : 242 : ResultRelInfo *rInfo = lfirst(lc);
2680 : 242 : TriggerDesc *trigdesc = rInfo->ri_TrigDesc;
2681 : 242 : bool has_noncloned_fkey = false;
2682 : :
2683 : : /* Root ancestor's triggers will be processed. */
2684 [ + + ]: 242 : if (rInfo == rootRelInfo)
2685 : 198 : continue;
2686 : :
2687 [ + - + - ]: 44 : if (trigdesc && trigdesc->trig_update_after_row)
2688 : : {
2689 [ + + ]: 152 : for (int i = 0; i < trigdesc->numtriggers; i++)
2690 : : {
2691 : 112 : Trigger *trig = &trigdesc->triggers[i];
2692 : :
2693 [ + + + - ]: 116 : if (!trig->tgisclone &&
2694 : 4 : RI_FKey_trigger_type(trig->tgfoid) == RI_TRIGGER_PK)
2695 : : {
2696 : 4 : has_noncloned_fkey = true;
2697 : 4 : break;
2698 : : }
2699 : : }
2700 : : }
2701 : :
2702 [ + + ]: 44 : if (has_noncloned_fkey)
2703 [ + - ]: 4 : ereport(ERROR,
2704 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2705 : : errmsg("cannot move tuple across partitions when a non-root ancestor of the source partition is directly referenced in a foreign key"),
2706 : : errdetail("A foreign key points to ancestor \"%s\" but not the root ancestor \"%s\".",
2707 : : RelationGetRelationName(rInfo->ri_RelationDesc),
2708 : : RelationGetRelationName(rootRelInfo->ri_RelationDesc)),
2709 : : errhint("Consider defining the foreign key on table \"%s\".",
2710 : : RelationGetRelationName(rootRelInfo->ri_RelationDesc))));
2711 : : }
2712 : :
2713 : : /* Perform the root table's triggers. */
2714 : 198 : ExecARUpdateTriggers(context->estate,
2715 : : rootRelInfo, sourcePartInfo, destPartInfo,
2716 : : tupleid, NULL, newslot, NIL, NULL, true);
2717 : 198 : }
2718 : :
2719 : : /* ----------------------------------------------------------------
2720 : : * ExecUpdate
2721 : : *
2722 : : * note: we can't run UPDATE queries with transactions
2723 : : * off because UPDATEs are actually INSERTs and our
2724 : : * scan will mistakenly loop forever, updating the tuple
2725 : : * it just inserted.. This should be fixed but until it
2726 : : * is, we don't want to get stuck in an infinite loop
2727 : : * which corrupts your database..
2728 : : *
2729 : : * When updating a table, tupleid identifies the tuple to update and
2730 : : * oldtuple is NULL. When updating through a view INSTEAD OF trigger,
2731 : : * oldtuple is passed to the triggers and identifies what to update, and
2732 : : * tupleid is invalid. When updating a foreign table, tupleid is
2733 : : * invalid; the FDW has to figure out which row to update using data from
2734 : : * the planSlot. oldtuple is passed to foreign table triggers; it is
2735 : : * NULL when the foreign table has no relevant triggers.
2736 : : *
2737 : : * oldSlot contains the old tuple value.
2738 : : * slot contains the new tuple value to be stored.
2739 : : * planSlot is the output of the ModifyTable's subplan; we use it
2740 : : * to access values from other input tables (for RETURNING),
2741 : : * row-ID junk columns, etc.
2742 : : *
2743 : : * Returns RETURNING result if any, otherwise NULL. On exit, if tupleid
2744 : : * had identified the tuple to update, it will identify the tuple
2745 : : * actually updated after EvalPlanQual.
2746 : : * ----------------------------------------------------------------
2747 : : */
2748 : : static TupleTableSlot *
1566 2749 : 2212037 : ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
2750 : : ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *oldSlot,
2751 : : TupleTableSlot *slot, bool canSetTag)
2752 : : {
2753 : 2212037 : EState *estate = context->estate;
2085 heikki.linnakangas@i 2754 : 2212037 : Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
1566 alvherre@alvh.no-ip. 2755 : 2212037 : UpdateContext updateCxt = {0};
2756 : : TM_Result result;
2757 : :
2758 : : /*
2759 : : * abort the operation if not running transactions
2760 : : */
6107 tgl@sss.pgh.pa.us 2761 [ - + ]: 2212037 : if (IsBootstrapProcessingMode())
6107 tgl@sss.pgh.pa.us 2762 [ # # ]:UBC 0 : elog(ERROR, "cannot UPDATE during bootstrap");
2763 : :
2764 : : /*
2765 : : * Prepare for the update. This includes BEFORE ROW triggers, so we're
2766 : : * done if it says we are.
2767 : : */
1205 dean.a.rasheed@gmail 2768 [ + + ]:CBC 2212037 : if (!ExecUpdatePrologue(context, resultRelInfo, tupleid, oldtuple, slot, NULL))
1566 alvherre@alvh.no-ip. 2769 : 85 : return NULL;
2770 : :
2771 : : /* INSTEAD OF ROW UPDATE Triggers */
5742 tgl@sss.pgh.pa.us 2772 [ + + ]: 2211940 : if (resultRelInfo->ri_TrigDesc &&
2773 [ + + ]: 3665 : resultRelInfo->ri_TrigDesc->trig_update_instead_row)
2774 : : {
2681 andres@anarazel.de 2775 [ + + ]: 83 : if (!ExecIRUpdateTriggers(estate, resultRelInfo,
2776 : : oldtuple, slot))
2596 tgl@sss.pgh.pa.us 2777 : 12 : return NULL; /* "do nothing" */
2778 : : }
4860 2779 [ + + ]: 2211857 : else if (resultRelInfo->ri_FdwRoutine)
2780 : : {
2781 : : /* Fill in GENERATEd columns */
1566 alvherre@alvh.no-ip. 2782 : 96 : ExecUpdatePrepareSlot(resultRelInfo, slot, estate);
2783 : :
2784 : : /*
2785 : : * update in foreign table: let the FDW do it
2786 : : */
4860 tgl@sss.pgh.pa.us 2787 : 96 : slot = resultRelInfo->ri_FdwRoutine->ExecForeignUpdate(estate,
2788 : : resultRelInfo,
2789 : : slot,
2790 : : context->planSlot);
2791 : :
2792 [ + + ]: 96 : if (slot == NULL) /* "do nothing" */
2793 : 1 : return NULL;
2794 : :
2795 : : /*
2796 : : * AFTER ROW Triggers or RETURNING expressions might reference the
2797 : : * tableoid column, so (re-)initialize tts_tableOid before evaluating
2798 : : * them. (This covers the case where the FDW replaced the slot.)
2799 : : */
2681 andres@anarazel.de 2800 : 95 : slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
2801 : : }
2802 : : else
2803 : : {
2804 : : ItemPointerData lockedtid;
2805 : :
2806 : : /*
2807 : : * If we generate a new candidate tuple after EvalPlanQual testing, we
2808 : : * must loop back here to try again. (We don't need to redo triggers,
2809 : : * however. If there are any BEFORE triggers then trigger.c will have
2810 : : * done table_tuple_lock to lock the correct tuple, so there's no need
2811 : : * to do them again.)
2812 : : */
1566 alvherre@alvh.no-ip. 2813 : 2211761 : redo_act:
644 noah@leadboat.com 2814 : 2211815 : lockedtid = *tupleid;
1566 alvherre@alvh.no-ip. 2815 : 2211815 : result = ExecUpdateAct(context, resultRelInfo, tupleid, oldtuple, slot,
2816 : : canSetTag, &updateCxt);
2817 : :
2818 : : /*
2819 : : * If ExecUpdateAct reports that a cross-partition update was done,
2820 : : * then the RETURNING tuple (if any) has been projected and there's
2821 : : * nothing else for us to do.
2822 : : */
2823 [ + + ]: 2211604 : if (updateCxt.crossPartUpdate)
2824 : 616 : return context->cpUpdateReturningSlot;
2825 : :
5742 tgl@sss.pgh.pa.us 2826 [ + + + + : 2211075 : switch (result)
- ]
2827 : : {
2656 andres@anarazel.de 2828 : 60 : case TM_SelfModified:
2829 : :
2830 : : /*
2831 : : * The target tuple was already updated or deleted by the
2832 : : * current command, or by a later command in the current
2833 : : * transaction. The former case is possible in a join UPDATE
2834 : : * where multiple tuples join to the same target tuple. This
2835 : : * is pretty questionable, but Postgres has always allowed it:
2836 : : * we just execute the first update action and ignore
2837 : : * additional update attempts.
2838 : : *
2839 : : * The latter case arises if the tuple is modified by a
2840 : : * command in a BEFORE trigger, or perhaps by a command in a
2841 : : * volatile function used in the query. In such situations we
2842 : : * should not ignore the update, but it is equally unsafe to
2843 : : * proceed. We don't want to discard the original UPDATE
2844 : : * while keeping the triggered actions based on it; and we
2845 : : * have no principled way to merge this update with the
2846 : : * previous ones. So throwing an error is the only safe
2847 : : * course.
2848 : : *
2849 : : * If a trigger actually intends this type of interaction, it
2850 : : * can re-execute the UPDATE (assuming it can figure out how)
2851 : : * and then return NULL to cancel the outer update.
2852 : : */
1566 alvherre@alvh.no-ip. 2853 [ + + ]: 60 : if (context->tmfd.cmax != estate->es_output_cid)
4995 kgrittn@postgresql.o 2854 [ + - ]: 4 : ereport(ERROR,
2855 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
2856 : : errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
2857 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
2858 : :
2859 : : /* Else, already updated by self; nothing to do */
5742 tgl@sss.pgh.pa.us 2860 : 56 : return NULL;
2861 : :
2656 andres@anarazel.de 2862 : 2210907 : case TM_Ok:
5742 tgl@sss.pgh.pa.us 2863 : 2210907 : break;
2864 : :
2656 andres@anarazel.de 2865 : 92 : case TM_Updated:
2866 : : {
2867 : : TupleTableSlot *inputslot;
2868 : : TupleTableSlot *epqslot;
2869 : :
2870 [ + + ]: 92 : if (IsolationUsesXactSnapshot())
2871 [ + - ]: 11 : ereport(ERROR,
2872 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2873 : : errmsg("could not serialize access due to concurrent update")));
2874 : :
2875 : : /*
2876 : : * Already know that we're going to need to do EPQ, so
2877 : : * fetch tuple directly into the right slot.
2878 : : */
810 akorotkov@postgresql 2879 : 81 : inputslot = EvalPlanQualSlot(context->epqstate, resultRelationDesc,
2880 : : resultRelInfo->ri_RangeTableIndex);
2881 : :
2882 : 81 : result = table_tuple_lock(resultRelationDesc, tupleid,
2883 : : estate->es_snapshot,
2884 : : inputslot, estate->es_output_cid,
2885 : : updateCxt.lockmode, LockWaitBlock,
2886 : : TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
2887 : : &context->tmfd);
2888 : :
2889 [ + + + - ]: 79 : switch (result)
2890 : : {
2891 : 74 : case TM_Ok:
2892 [ - + ]: 74 : Assert(context->tmfd.traversed);
2893 : :
2894 : 74 : epqslot = EvalPlanQual(context->epqstate,
2895 : : resultRelationDesc,
2896 : : resultRelInfo->ri_RangeTableIndex,
2897 : : inputslot);
2898 [ + + + + ]: 74 : if (TupIsNull(epqslot))
2899 : : /* Tuple not passing quals anymore, exiting... */
2900 : 20 : return NULL;
2901 : :
2902 : : /* Make sure ri_oldTupleSlot is initialized. */
2903 [ - + ]: 54 : if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
810 akorotkov@postgresql 2904 :UBC 0 : ExecInitUpdateProjection(context->mtstate,
2905 : : resultRelInfo);
2906 : :
644 noah@leadboat.com 2907 [ + + ]:CBC 54 : if (resultRelInfo->ri_needLockTagTuple)
2908 : : {
2909 : 1 : UnlockTuple(resultRelationDesc,
2910 : : &lockedtid, InplaceUpdateTupleLock);
2911 : 1 : LockTuple(resultRelationDesc,
2912 : : tupleid, InplaceUpdateTupleLock);
2913 : : }
2914 : :
2915 : : /* Fetch the most recent version of old tuple. */
810 akorotkov@postgresql 2916 : 54 : oldSlot = resultRelInfo->ri_oldTupleSlot;
2917 [ - + ]: 54 : if (!table_tuple_fetch_row_version(resultRelationDesc,
2918 : : tupleid,
2919 : : SnapshotAny,
2920 : : oldSlot))
810 akorotkov@postgresql 2921 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple being updated");
810 akorotkov@postgresql 2922 :CBC 54 : slot = ExecGetUpdateNewTuple(resultRelInfo,
2923 : : epqslot, oldSlot);
2924 : 54 : goto redo_act;
2925 : :
2926 : 1 : case TM_Deleted:
2927 : : /* tuple already deleted; nothing to do */
2928 : 1 : return NULL;
2929 : :
2930 : 4 : case TM_SelfModified:
2931 : :
2932 : : /*
2933 : : * This can be reached when following an update
2934 : : * chain from a tuple updated by another session,
2935 : : * reaching a tuple that was already updated in
2936 : : * this transaction. If previously modified by
2937 : : * this command, ignore the redundant update,
2938 : : * otherwise error out.
2939 : : *
2940 : : * See also TM_SelfModified response to
2941 : : * table_tuple_update() above.
2942 : : */
2943 [ + + ]: 4 : if (context->tmfd.cmax != estate->es_output_cid)
2944 [ + - ]: 1 : ereport(ERROR,
2945 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
2946 : : errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
2947 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
2948 : 3 : return NULL;
2949 : :
810 akorotkov@postgresql 2950 :UBC 0 : default:
2951 : : /* see table_tuple_lock call in ExecDelete() */
2952 [ # # ]: 0 : elog(ERROR, "unexpected table_tuple_lock status: %u",
2953 : : result);
2954 : : return NULL;
2955 : : }
2956 : : }
2957 : :
2958 : : break;
2959 : :
2656 andres@anarazel.de 2960 :CBC 16 : case TM_Deleted:
2961 [ + + ]: 16 : if (IsolationUsesXactSnapshot())
2656 andres@anarazel.de 2962 [ + - ]:GBC 9 : ereport(ERROR,
2963 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2964 : : errmsg("could not serialize access due to concurrent delete")));
2965 : : /* tuple already deleted; nothing to do */
5742 tgl@sss.pgh.pa.us 2966 :CBC 7 : return NULL;
2967 : :
5742 tgl@sss.pgh.pa.us 2968 :UBC 0 : default:
2595 andres@anarazel.de 2969 [ # # ]: 0 : elog(ERROR, "unrecognized table_tuple_update status: %u",
2970 : : result);
2971 : : return NULL;
2972 : : }
2973 : : }
2974 : :
5604 tgl@sss.pgh.pa.us 2975 [ + + ]:CBC 2211065 : if (canSetTag)
2976 : 2210648 : (estate->es_processed)++;
2977 : :
1566 alvherre@alvh.no-ip. 2978 : 2211065 : ExecUpdateEpilogue(context, &updateCxt, resultRelInfo, tupleid, oldtuple,
2979 : : slot);
2980 : :
2981 : : /* Process RETURNING if present */
6107 tgl@sss.pgh.pa.us 2982 [ + + ]: 2210940 : if (resultRelInfo->ri_projectReturning)
138 dean.a.rasheed@gmail 2983 :GNC 1516 : return ExecProcessReturning(context, resultRelInfo, false,
2984 : : oldSlot, slot, context->planSlot);
2985 : :
6107 tgl@sss.pgh.pa.us 2986 :CBC 2209424 : return NULL;
2987 : : }
2988 : :
2989 : : /*
2990 : : * ExecOnConflictLockRow --- lock the row for ON CONFLICT DO SELECT/UPDATE
2991 : : *
2992 : : * Try to lock tuple for update as part of speculative insertion for ON
2993 : : * CONFLICT DO UPDATE or ON CONFLICT DO SELECT FOR UPDATE/SHARE.
2994 : : *
2995 : : * Returns true if the row is successfully locked, or false if the caller must
2996 : : * retry the INSERT from scratch.
2997 : : */
2998 : : static bool
138 dean.a.rasheed@gmail 2999 :GNC 2831 : ExecOnConflictLockRow(ModifyTableContext *context,
3000 : : TupleTableSlot *existing,
3001 : : ItemPointer conflictTid,
3002 : : Relation relation,
3003 : : LockTupleMode lockmode,
3004 : : bool isUpdate)
3005 : : {
3006 : : TM_FailureData tmfd;
3007 : : TM_Result test;
3008 : : Datum xminDatum;
3009 : : TransactionId xmin;
3010 : : bool isnull;
3011 : :
3012 : : /*
3013 : : * Lock tuple with lockmode. Don't follow updates when tuple cannot be
3014 : : * locked without doing so. A row locking conflict here means our
3015 : : * previous conclusion that the tuple is conclusively committed is not
3016 : : * true anymore.
3017 : : */
2595 andres@anarazel.de 3018 :CBC 2831 : test = table_tuple_lock(relation, conflictTid,
1566 alvherre@alvh.no-ip. 3019 : 2831 : context->estate->es_snapshot,
3020 : 2831 : existing, context->estate->es_output_cid,
3021 : : lockmode, LockWaitBlock, 0,
3022 : : &tmfd);
4071 andres@anarazel.de 3023 [ + + - + : 2831 : switch (test)
+ - ]
3024 : : {
2656 3025 : 2800 : case TM_Ok:
3026 : : /* success! */
4071 3027 : 2800 : break;
3028 : :
2656 3029 : 28 : case TM_Invisible:
3030 : :
3031 : : /*
3032 : : * This can occur when a just inserted tuple is updated again in
3033 : : * the same command. E.g. because multiple rows with the same
3034 : : * conflicting key values are inserted.
3035 : : *
3036 : : * This is somewhat similar to the ExecUpdate() TM_SelfModified
3037 : : * case. We do not want to proceed because it would lead to the
3038 : : * same row being updated a second time in some unspecified order,
3039 : : * and in contrast to plain UPDATEs there's no historical behavior
3040 : : * to break.
3041 : : *
3042 : : * It is the user's responsibility to prevent this situation from
3043 : : * occurring. These problems are why the SQL standard similarly
3044 : : * specifies that for SQL MERGE, an exception must be raised in
3045 : : * the event of an attempt to update the same row twice.
3046 : : */
3047 : 28 : xminDatum = slot_getsysattr(existing,
3048 : : MinTransactionIdAttributeNumber,
3049 : : &isnull);
3050 [ - + ]: 28 : Assert(!isnull);
3051 : 28 : xmin = DatumGetTransactionId(xminDatum);
3052 : :
3053 [ + - ]: 28 : if (TransactionIdIsCurrentTransactionId(xmin))
4071 3054 [ + - + + ]: 28 : ereport(ERROR,
3055 : : (errcode(ERRCODE_CARDINALITY_VIOLATION),
3056 : : /* translator: %s is a SQL command name */
3057 : : errmsg("%s command cannot affect row a second time",
3058 : : isUpdate ? "ON CONFLICT DO UPDATE" : "ON CONFLICT DO SELECT"),
3059 : : errhint("Ensure that no rows proposed for insertion within the same command have duplicate constrained values.")));
3060 : :
3061 : : /* This shouldn't happen */
4071 andres@anarazel.de 3062 [ # # ]:UBC 0 : elog(ERROR, "attempted to lock invisible tuple");
3063 : : break;
3064 : :
2656 3065 : 0 : case TM_SelfModified:
3066 : :
3067 : : /*
3068 : : * This state should never be reached. As a dirty snapshot is used
3069 : : * to find conflicting tuples, speculative insertion wouldn't have
3070 : : * seen this row to conflict with.
3071 : : */
4071 3072 [ # # ]: 0 : elog(ERROR, "unexpected self-updated tuple");
3073 : : break;
3074 : :
2656 andres@anarazel.de 3075 :CBC 2 : case TM_Updated:
4071 3076 [ - + ]: 2 : if (IsolationUsesXactSnapshot())
4071 andres@anarazel.de 3077 [ # # ]:UBC 0 : ereport(ERROR,
3078 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3079 : : errmsg("could not serialize access due to concurrent update")));
3080 : :
3081 : : /*
3082 : : * Tell caller to try again from the very start.
3083 : : *
3084 : : * It does not make sense to use the usual EvalPlanQual() style
3085 : : * loop here, as the new version of the row might not conflict
3086 : : * anymore, or the conflicting tuple has actually been deleted.
3087 : : */
2656 andres@anarazel.de 3088 :CBC 2 : ExecClearTuple(existing);
3089 : 2 : return false;
3090 : :
3091 : 1 : case TM_Deleted:
3092 [ - + ]: 1 : if (IsolationUsesXactSnapshot())
2656 andres@anarazel.de 3093 [ # # ]:UBC 0 : ereport(ERROR,
3094 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3095 : : errmsg("could not serialize access due to concurrent delete")));
3096 : :
3097 : : /* see TM_Updated case */
2656 andres@anarazel.de 3098 :CBC 1 : ExecClearTuple(existing);
4071 3099 : 1 : return false;
3100 : :
4071 andres@anarazel.de 3101 :UBC 0 : default:
2595 3102 [ # # ]: 0 : elog(ERROR, "unrecognized table_tuple_lock status: %u", test);
3103 : : }
3104 : :
3105 : : /* Success, the tuple is locked. */
138 dean.a.rasheed@gmail 3106 :GNC 2800 : return true;
3107 : : }
3108 : :
3109 : : /*
3110 : : * ExecOnConflictUpdate --- execute UPDATE of INSERT ON CONFLICT DO UPDATE
3111 : : *
3112 : : * Try to lock tuple for update as part of speculative insertion. If
3113 : : * a qual originating from ON CONFLICT DO UPDATE is satisfied, update
3114 : : * (but still lock row, even though it may not satisfy estate's
3115 : : * snapshot).
3116 : : *
3117 : : * Returns true if we're done (with or without an update), or false if
3118 : : * the caller must retry the INSERT from scratch.
3119 : : */
3120 : : static bool
3121 : 2761 : ExecOnConflictUpdate(ModifyTableContext *context,
3122 : : ResultRelInfo *resultRelInfo,
3123 : : ItemPointer conflictTid,
3124 : : TupleTableSlot *excludedSlot,
3125 : : bool canSetTag,
3126 : : TupleTableSlot **returning)
3127 : : {
3128 : 2761 : ModifyTableState *mtstate = context->mtstate;
3129 : 2761 : ExprContext *econtext = mtstate->ps.ps_ExprContext;
3130 : 2761 : Relation relation = resultRelInfo->ri_RelationDesc;
3131 : 2761 : ExprState *onConflictSetWhere = resultRelInfo->ri_onConflict->oc_WhereClause;
3132 : 2761 : TupleTableSlot *existing = resultRelInfo->ri_onConflict->oc_Existing;
3133 : : LockTupleMode lockmode;
3134 : :
3135 : : /*
3136 : : * Parse analysis should have blocked ON CONFLICT for all system
3137 : : * relations, which includes these. There's no fundamental obstacle to
3138 : : * supporting this; we'd just need to handle LOCKTAG_TUPLE like the other
3139 : : * ExecUpdate() caller.
3140 : : */
3141 [ - + ]: 2761 : Assert(!resultRelInfo->ri_needLockTagTuple);
3142 : :
3143 : : /* Determine lock mode to use */
3144 : 2761 : lockmode = ExecUpdateLockMode(context->estate, resultRelInfo);
3145 : :
3146 : : /* Lock tuple for update */
3147 [ + + ]: 2761 : if (!ExecOnConflictLockRow(context, existing, conflictTid,
3148 : : resultRelInfo->ri_RelationDesc, lockmode, true))
3149 : 3 : return false;
3150 : :
3151 : : /*
3152 : : * Verify that the tuple is visible to our MVCC snapshot if the current
3153 : : * isolation level mandates that.
3154 : : *
3155 : : * It's not sufficient to rely on the check within ExecUpdate() as e.g.
3156 : : * CONFLICT ... WHERE clause may prevent us from reaching that.
3157 : : *
3158 : : * This means we only ever continue when a new command in the current
3159 : : * transaction could see the row, even though in READ COMMITTED mode the
3160 : : * tuple will not be visible according to the current statement's
3161 : : * snapshot. This is in line with the way UPDATE deals with newer tuple
3162 : : * versions.
3163 : : */
1566 alvherre@alvh.no-ip. 3164 :CBC 2742 : ExecCheckTupleVisible(context->estate, relation, existing);
3165 : :
3166 : : /*
3167 : : * Make tuple and any needed join variables available to ExecQual and
3168 : : * ExecProject. The EXCLUDED tuple is installed in ecxt_innertuple, while
3169 : : * the target's existing tuple is installed in the scantuple. EXCLUDED
3170 : : * has been made to reference INNER_VAR in setrefs.c, but there is no
3171 : : * other redirection.
3172 : : */
2673 andres@anarazel.de 3173 : 2742 : econtext->ecxt_scantuple = existing;
4071 3174 : 2742 : econtext->ecxt_innertuple = excludedSlot;
3175 : 2742 : econtext->ecxt_outertuple = NULL;
3176 : :
3395 3177 [ + + ]: 2742 : if (!ExecQual(onConflictSetWhere, econtext))
3178 : : {
2673 3179 : 21 : ExecClearTuple(existing); /* see return below */
4071 3180 [ - + ]: 21 : InstrCountFiltered1(&mtstate->ps, 1);
3181 : 21 : return true; /* done with the tuple */
3182 : : }
3183 : :
3184 [ + + ]: 2721 : if (resultRelInfo->ri_WithCheckOptions != NIL)
3185 : : {
3186 : : /*
3187 : : * Check target's existing tuple against UPDATE-applicable USING
3188 : : * security barrier quals (if any), enforced here as RLS checks/WCOs.
3189 : : *
3190 : : * The rewriter creates UPDATE RLS checks/WCOs for UPDATE security
3191 : : * quals, and stores them as WCOs of "kind" WCO_RLS_CONFLICT_CHECK.
3192 : : * Since SELECT permission on the target table is always required for
3193 : : * INSERT ... ON CONFLICT DO UPDATE, the rewriter also adds SELECT RLS
3194 : : * checks/WCOs for SELECT security quals, using WCOs of the same kind,
3195 : : * and this check enforces them too.
3196 : : *
3197 : : * The rewriter will also have associated UPDATE-applicable straight
3198 : : * RLS checks/WCOs for the benefit of the ExecUpdate() call that
3199 : : * follows. INSERTs and UPDATEs naturally have mutually exclusive WCO
3200 : : * kinds, so there is no danger of spurious over-enforcement in the
3201 : : * INSERT or UPDATE path.
3202 : : */
3203 : 48 : ExecWithCheckOptions(WCO_RLS_CONFLICT_CHECK, resultRelInfo,
3204 : : existing,
3205 : : mtstate->ps.state);
3206 : : }
3207 : :
3208 : : /* Project the new tuple version */
3018 alvherre@alvh.no-ip. 3209 : 2705 : ExecProject(resultRelInfo->ri_onConflict->oc_ProjInfo);
3210 : :
3211 : : /*
3212 : : * Note that it is possible that the target tuple has been modified in
3213 : : * this session, after the above table_tuple_lock. We choose to not error
3214 : : * out in that case, in line with ExecUpdate's treatment of similar cases.
3215 : : * This can happen if an UPDATE is triggered from within ExecQual(),
3216 : : * ExecWithCheckOptions() or ExecProject() above, e.g. by selecting from a
3217 : : * wCTE in the ON CONFLICT's SET.
3218 : : */
3219 : :
3220 : : /* Execute UPDATE with projection */
1566 3221 : 5390 : *returning = ExecUpdate(context, resultRelInfo,
3222 : : conflictTid, NULL, existing,
2673 andres@anarazel.de 3223 : 2705 : resultRelInfo->ri_onConflict->oc_ProjSlot,
3224 : : canSetTag);
3225 : :
3226 : : /*
3227 : : * Clear out existing tuple, as there might not be another conflict among
3228 : : * the next input rows. Don't want to hold resources till the end of the
3229 : : * query. First though, make sure that the returning slot, if any, has a
3230 : : * local copy of any OLD pass-by-reference values, if it refers to any OLD
3231 : : * columns.
3232 : : */
530 dean.a.rasheed@gmail 3233 [ + + ]: 2685 : if (*returning != NULL &&
3234 [ + + ]: 174 : resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD)
3235 : 8 : ExecMaterializeSlot(*returning);
3236 : :
2673 andres@anarazel.de 3237 : 2685 : ExecClearTuple(existing);
3238 : :
4071 3239 : 2685 : return true;
3240 : : }
3241 : :
3242 : : /*
3243 : : * ExecOnConflictSelect --- execute SELECT of INSERT ON CONFLICT DO SELECT
3244 : : *
3245 : : * If SELECT FOR UPDATE/SHARE is specified, try to lock tuple as part of
3246 : : * speculative insertion. If a qual originating from ON CONFLICT DO SELECT is
3247 : : * satisfied, select (but still lock row, even though it may not satisfy
3248 : : * estate's snapshot).
3249 : : *
3250 : : * Returns true if we're done (with or without a select), or false if the
3251 : : * caller must retry the INSERT from scratch.
3252 : : */
3253 : : static bool
138 dean.a.rasheed@gmail 3254 :GNC 192 : ExecOnConflictSelect(ModifyTableContext *context,
3255 : : ResultRelInfo *resultRelInfo,
3256 : : ItemPointer conflictTid,
3257 : : TupleTableSlot *excludedSlot,
3258 : : bool canSetTag,
3259 : : TupleTableSlot **returning)
3260 : : {
3261 : 192 : ModifyTableState *mtstate = context->mtstate;
3262 : 192 : ExprContext *econtext = mtstate->ps.ps_ExprContext;
3263 : 192 : Relation relation = resultRelInfo->ri_RelationDesc;
3264 : 192 : ExprState *onConflictSelectWhere = resultRelInfo->ri_onConflict->oc_WhereClause;
3265 : 192 : TupleTableSlot *existing = resultRelInfo->ri_onConflict->oc_Existing;
3266 : 192 : LockClauseStrength lockStrength = resultRelInfo->ri_onConflict->oc_LockStrength;
3267 : :
3268 : : /*
3269 : : * Parse analysis should have blocked ON CONFLICT for all system
3270 : : * relations, which includes these. There's no fundamental obstacle to
3271 : : * supporting this; we'd just need to handle LOCKTAG_TUPLE appropriately.
3272 : : */
3273 [ - + ]: 192 : Assert(!resultRelInfo->ri_needLockTagTuple);
3274 : :
3275 : : /* Fetch/lock existing tuple, according to the requested lock strength */
3276 [ + + ]: 192 : if (lockStrength == LCS_NONE)
3277 : : {
3278 [ - + ]: 122 : if (!table_tuple_fetch_row_version(relation,
3279 : : conflictTid,
3280 : : SnapshotAny,
3281 : : existing))
138 dean.a.rasheed@gmail 3282 [ # # ]:UNC 0 : elog(ERROR, "failed to fetch conflicting tuple for ON CONFLICT");
3283 : : }
3284 : : else
3285 : : {
3286 : : LockTupleMode lockmode;
3287 : :
138 dean.a.rasheed@gmail 3288 [ + + + + :GNC 70 : switch (lockStrength)
- ]
3289 : : {
3290 : 1 : case LCS_FORKEYSHARE:
3291 : 1 : lockmode = LockTupleKeyShare;
3292 : 1 : break;
3293 : 1 : case LCS_FORSHARE:
3294 : 1 : lockmode = LockTupleShare;
3295 : 1 : break;
3296 : 1 : case LCS_FORNOKEYUPDATE:
3297 : 1 : lockmode = LockTupleNoKeyExclusive;
3298 : 1 : break;
3299 : 67 : case LCS_FORUPDATE:
3300 : 67 : lockmode = LockTupleExclusive;
3301 : 67 : break;
138 dean.a.rasheed@gmail 3302 :UNC 0 : default:
3303 [ # # ]: 0 : elog(ERROR, "Unexpected lock strength %d", (int) lockStrength);
3304 : : }
3305 : :
138 dean.a.rasheed@gmail 3306 [ - + ]:GNC 70 : if (!ExecOnConflictLockRow(context, existing, conflictTid,
3307 : : resultRelInfo->ri_RelationDesc, lockmode, false))
138 dean.a.rasheed@gmail 3308 :UNC 0 : return false;
3309 : : }
3310 : :
3311 : : /*
3312 : : * Verify that the tuple is visible to our MVCC snapshot if the current
3313 : : * isolation level mandates that. See comments in ExecOnConflictUpdate().
3314 : : */
138 dean.a.rasheed@gmail 3315 :GNC 180 : ExecCheckTupleVisible(context->estate, relation, existing);
3316 : :
3317 : : /*
3318 : : * Make tuple and any needed join variables available to ExecQual. The
3319 : : * EXCLUDED tuple is installed in ecxt_innertuple, while the target's
3320 : : * existing tuple is installed in the scantuple. EXCLUDED has been made
3321 : : * to reference INNER_VAR in setrefs.c, but there is no other redirection.
3322 : : */
3323 : 180 : econtext->ecxt_scantuple = existing;
3324 : 180 : econtext->ecxt_innertuple = excludedSlot;
3325 : 180 : econtext->ecxt_outertuple = NULL;
3326 : :
3327 [ + + ]: 180 : if (!ExecQual(onConflictSelectWhere, econtext))
3328 : : {
3329 : 24 : ExecClearTuple(existing); /* see return below */
3330 [ - + ]: 24 : InstrCountFiltered1(&mtstate->ps, 1);
3331 : 24 : return true; /* done with the tuple */
3332 : : }
3333 : :
3334 [ + + ]: 156 : if (resultRelInfo->ri_WithCheckOptions != NIL)
3335 : : {
3336 : : /*
3337 : : * Check target's existing tuple against SELECT-applicable USING
3338 : : * security barrier quals (if any), enforced here as RLS checks/WCOs.
3339 : : *
3340 : : * The rewriter creates WCOs from the USING quals of SELECT policies,
3341 : : * and stores them as WCOs of "kind" WCO_RLS_CONFLICT_CHECK. If FOR
3342 : : * UPDATE/SHARE was specified, UPDATE permissions are required on the
3343 : : * target table, and the rewriter also adds WCOs built from the USING
3344 : : * quals of UPDATE policies, using WCOs of the same kind, and this
3345 : : * check enforces them too.
3346 : : */
3347 : 24 : ExecWithCheckOptions(WCO_RLS_CONFLICT_CHECK, resultRelInfo,
3348 : : existing,
3349 : : mtstate->ps.state);
3350 : : }
3351 : :
3352 : : /* RETURNING is required for DO SELECT */
3353 [ - + ]: 152 : Assert(resultRelInfo->ri_projectReturning);
3354 : :
3355 : 152 : *returning = ExecProcessReturning(context, resultRelInfo, false,
3356 : : existing, existing, context->planSlot);
3357 : :
3358 [ + - ]: 152 : if (canSetTag)
3359 : 152 : context->estate->es_processed++;
3360 : :
3361 : : /*
3362 : : * Before releasing the existing tuple, make sure that the returning slot
3363 : : * has a local copy of any pass-by-reference values.
3364 : : */
3365 : 152 : ExecMaterializeSlot(*returning);
3366 : :
3367 : : /*
3368 : : * Clear out existing tuple, as there might not be another conflict among
3369 : : * the next input rows. Don't want to hold resources till the end of the
3370 : : * query.
3371 : : */
3372 : 152 : ExecClearTuple(existing);
3373 : :
3374 : 152 : return true;
3375 : : }
3376 : :
3377 : : /*
3378 : : * Perform MERGE.
3379 : : */
3380 : : static TupleTableSlot *
1555 alvherre@alvh.no-ip. 3381 :CBC 10549 : ExecMerge(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
3382 : : ItemPointer tupleid, HeapTuple oldtuple, bool canSetTag)
3383 : : {
835 dean.a.rasheed@gmail 3384 : 10549 : TupleTableSlot *rslot = NULL;
3385 : : bool matched;
3386 : :
3387 : : /*-----
3388 : : * If we are dealing with a WHEN MATCHED case, tupleid or oldtuple is
3389 : : * valid, depending on whether the result relation is a table or a view.
3390 : : * We execute the first action for which the additional WHEN MATCHED AND
3391 : : * quals pass. If an action without quals is found, that action is
3392 : : * executed.
3393 : : *
3394 : : * Similarly, in the WHEN NOT MATCHED BY SOURCE case, tupleid or oldtuple
3395 : : * is valid, and we look at the given WHEN NOT MATCHED BY SOURCE actions
3396 : : * in sequence until one passes. This is almost identical to the WHEN
3397 : : * MATCHED case, and both cases are handled by ExecMergeMatched().
3398 : : *
3399 : : * Finally, in the WHEN NOT MATCHED [BY TARGET] case, both tupleid and
3400 : : * oldtuple are invalid, and we look at the given WHEN NOT MATCHED [BY
3401 : : * TARGET] actions in sequence until one passes.
3402 : : *
3403 : : * Things get interesting in case of concurrent update/delete of the
3404 : : * target tuple. Such concurrent update/delete is detected while we are
3405 : : * executing a WHEN MATCHED or WHEN NOT MATCHED BY SOURCE action.
3406 : : *
3407 : : * A concurrent update can:
3408 : : *
3409 : : * 1. modify the target tuple so that the results from checking any
3410 : : * additional quals attached to WHEN MATCHED or WHEN NOT MATCHED BY
3411 : : * SOURCE actions potentially change, but the result from the join
3412 : : * quals does not change.
3413 : : *
3414 : : * In this case, we are still dealing with the same kind of match
3415 : : * (MATCHED or NOT MATCHED BY SOURCE). We recheck the same list of
3416 : : * actions from the start and choose the first one that satisfies the
3417 : : * new target tuple.
3418 : : *
3419 : : * 2. modify the target tuple in the WHEN MATCHED case so that the join
3420 : : * quals no longer pass and hence the source and target tuples no
3421 : : * longer match.
3422 : : *
3423 : : * In this case, we are now dealing with a NOT MATCHED case, and we
3424 : : * process both WHEN NOT MATCHED BY SOURCE and WHEN NOT MATCHED [BY
3425 : : * TARGET] actions. First ExecMergeMatched() processes the list of
3426 : : * WHEN NOT MATCHED BY SOURCE actions in sequence until one passes,
3427 : : * then ExecMergeNotMatched() processes any WHEN NOT MATCHED [BY
3428 : : * TARGET] actions in sequence until one passes. Thus we may execute
3429 : : * two actions; one of each kind.
3430 : : *
3431 : : * Thus we support concurrent updates that turn MATCHED candidate rows
3432 : : * into NOT MATCHED rows. However, we do not attempt to support cases
3433 : : * that would turn NOT MATCHED rows into MATCHED rows, or which would
3434 : : * cause a target row to match a different source row.
3435 : : *
3436 : : * A concurrent delete changes a WHEN MATCHED case to WHEN NOT MATCHED
3437 : : * [BY TARGET].
3438 : : *
3439 : : * ExecMergeMatched() takes care of following the update chain and
3440 : : * re-finding the qualifying WHEN MATCHED or WHEN NOT MATCHED BY SOURCE
3441 : : * action, as long as the target tuple still exists. If the target tuple
3442 : : * gets deleted or a concurrent update causes the join quals to fail, it
3443 : : * returns a matched status of false and we call ExecMergeNotMatched().
3444 : : * Given that ExecMergeMatched() always makes progress by following the
3445 : : * update chain and we never switch from ExecMergeNotMatched() to
3446 : : * ExecMergeMatched(), there is no risk of a livelock.
3447 : : */
852 3448 [ + + + + ]: 10549 : matched = tupleid != NULL || oldtuple != NULL;
1555 alvherre@alvh.no-ip. 3449 [ + + ]: 10549 : if (matched)
835 dean.a.rasheed@gmail 3450 : 8758 : rslot = ExecMergeMatched(context, resultRelInfo, tupleid, oldtuple,
3451 : : canSetTag, &matched);
3452 : :
3453 : : /*
3454 : : * Deal with the NOT MATCHED case (either a NOT MATCHED tuple from the
3455 : : * join, or a previously MATCHED tuple for which ExecMergeMatched() set
3456 : : * "matched" to false, indicating that it no longer matches).
3457 : : */
1555 alvherre@alvh.no-ip. 3458 [ + + ]: 10487 : if (!matched)
3459 : : {
3460 : : /*
3461 : : * If a concurrent update turned a MATCHED case into a NOT MATCHED
3462 : : * case, and we have both WHEN NOT MATCHED BY SOURCE and WHEN NOT
3463 : : * MATCHED [BY TARGET] actions, and there is a RETURNING clause,
3464 : : * ExecMergeMatched() may have already executed a WHEN NOT MATCHED BY
3465 : : * SOURCE action, and computed the row to return. If so, we cannot
3466 : : * execute a WHEN NOT MATCHED [BY TARGET] action now, so mark it as
3467 : : * pending (to be processed on the next call to ExecModifyTable()).
3468 : : * Otherwise, just process the action now.
3469 : : */
822 dean.a.rasheed@gmail 3470 [ + + ]: 1800 : if (rslot == NULL)
3471 : 1798 : rslot = ExecMergeNotMatched(context, resultRelInfo, canSetTag);
3472 : : else
3473 : 2 : context->mtstate->mt_merge_pending_not_matched = context->planSlot;
3474 : : }
3475 : :
835 3476 : 10448 : return rslot;
3477 : : }
3478 : :
3479 : : /*
3480 : : * Check and execute the first qualifying MATCHED or NOT MATCHED BY SOURCE
3481 : : * action, depending on whether the join quals are satisfied. If the target
3482 : : * relation is a table, the current target tuple is identified by tupleid.
3483 : : * Otherwise, if the target relation is a view, oldtuple is the current target
3484 : : * tuple from the view.
3485 : : *
3486 : : * We start from the first WHEN MATCHED or WHEN NOT MATCHED BY SOURCE action
3487 : : * and check if the WHEN quals pass, if any. If the WHEN quals for the first
3488 : : * action do not pass, we check the second, then the third and so on. If we
3489 : : * reach the end without finding a qualifying action, we return NULL.
3490 : : * Otherwise, we execute the qualifying action and return its RETURNING
3491 : : * result, if any, or NULL.
3492 : : *
3493 : : * On entry, "*matched" is assumed to be true. If a concurrent update or
3494 : : * delete is detected that causes the join quals to no longer pass, we set it
3495 : : * to false, indicating that the caller should process any NOT MATCHED [BY
3496 : : * TARGET] actions.
3497 : : *
3498 : : * After a concurrent update, we restart from the first action to look for a
3499 : : * new qualifying action to execute. If the join quals originally passed, and
3500 : : * the concurrent update caused them to no longer pass, then we switch from
3501 : : * the MATCHED to the NOT MATCHED BY SOURCE list of actions before restarting
3502 : : * (and setting "*matched" to false). As a result we may execute a WHEN NOT
3503 : : * MATCHED BY SOURCE action, and set "*matched" to false, causing the caller
3504 : : * to also execute a WHEN NOT MATCHED [BY TARGET] action.
3505 : : */
3506 : : static TupleTableSlot *
1555 alvherre@alvh.no-ip. 3507 : 8758 : ExecMergeMatched(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
3508 : : ItemPointer tupleid, HeapTuple oldtuple, bool canSetTag,
3509 : : bool *matched)
3510 : : {
3511 : 8758 : ModifyTableState *mtstate = context->mtstate;
822 dean.a.rasheed@gmail 3512 : 8758 : List **mergeActions = resultRelInfo->ri_MergeActions;
3513 : : ItemPointerData lockedtid;
3514 : : List *actionStates;
835 3515 : 8758 : TupleTableSlot *newslot = NULL;
3516 : 8758 : TupleTableSlot *rslot = NULL;
1555 alvherre@alvh.no-ip. 3517 : 8758 : EState *estate = context->estate;
3518 : 8758 : ExprContext *econtext = mtstate->ps.ps_ExprContext;
3519 : : bool isNull;
3520 : 8758 : EPQState *epqstate = &mtstate->mt_epqstate;
3521 : : ListCell *l;
3522 : :
3523 : : /* Expect matched to be true on entry */
822 dean.a.rasheed@gmail 3524 [ - + ]: 8758 : Assert(*matched);
3525 : :
3526 : : /*
3527 : : * If there are no WHEN MATCHED or WHEN NOT MATCHED BY SOURCE actions, we
3528 : : * are done.
3529 : : */
3530 [ + + ]: 8758 : if (mergeActions[MERGE_WHEN_MATCHED] == NIL &&
3531 [ + + ]: 780 : mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] == NIL)
835 3532 : 332 : return NULL;
3533 : :
3534 : : /*
3535 : : * Make tuple and any needed join variables available to ExecQual and
3536 : : * ExecProject. The target's existing tuple is installed in the scantuple.
3537 : : * This target relation's slot is required only in the case of a MATCHED
3538 : : * or NOT MATCHED BY SOURCE tuple and UPDATE/DELETE actions.
3539 : : */
1555 alvherre@alvh.no-ip. 3540 : 8426 : econtext->ecxt_scantuple = resultRelInfo->ri_oldTupleSlot;
3541 : 8426 : econtext->ecxt_innertuple = context->planSlot;
3542 : 8426 : econtext->ecxt_outertuple = NULL;
3543 : :
3544 : : /*
3545 : : * This routine is only invoked for matched target rows, so we should
3546 : : * either have the tupleid of the target row, or an old tuple from the
3547 : : * target wholerow junk attr.
3548 : : */
852 dean.a.rasheed@gmail 3549 [ + + - + ]: 8426 : Assert(tupleid != NULL || oldtuple != NULL);
644 noah@leadboat.com 3550 : 8426 : ItemPointerSetInvalid(&lockedtid);
852 dean.a.rasheed@gmail 3551 [ + + ]: 8426 : if (oldtuple != NULL)
3552 : : {
644 noah@leadboat.com 3553 [ - + ]: 64 : Assert(!resultRelInfo->ri_needLockTagTuple);
852 dean.a.rasheed@gmail 3554 : 64 : ExecForceStoreHeapTuple(oldtuple, resultRelInfo->ri_oldTupleSlot,
3555 : : false);
3556 : : }
3557 : : else
3558 : : {
644 noah@leadboat.com 3559 [ + + ]: 8362 : if (resultRelInfo->ri_needLockTagTuple)
3560 : : {
3561 : : /*
3562 : : * This locks even for CMD_DELETE, for CMD_NOTHING, and for tuples
3563 : : * that don't match mas_whenqual. MERGE on system catalogs is a
3564 : : * minor use case, so don't bother optimizing those.
3565 : : */
3566 : 5745 : LockTuple(resultRelInfo->ri_RelationDesc, tupleid,
3567 : : InplaceUpdateTupleLock);
3568 : 5745 : lockedtid = *tupleid;
3569 : : }
3570 [ - + ]: 8362 : if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc,
3571 : : tupleid,
3572 : : SnapshotAny,
3573 : : resultRelInfo->ri_oldTupleSlot))
644 noah@leadboat.com 3574 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch the target tuple");
3575 : : }
3576 : :
3577 : : /*
3578 : : * Test the join condition. If it's satisfied, perform a MATCHED action.
3579 : : * Otherwise, perform a NOT MATCHED BY SOURCE action.
3580 : : *
3581 : : * Note that this join condition will be NULL if there are no NOT MATCHED
3582 : : * BY SOURCE actions --- see transform_MERGE_to_join(). In that case, we
3583 : : * need only consider MATCHED actions here.
3584 : : */
822 dean.a.rasheed@gmail 3585 [ + + ]:CBC 8426 : if (ExecQual(resultRelInfo->ri_MergeJoinCondition, econtext))
3586 : 8304 : actionStates = mergeActions[MERGE_WHEN_MATCHED];
3587 : : else
3588 : 122 : actionStates = mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE];
3589 : :
3590 : 8426 : lmerge_matched:
3591 : :
3592 [ + + + + : 15178 : foreach(l, actionStates)
+ + ]
3593 : : {
1555 alvherre@alvh.no-ip. 3594 : 8509 : MergeActionState *relaction = (MergeActionState *) lfirst(l);
3595 : 8509 : CmdType commandType = relaction->mas_action->commandType;
3596 : : TM_Result result;
3597 : 8509 : UpdateContext updateCxt = {0};
3598 : :
3599 : : /*
3600 : : * Test condition, if any.
3601 : : *
3602 : : * In the absence of any condition, we perform the action
3603 : : * unconditionally (no need to check separately since ExecQual() will
3604 : : * return true if there are no conditions to evaluate).
3605 : : */
3606 [ + + ]: 8509 : if (!ExecQual(relaction->mas_whenqual, econtext))
3607 : 6712 : continue;
3608 : :
3609 : : /*
3610 : : * Check if the existing target tuple meets the USING checks of
3611 : : * UPDATE/DELETE RLS policies. If those checks fail, we throw an
3612 : : * error.
3613 : : *
3614 : : * The WITH CHECK quals for UPDATE RLS policies are applied in
3615 : : * ExecUpdateAct() and hence we need not do anything special to handle
3616 : : * them.
3617 : : *
3618 : : * NOTE: We must do this after WHEN quals are evaluated, so that we
3619 : : * check policies only when they matter.
3620 : : */
1058 dean.a.rasheed@gmail 3621 [ + + + + ]: 1797 : if (resultRelInfo->ri_WithCheckOptions && commandType != CMD_NOTHING)
3622 : : {
1555 alvherre@alvh.no-ip. 3623 : 76 : ExecWithCheckOptions(commandType == CMD_UPDATE ?
3624 : : WCO_RLS_MERGE_UPDATE_CHECK : WCO_RLS_MERGE_DELETE_CHECK,
3625 : : resultRelInfo,
3626 : : resultRelInfo->ri_oldTupleSlot,
3627 [ + + ]: 76 : context->mtstate->ps.state);
3628 : : }
3629 : :
3630 : : /* Perform stated action */
3631 [ + + + - ]: 1781 : switch (commandType)
3632 : : {
3633 : 1413 : case CMD_UPDATE:
3634 : :
3635 : : /*
3636 : : * Project the output tuple, and use that to update the table.
3637 : : * We don't need to filter out junk attributes, because the
3638 : : * UPDATE action's targetlist doesn't have any.
3639 : : */
3640 : 1413 : newslot = ExecProject(relaction->mas_proj);
3641 : :
835 dean.a.rasheed@gmail 3642 : 1413 : mtstate->mt_merge_action = relaction;
1555 alvherre@alvh.no-ip. 3643 [ + + ]: 1413 : if (!ExecUpdatePrologue(context, resultRelInfo,
3644 : : tupleid, NULL, newslot, &result))
3645 : : {
1205 dean.a.rasheed@gmail 3646 [ + + ]: 11 : if (result == TM_Ok)
644 noah@leadboat.com 3647 : 102 : goto out; /* "do nothing" */
3648 : :
1205 dean.a.rasheed@gmail 3649 : 7 : break; /* concurrent update/delete */
3650 : : }
3651 : :
3652 : : /* INSTEAD OF ROW UPDATE Triggers */
852 3653 [ + + ]: 1402 : if (resultRelInfo->ri_TrigDesc &&
3654 [ + + ]: 230 : resultRelInfo->ri_TrigDesc->trig_update_instead_row)
3655 : : {
3656 [ - + ]: 52 : if (!ExecIRUpdateTriggers(estate, resultRelInfo,
3657 : : oldtuple, newslot))
644 noah@leadboat.com 3658 :UBC 0 : goto out; /* "do nothing" */
3659 : : }
3660 : : else
3661 : : {
3662 : : /* checked ri_needLockTagTuple above */
717 noah@leadboat.com 3663 [ - + ]:CBC 1350 : Assert(oldtuple == NULL);
3664 : :
852 dean.a.rasheed@gmail 3665 : 1350 : result = ExecUpdateAct(context, resultRelInfo, tupleid,
3666 : : NULL, newslot, canSetTag,
3667 : : &updateCxt);
3668 : :
3669 : : /*
3670 : : * As in ExecUpdate(), if ExecUpdateAct() reports that a
3671 : : * cross-partition update was done, then there's nothing
3672 : : * else for us to do --- the UPDATE has been turned into a
3673 : : * DELETE and an INSERT, and we must not perform any of
3674 : : * the usual post-update tasks. Also, the RETURNING tuple
3675 : : * (if any) has been projected, so we can just return
3676 : : * that.
3677 : : */
3678 [ + + ]: 1335 : if (updateCxt.crossPartUpdate)
3679 : : {
3680 : 89 : mtstate->mt_merge_updated += 1;
644 noah@leadboat.com 3681 : 89 : rslot = context->cpUpdateReturningSlot;
3682 : 89 : goto out;
3683 : : }
3684 : : }
3685 : :
852 dean.a.rasheed@gmail 3686 [ + + ]: 1298 : if (result == TM_Ok)
3687 : : {
1555 alvherre@alvh.no-ip. 3688 : 1251 : ExecUpdateEpilogue(context, &updateCxt, resultRelInfo,
3689 : : tupleid, NULL, newslot);
3690 : 1243 : mtstate->mt_merge_updated += 1;
3691 : : }
3692 : 1290 : break;
3693 : :
3694 : 348 : case CMD_DELETE:
835 dean.a.rasheed@gmail 3695 : 348 : mtstate->mt_merge_action = relaction;
1555 alvherre@alvh.no-ip. 3696 [ + + ]: 348 : if (!ExecDeletePrologue(context, resultRelInfo, tupleid,
3697 : : NULL, NULL, &result))
3698 : : {
1205 dean.a.rasheed@gmail 3699 [ + + ]: 7 : if (result == TM_Ok)
644 noah@leadboat.com 3700 : 4 : goto out; /* "do nothing" */
3701 : :
1205 dean.a.rasheed@gmail 3702 : 3 : break; /* concurrent update/delete */
3703 : : }
3704 : :
3705 : : /* INSTEAD OF ROW DELETE Triggers */
852 3706 [ + + ]: 341 : if (resultRelInfo->ri_TrigDesc &&
3707 [ + + ]: 37 : resultRelInfo->ri_TrigDesc->trig_delete_instead_row)
3708 : : {
3709 [ - + ]: 4 : if (!ExecIRDeleteTriggers(estate, resultRelInfo,
3710 : : oldtuple))
644 noah@leadboat.com 3711 :UBC 0 : goto out; /* "do nothing" */
3712 : : }
3713 : : else
3714 : : {
3715 : : /* checked ri_needLockTagTuple above */
717 noah@leadboat.com 3716 [ - + ]:CBC 337 : Assert(oldtuple == NULL);
3717 : :
852 dean.a.rasheed@gmail 3718 : 337 : result = ExecDeleteAct(context, resultRelInfo, tupleid,
3719 : : false);
3720 : : }
3721 : :
1555 alvherre@alvh.no-ip. 3722 [ + + ]: 341 : if (result == TM_Ok)
3723 : : {
3724 : 330 : ExecDeleteEpilogue(context, resultRelInfo, tupleid, NULL,
3725 : : false);
3726 : 330 : mtstate->mt_merge_deleted += 1;
3727 : : }
3728 : 341 : break;
3729 : :
3730 : 20 : case CMD_NOTHING:
3731 : : /* Doing nothing is always OK */
3732 : 20 : result = TM_Ok;
3733 : 20 : break;
3734 : :
1555 alvherre@alvh.no-ip. 3735 :UBC 0 : default:
822 dean.a.rasheed@gmail 3736 [ # # ]: 0 : elog(ERROR, "unknown action in MERGE WHEN clause");
3737 : : }
3738 : :
1555 alvherre@alvh.no-ip. 3739 [ + + + + :CBC 1661 : switch (result)
- - ]
3740 : : {
3741 : 1593 : case TM_Ok:
3742 : : /* all good; perform final actions */
1321 3743 [ + + + + ]: 1593 : if (canSetTag && commandType != CMD_NOTHING)
1555 3744 : 1559 : (estate->es_processed)++;
3745 : :
3746 : 1593 : break;
3747 : :
3748 : 21 : case TM_SelfModified:
3749 : :
3750 : : /*
3751 : : * The target tuple was already updated or deleted by the
3752 : : * current command, or by a later command in the current
3753 : : * transaction. The former case is explicitly disallowed by
3754 : : * the SQL standard for MERGE, which insists that the MERGE
3755 : : * join condition should not join a target row to more than
3756 : : * one source row.
3757 : : *
3758 : : * The latter case arises if the tuple is modified by a
3759 : : * command in a BEFORE trigger, or perhaps by a command in a
3760 : : * volatile function used in the query. In such situations we
3761 : : * should not ignore the MERGE action, but it is equally
3762 : : * unsafe to proceed. We don't want to discard the original
3763 : : * MERGE action while keeping the triggered actions based on
3764 : : * it; and it would be no better to allow the original MERGE
3765 : : * action while discarding the updates that it triggered. So
3766 : : * throwing an error is the only safe course.
3767 : : */
845 dean.a.rasheed@gmail 3768 [ + + ]: 21 : if (context->tmfd.cmax != estate->es_output_cid)
3769 [ + - ]: 8 : ereport(ERROR,
3770 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
3771 : : errmsg("tuple to be updated or deleted was already modified by an operation triggered by the current command"),
3772 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
3773 : :
1555 alvherre@alvh.no-ip. 3774 [ + - ]: 13 : if (TransactionIdIsCurrentTransactionId(context->tmfd.xmax))
3775 [ + - ]: 13 : ereport(ERROR,
3776 : : (errcode(ERRCODE_CARDINALITY_VIOLATION),
3777 : : /* translator: %s is a SQL command name */
3778 : : errmsg("%s command cannot affect row a second time",
3779 : : "MERGE"),
3780 : : errhint("Ensure that not more than one source row matches any one target row.")));
3781 : :
3782 : : /* This shouldn't happen */
1555 alvherre@alvh.no-ip. 3783 [ # # ]:UBC 0 : elog(ERROR, "attempted to update or delete invisible tuple");
3784 : : break;
3785 : :
1555 alvherre@alvh.no-ip. 3786 :CBC 5 : case TM_Deleted:
3787 [ - + ]: 5 : if (IsolationUsesXactSnapshot())
1555 alvherre@alvh.no-ip. 3788 [ # # ]:UBC 0 : ereport(ERROR,
3789 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3790 : : errmsg("could not serialize access due to concurrent delete")));
3791 : :
3792 : : /*
3793 : : * If the tuple was already deleted, set matched to false to
3794 : : * let caller handle it under NOT MATCHED [BY TARGET] clauses.
3795 : : */
835 dean.a.rasheed@gmail 3796 :CBC 5 : *matched = false;
644 noah@leadboat.com 3797 : 5 : goto out;
3798 : :
1555 alvherre@alvh.no-ip. 3799 : 42 : case TM_Updated:
3800 : : {
3801 : : bool was_matched;
3802 : : Relation resultRelationDesc;
3803 : : TupleTableSlot *epqslot,
3804 : : *inputslot;
3805 : : LockTupleMode lockmode;
3806 : :
117 akorotkov@postgresql 3807 [ + + ]: 42 : if (IsolationUsesXactSnapshot())
3808 [ + - ]: 1 : ereport(ERROR,
3809 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3810 : : errmsg("could not serialize access due to concurrent update")));
3811 : :
3812 : : /*
3813 : : * The target tuple was concurrently updated by some other
3814 : : * transaction. If we are currently processing a MATCHED
3815 : : * action, use EvalPlanQual() with the new version of the
3816 : : * tuple and recheck the join qual, to detect a change
3817 : : * from the MATCHED to the NOT MATCHED cases. If we are
3818 : : * already processing a NOT MATCHED BY SOURCE action, we
3819 : : * skip this (cannot switch from NOT MATCHED BY SOURCE to
3820 : : * MATCHED).
3821 : : */
822 dean.a.rasheed@gmail 3822 : 41 : was_matched = relaction->mas_action->matchKind == MERGE_WHEN_MATCHED;
1555 alvherre@alvh.no-ip. 3823 : 41 : resultRelationDesc = resultRelInfo->ri_RelationDesc;
3824 : 41 : lockmode = ExecUpdateLockMode(estate, resultRelInfo);
3825 : :
822 dean.a.rasheed@gmail 3826 [ + - ]: 41 : if (was_matched)
3827 : 41 : inputslot = EvalPlanQualSlot(epqstate, resultRelationDesc,
3828 : : resultRelInfo->ri_RangeTableIndex);
3829 : : else
822 dean.a.rasheed@gmail 3830 :UBC 0 : inputslot = resultRelInfo->ri_oldTupleSlot;
3831 : :
1555 alvherre@alvh.no-ip. 3832 :CBC 41 : result = table_tuple_lock(resultRelationDesc, tupleid,
3833 : : estate->es_snapshot,
3834 : : inputslot, estate->es_output_cid,
3835 : : lockmode, LockWaitBlock,
3836 : : TUPLE_LOCK_FLAG_FIND_LAST_VERSION,
3837 : : &context->tmfd);
3838 [ + - + - ]: 41 : switch (result)
3839 : : {
3840 : 40 : case TM_Ok:
3841 : :
3842 : : /*
3843 : : * If the tuple was updated and migrated to
3844 : : * another partition concurrently, the current
3845 : : * MERGE implementation can't follow. There's
3846 : : * probably a better way to handle this case, but
3847 : : * it'd require recognizing the relation to which
3848 : : * the tuple moved, and setting our current
3849 : : * resultRelInfo to that.
3850 : : */
298 dean.a.rasheed@gmail 3851 [ - + ]: 40 : if (ItemPointerIndicatesMovedPartitions(tupleid))
1555 alvherre@alvh.no-ip. 3852 [ # # ]:UBC 0 : ereport(ERROR,
3853 : : (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
3854 : : errmsg("tuple to be merged was already moved to another partition due to concurrent update")));
3855 : :
3856 : : /*
3857 : : * If this was a MATCHED case, use EvalPlanQual()
3858 : : * to recheck the join condition.
3859 : : */
822 dean.a.rasheed@gmail 3860 [ + - ]:CBC 40 : if (was_matched)
3861 : : {
3862 : 40 : epqslot = EvalPlanQual(epqstate,
3863 : : resultRelationDesc,
3864 : : resultRelInfo->ri_RangeTableIndex,
3865 : : inputslot);
3866 : :
3867 : : /*
3868 : : * If the subplan didn't return a tuple, then
3869 : : * we must be dealing with an inner join for
3870 : : * which the join condition no longer matches.
3871 : : * This can only happen if there are no NOT
3872 : : * MATCHED actions, and so there is nothing
3873 : : * more to do.
3874 : : */
3875 [ + - - + ]: 40 : if (TupIsNull(epqslot))
644 noah@leadboat.com 3876 :UBC 0 : goto out;
3877 : :
3878 : : /*
3879 : : * If we got a NULL ctid from the subplan, the
3880 : : * join quals no longer pass and we switch to
3881 : : * the NOT MATCHED BY SOURCE case.
3882 : : */
822 dean.a.rasheed@gmail 3883 :CBC 40 : (void) ExecGetJunkAttribute(epqslot,
3884 : 40 : resultRelInfo->ri_RowIdAttNo,
3885 : : &isNull);
3886 [ + + ]: 40 : if (isNull)
3887 : 2 : *matched = false;
3888 : :
3889 : : /*
3890 : : * Otherwise, recheck the join quals to see if
3891 : : * we need to switch to the NOT MATCHED BY
3892 : : * SOURCE case.
3893 : : */
644 noah@leadboat.com 3894 [ + + ]: 40 : if (resultRelInfo->ri_needLockTagTuple)
3895 : : {
3896 [ + - ]: 1 : if (ItemPointerIsValid(&lockedtid))
3897 : 1 : UnlockTuple(resultRelInfo->ri_RelationDesc, &lockedtid,
3898 : : InplaceUpdateTupleLock);
298 dean.a.rasheed@gmail 3899 : 1 : LockTuple(resultRelInfo->ri_RelationDesc, tupleid,
3900 : : InplaceUpdateTupleLock);
3901 : 1 : lockedtid = *tupleid;
3902 : : }
3903 : :
822 3904 [ - + ]: 40 : if (!table_tuple_fetch_row_version(resultRelationDesc,
3905 : : tupleid,
3906 : : SnapshotAny,
3907 : : resultRelInfo->ri_oldTupleSlot))
822 dean.a.rasheed@gmail 3908 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch the target tuple");
3909 : :
822 dean.a.rasheed@gmail 3910 [ + + ]:CBC 40 : if (*matched)
3911 : 38 : *matched = ExecQual(resultRelInfo->ri_MergeJoinCondition,
3912 : : econtext);
3913 : :
3914 : : /* Switch lists, if necessary */
3915 [ + + ]: 40 : if (!*matched)
3916 : : {
3917 : 4 : actionStates = mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE];
3918 : :
3919 : : /*
3920 : : * If we have both NOT MATCHED BY SOURCE
3921 : : * and NOT MATCHED BY TARGET actions (a
3922 : : * full join between the source and target
3923 : : * relations), the single previously
3924 : : * matched tuple from the outer plan node
3925 : : * is treated as two not matched tuples,
3926 : : * in the same way as if they had not
3927 : : * matched to start with. Therefore, we
3928 : : * must adjust the outer plan node's tuple
3929 : : * count, if we're instrumenting the
3930 : : * query, to get the correct "skipped" row
3931 : : * count --- see show_modifytable_info().
3932 : : */
226 3933 [ + + ]: 4 : if (outerPlanState(mtstate)->instrument &&
3934 [ + - ]: 1 : mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] &&
3935 [ + - ]: 1 : mergeActions[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
3936 : 1 : InstrUpdateTupleCount(outerPlanState(mtstate)->instrument, 1.0);
3937 : : }
3938 : : }
3939 : :
3940 : : /*
3941 : : * Loop back and process the MATCHED or NOT
3942 : : * MATCHED BY SOURCE actions from the start.
3943 : : */
1555 alvherre@alvh.no-ip. 3944 : 40 : goto lmerge_matched;
3945 : :
1555 alvherre@alvh.no-ip. 3946 :UBC 0 : case TM_Deleted:
3947 : :
3948 : : /*
3949 : : * tuple already deleted; tell caller to run NOT
3950 : : * MATCHED [BY TARGET] actions
3951 : : */
835 dean.a.rasheed@gmail 3952 : 0 : *matched = false;
644 noah@leadboat.com 3953 : 0 : goto out;
3954 : :
1555 alvherre@alvh.no-ip. 3955 :CBC 1 : case TM_SelfModified:
3956 : :
3957 : : /*
3958 : : * This can be reached when following an update
3959 : : * chain from a tuple updated by another session,
3960 : : * reaching a tuple that was already updated or
3961 : : * deleted by the current command, or by a later
3962 : : * command in the current transaction. As above,
3963 : : * this should always be treated as an error.
3964 : : */
3965 [ - + ]: 1 : if (context->tmfd.cmax != estate->es_output_cid)
1555 alvherre@alvh.no-ip. 3966 [ # # ]:UBC 0 : ereport(ERROR,
3967 : : (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
3968 : : errmsg("tuple to be updated or deleted was already modified by an operation triggered by the current command"),
3969 : : errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
3970 : :
845 dean.a.rasheed@gmail 3971 [ + - ]:CBC 1 : if (TransactionIdIsCurrentTransactionId(context->tmfd.xmax))
3972 [ + - ]: 1 : ereport(ERROR,
3973 : : (errcode(ERRCODE_CARDINALITY_VIOLATION),
3974 : : /* translator: %s is a SQL command name */
3975 : : errmsg("%s command cannot affect row a second time",
3976 : : "MERGE"),
3977 : : errhint("Ensure that not more than one source row matches any one target row.")));
3978 : :
3979 : : /* This shouldn't happen */
845 dean.a.rasheed@gmail 3980 [ # # ]:UBC 0 : elog(ERROR, "attempted to update or delete invisible tuple");
3981 : : goto out;
3982 : :
1555 alvherre@alvh.no-ip. 3983 : 0 : default:
3984 : : /* see table_tuple_lock call in ExecDelete() */
3985 [ # # ]: 0 : elog(ERROR, "unexpected table_tuple_lock status: %u",
3986 : : result);
3987 : : goto out;
3988 : : }
3989 : : }
3990 : :
3991 : 0 : case TM_Invisible:
3992 : : case TM_WouldBlock:
3993 : : case TM_BeingModified:
3994 : : /* these should not occur */
3995 [ # # ]: 0 : elog(ERROR, "unexpected tuple operation result: %d", result);
3996 : : break;
3997 : : }
3998 : :
3999 : : /* Process RETURNING if present */
835 dean.a.rasheed@gmail 4000 [ + + ]:CBC 1593 : if (resultRelInfo->ri_projectReturning)
4001 : : {
4002 [ + + - - ]: 288 : switch (commandType)
4003 : : {
4004 : 124 : case CMD_UPDATE:
530 4005 : 124 : rslot = ExecProcessReturning(context,
4006 : : resultRelInfo,
4007 : : false,
4008 : : resultRelInfo->ri_oldTupleSlot,
4009 : : newslot,
4010 : : context->planSlot);
835 4011 : 124 : break;
4012 : :
4013 : 164 : case CMD_DELETE:
530 4014 : 164 : rslot = ExecProcessReturning(context,
4015 : : resultRelInfo,
4016 : : true,
4017 : : resultRelInfo->ri_oldTupleSlot,
4018 : : NULL,
4019 : : context->planSlot);
835 4020 : 164 : break;
4021 : :
835 dean.a.rasheed@gmail 4022 :UBC 0 : case CMD_NOTHING:
4023 : 0 : break;
4024 : :
4025 : 0 : default:
4026 [ # # ]: 0 : elog(ERROR, "unrecognized commandType: %d",
4027 : : (int) commandType);
4028 : : }
4029 : : }
4030 : :
4031 : : /*
4032 : : * We've activated one of the WHEN clauses, so we don't search
4033 : : * further. This is required behaviour, not an optimization.
4034 : : */
1555 alvherre@alvh.no-ip. 4035 :CBC 1593 : break;
4036 : : }
4037 : :
4038 : : /*
4039 : : * Successfully executed an action or no qualifying action was found.
4040 : : */
644 noah@leadboat.com 4041 : 8364 : out:
4042 [ + + ]: 8364 : if (ItemPointerIsValid(&lockedtid))
4043 : 5745 : UnlockTuple(resultRelInfo->ri_RelationDesc, &lockedtid,
4044 : : InplaceUpdateTupleLock);
835 dean.a.rasheed@gmail 4045 : 8364 : return rslot;
4046 : : }
4047 : :
4048 : : /*
4049 : : * Execute the first qualifying NOT MATCHED [BY TARGET] action.
4050 : : */
4051 : : static TupleTableSlot *
1555 alvherre@alvh.no-ip. 4052 : 1800 : ExecMergeNotMatched(ModifyTableContext *context, ResultRelInfo *resultRelInfo,
4053 : : bool canSetTag)
4054 : : {
4055 : 1800 : ModifyTableState *mtstate = context->mtstate;
4056 : 1800 : ExprContext *econtext = mtstate->ps.ps_ExprContext;
4057 : : List *actionStates;
835 dean.a.rasheed@gmail 4058 : 1800 : TupleTableSlot *rslot = NULL;
4059 : : ListCell *l;
4060 : :
4061 : : /*
4062 : : * For INSERT actions, the root relation's merge action is OK since the
4063 : : * INSERT's targetlist and the WHEN conditions can only refer to the
4064 : : * source relation and hence it does not matter which result relation we
4065 : : * work with.
4066 : : *
4067 : : * XXX does this mean that we can avoid creating copies of actionStates on
4068 : : * partitioned tables, for not-matched actions?
4069 : : */
822 4070 : 1800 : actionStates = resultRelInfo->ri_MergeActions[MERGE_WHEN_NOT_MATCHED_BY_TARGET];
4071 : :
4072 : : /*
4073 : : * Make source tuple available to ExecQual and ExecProject. We don't need
4074 : : * the target tuple, since the WHEN quals and targetlist can't refer to
4075 : : * the target columns.
4076 : : */
1555 alvherre@alvh.no-ip. 4077 : 1800 : econtext->ecxt_scantuple = NULL;
4078 : 1800 : econtext->ecxt_innertuple = context->planSlot;
4079 : 1800 : econtext->ecxt_outertuple = NULL;
4080 : :
4081 [ + - + + : 2380 : foreach(l, actionStates)
+ + ]
4082 : : {
4083 : 1800 : MergeActionState *action = (MergeActionState *) lfirst(l);
4084 : 1800 : CmdType commandType = action->mas_action->commandType;
4085 : : TupleTableSlot *newslot;
4086 : :
4087 : : /*
4088 : : * Test condition, if any.
4089 : : *
4090 : : * In the absence of any condition, we perform the action
4091 : : * unconditionally (no need to check separately since ExecQual() will
4092 : : * return true if there are no conditions to evaluate).
4093 : : */
4094 [ + + ]: 1800 : if (!ExecQual(action->mas_whenqual, econtext))
4095 : 580 : continue;
4096 : :
4097 : : /* Perform stated action */
4098 [ + - - ]: 1220 : switch (commandType)
4099 : : {
4100 : 1220 : case CMD_INSERT:
4101 : :
4102 : : /*
4103 : : * Project the tuple. In case of a partitioned table, the
4104 : : * projection was already built to use the root's descriptor,
4105 : : * so we don't need to map the tuple here.
4106 : : */
4107 : 1220 : newslot = ExecProject(action->mas_proj);
835 dean.a.rasheed@gmail 4108 : 1220 : mtstate->mt_merge_action = action;
4109 : :
4110 : 1220 : rslot = ExecInsert(context, mtstate->rootResultRelInfo,
4111 : : newslot, canSetTag, NULL, NULL);
1555 alvherre@alvh.no-ip. 4112 : 1181 : mtstate->mt_merge_inserted += 1;
4113 : 1181 : break;
1555 alvherre@alvh.no-ip. 4114 :UBC 0 : case CMD_NOTHING:
4115 : : /* Do nothing */
4116 : 0 : break;
4117 : 0 : default:
4118 [ # # ]: 0 : elog(ERROR, "unknown action in MERGE WHEN NOT MATCHED clause");
4119 : : }
4120 : :
4121 : : /*
4122 : : * We've activated one of the WHEN clauses, so we don't search
4123 : : * further. This is required behaviour, not an optimization.
4124 : : */
1555 alvherre@alvh.no-ip. 4125 :CBC 1181 : break;
4126 : : }
4127 : :
835 dean.a.rasheed@gmail 4128 : 1761 : return rslot;
4129 : : }
4130 : :
4131 : : /*
4132 : : * Initialize state for execution of MERGE.
4133 : : */
4134 : : void
1555 alvherre@alvh.no-ip. 4135 : 1056 : ExecInitMerge(ModifyTableState *mtstate, EState *estate)
4136 : : {
498 amitlan@postgresql.o 4137 : 1056 : List *mergeActionLists = mtstate->mt_mergeActionLists;
4138 : 1056 : List *mergeJoinConditions = mtstate->mt_mergeJoinConditions;
1555 alvherre@alvh.no-ip. 4139 : 1056 : ResultRelInfo *rootRelInfo = mtstate->rootResultRelInfo;
4140 : : ResultRelInfo *resultRelInfo;
4141 : : ExprContext *econtext;
4142 : : ListCell *lc;
4143 : : int i;
4144 : :
498 amitlan@postgresql.o 4145 [ - + ]: 1056 : if (mergeActionLists == NIL)
1555 alvherre@alvh.no-ip. 4146 :UBC 0 : return;
4147 : :
1555 alvherre@alvh.no-ip. 4148 :CBC 1056 : mtstate->mt_merge_subcommands = 0;
4149 : :
4150 [ + + ]: 1056 : if (mtstate->ps.ps_ExprContext == NULL)
4151 : 864 : ExecAssignExprContext(estate, &mtstate->ps);
4152 : 1056 : econtext = mtstate->ps.ps_ExprContext;
4153 : :
4154 : : /*
4155 : : * Create a MergeActionState for each action on the mergeActionList and
4156 : : * add it to either a list of matched actions or not-matched actions.
4157 : : *
4158 : : * Similar logic appears in ExecInitPartitionInfo(), so if changing
4159 : : * anything here, do so there too.
4160 : : */
4161 : 1056 : i = 0;
498 amitlan@postgresql.o 4162 [ + - + + : 2268 : foreach(lc, mergeActionLists)
+ + ]
4163 : : {
1555 alvherre@alvh.no-ip. 4164 : 1212 : List *mergeActionList = lfirst(lc);
4165 : : Node *joinCondition;
4166 : : TupleDesc relationDesc;
4167 : : ListCell *l;
4168 : :
498 amitlan@postgresql.o 4169 : 1212 : joinCondition = (Node *) list_nth(mergeJoinConditions, i);
1555 alvherre@alvh.no-ip. 4170 : 1212 : resultRelInfo = mtstate->resultRelInfo + i;
4171 : 1212 : i++;
4172 : 1212 : relationDesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
4173 : :
4174 : : /* initialize slots for MERGE fetches from this rel */
4175 [ + - ]: 1212 : if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
4176 : 1212 : ExecInitMergeTupleSlots(mtstate, resultRelInfo);
4177 : :
4178 : : /* initialize state for join condition checking */
822 dean.a.rasheed@gmail 4179 : 1212 : resultRelInfo->ri_MergeJoinCondition =
4180 : 1212 : ExecInitQual((List *) joinCondition, &mtstate->ps);
4181 : :
1555 alvherre@alvh.no-ip. 4182 [ + - + + : 3321 : foreach(l, mergeActionList)
+ + ]
4183 : : {
4184 : 2109 : MergeAction *action = (MergeAction *) lfirst(l);
4185 : : MergeActionState *action_state;
4186 : : TupleTableSlot *tgtslot;
4187 : : TupleDesc tgtdesc;
4188 : :
4189 : : /*
4190 : : * Build action merge state for this rel. (For partitions,
4191 : : * equivalent code exists in ExecInitPartitionInfo.)
4192 : : */
4193 : 2109 : action_state = makeNode(MergeActionState);
4194 : 2109 : action_state->mas_action = action;
4195 : 2109 : action_state->mas_whenqual = ExecInitQual((List *) action->qual,
4196 : : &mtstate->ps);
4197 : :
4198 : : /*
4199 : : * We create three lists - one for each MergeMatchKind - and stick
4200 : : * the MergeActionState into the appropriate list.
4201 : : */
822 dean.a.rasheed@gmail 4202 : 4218 : resultRelInfo->ri_MergeActions[action->matchKind] =
4203 : 2109 : lappend(resultRelInfo->ri_MergeActions[action->matchKind],
4204 : : action_state);
4205 : :
1555 alvherre@alvh.no-ip. 4206 [ + + + + : 2109 : switch (action->commandType)
- ]
4207 : : {
4208 : 704 : case CMD_INSERT:
4209 : : /* INSERT actions always use rootRelInfo */
4210 : 704 : ExecCheckPlanOutput(rootRelInfo->ri_RelationDesc,
4211 : : action->targetList);
4212 : :
4213 : : /*
4214 : : * If the MERGE targets a partitioned table, any INSERT
4215 : : * actions must be routed through it, not the child
4216 : : * relations. Initialize the routing struct and the root
4217 : : * table's "new" tuple slot for that, if not already done.
4218 : : * The projection we prepare, for all relations, uses the
4219 : : * root relation descriptor, and targets the plan's root
4220 : : * slot. (This is consistent with the fact that we
4221 : : * checked the plan output to match the root relation,
4222 : : * above.)
4223 : : */
4224 [ + + ]: 704 : if (rootRelInfo->ri_RelationDesc->rd_rel->relkind ==
4225 : : RELKIND_PARTITIONED_TABLE)
4226 : : {
4227 [ + + ]: 216 : if (mtstate->mt_partition_tuple_routing == NULL)
4228 : : {
4229 : : /*
4230 : : * Initialize planstate for routing if not already
4231 : : * done.
4232 : : *
4233 : : * Note that the slot is managed as a standalone
4234 : : * slot belonging to ModifyTableState, so we pass
4235 : : * NULL for the 2nd argument.
4236 : : */
4237 : 100 : mtstate->mt_root_tuple_slot =
4238 : 100 : table_slot_create(rootRelInfo->ri_RelationDesc,
4239 : : NULL);
4240 : 100 : mtstate->mt_partition_tuple_routing =
4241 : 100 : ExecSetupPartitionTupleRouting(estate,
4242 : : rootRelInfo->ri_RelationDesc);
4243 : : }
4244 : 216 : tgtslot = mtstate->mt_root_tuple_slot;
4245 : 216 : tgtdesc = RelationGetDescr(rootRelInfo->ri_RelationDesc);
4246 : : }
4247 : : else
4248 : : {
4249 : : /*
4250 : : * If the MERGE targets an inherited table, we insert
4251 : : * into the root table, so we must initialize its
4252 : : * "new" tuple slot, if not already done, and use its
4253 : : * relation descriptor for the projection.
4254 : : *
4255 : : * For non-inherited tables, rootRelInfo and
4256 : : * resultRelInfo are the same, and the "new" tuple
4257 : : * slot will already have been initialized.
4258 : : */
395 dean.a.rasheed@gmail 4259 [ + + ]: 488 : if (rootRelInfo->ri_newTupleSlot == NULL)
4260 : 24 : rootRelInfo->ri_newTupleSlot =
4261 : 24 : table_slot_create(rootRelInfo->ri_RelationDesc,
4262 : : &estate->es_tupleTable);
4263 : :
4264 : 488 : tgtslot = rootRelInfo->ri_newTupleSlot;
4265 : 488 : tgtdesc = RelationGetDescr(rootRelInfo->ri_RelationDesc);
4266 : : }
4267 : :
1555 alvherre@alvh.no-ip. 4268 : 704 : action_state->mas_proj =
4269 : 704 : ExecBuildProjectionInfo(action->targetList, econtext,
4270 : : tgtslot,
4271 : : &mtstate->ps,
4272 : : tgtdesc);
4273 : :
4274 : 704 : mtstate->mt_merge_subcommands |= MERGE_INSERT;
4275 : 704 : break;
4276 : 1040 : case CMD_UPDATE:
4277 : 1040 : action_state->mas_proj =
4278 : 1040 : ExecBuildUpdateProjection(action->targetList,
4279 : : true,
4280 : : action->updateColnos,
4281 : : relationDesc,
4282 : : econtext,
4283 : : resultRelInfo->ri_newTupleSlot,
4284 : : &mtstate->ps);
4285 : 1040 : mtstate->mt_merge_subcommands |= MERGE_UPDATE;
4286 : 1040 : break;
4287 : 315 : case CMD_DELETE:
4288 : 315 : mtstate->mt_merge_subcommands |= MERGE_DELETE;
4289 : 315 : break;
4290 : 50 : case CMD_NOTHING:
4291 : 50 : break;
1555 alvherre@alvh.no-ip. 4292 :UBC 0 : default:
458 dean.a.rasheed@gmail 4293 [ # # ]: 0 : elog(ERROR, "unknown action in MERGE WHEN clause");
4294 : : break;
4295 : : }
4296 : : }
4297 : : }
4298 : :
4299 : : /*
4300 : : * If the MERGE targets an inherited table, any INSERT actions will use
4301 : : * rootRelInfo, and rootRelInfo will not be in the resultRelInfo array.
4302 : : * Therefore we must initialize its WITH CHECK OPTION constraints and
4303 : : * RETURNING projection, as ExecInitModifyTable did for the resultRelInfo
4304 : : * entries.
4305 : : *
4306 : : * Note that the planner does not build a withCheckOptionList or
4307 : : * returningList for the root relation, but as in ExecInitPartitionInfo,
4308 : : * we can use the first resultRelInfo entry as a reference to calculate
4309 : : * the attno's for the root table.
4310 : : */
395 dean.a.rasheed@gmail 4311 [ + + ]:CBC 1056 : if (rootRelInfo != mtstate->resultRelInfo &&
4312 [ + + ]: 159 : rootRelInfo->ri_RelationDesc->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
4313 [ + + ]: 32 : (mtstate->mt_merge_subcommands & MERGE_INSERT) != 0)
4314 : : {
4315 : 24 : ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
4316 : 24 : Relation rootRelation = rootRelInfo->ri_RelationDesc;
4317 : 24 : Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
4318 : 24 : int firstVarno = mtstate->resultRelInfo[0].ri_RangeTableIndex;
4319 : 24 : AttrMap *part_attmap = NULL;
4320 : : bool found_whole_row;
4321 : :
4322 [ + + ]: 24 : if (node->withCheckOptionLists != NIL)
4323 : : {
4324 : : List *wcoList;
4325 : 12 : List *wcoExprs = NIL;
4326 : :
4327 : : /* There should be as many WCO lists as result rels */
4328 [ - + ]: 12 : Assert(list_length(node->withCheckOptionLists) ==
4329 : : list_length(node->resultRelations));
4330 : :
4331 : : /*
4332 : : * Use the first WCO list as a reference. In the most common case,
4333 : : * this will be for the same relation as rootRelInfo, and so there
4334 : : * will be no need to adjust its attno's.
4335 : : */
4336 : 12 : wcoList = linitial(node->withCheckOptionLists);
4337 [ + - ]: 12 : if (rootRelation != firstResultRel)
4338 : : {
4339 : : /* Convert any Vars in it to contain the root's attno's */
4340 : : part_attmap =
4341 : 12 : build_attrmap_by_name(RelationGetDescr(rootRelation),
4342 : : RelationGetDescr(firstResultRel),
4343 : : false);
4344 : :
4345 : : wcoList = (List *)
4346 : 12 : map_variable_attnos((Node *) wcoList,
4347 : : firstVarno, 0,
4348 : : part_attmap,
4349 : 12 : RelationGetForm(rootRelation)->reltype,
4350 : : &found_whole_row);
4351 : : }
4352 : :
4353 [ + - + + : 60 : foreach(lc, wcoList)
+ + ]
4354 : : {
4355 : 48 : WithCheckOption *wco = lfirst_node(WithCheckOption, lc);
4356 : 48 : ExprState *wcoExpr = ExecInitQual(castNode(List, wco->qual),
4357 : : &mtstate->ps);
4358 : :
4359 : 48 : wcoExprs = lappend(wcoExprs, wcoExpr);
4360 : : }
4361 : :
4362 : 12 : rootRelInfo->ri_WithCheckOptions = wcoList;
4363 : 12 : rootRelInfo->ri_WithCheckOptionExprs = wcoExprs;
4364 : : }
4365 : :
4366 [ + + ]: 24 : if (node->returningLists != NIL)
4367 : : {
4368 : : List *returningList;
4369 : :
4370 : : /* There should be as many returning lists as result rels */
4371 [ - + ]: 4 : Assert(list_length(node->returningLists) ==
4372 : : list_length(node->resultRelations));
4373 : :
4374 : : /*
4375 : : * Use the first returning list as a reference. In the most common
4376 : : * case, this will be for the same relation as rootRelInfo, and so
4377 : : * there will be no need to adjust its attno's.
4378 : : */
4379 : 4 : returningList = linitial(node->returningLists);
4380 [ + - ]: 4 : if (rootRelation != firstResultRel)
4381 : : {
4382 : : /* Convert any Vars in it to contain the root's attno's */
4383 [ - + ]: 4 : if (part_attmap == NULL)
4384 : : part_attmap =
395 dean.a.rasheed@gmail 4385 :UBC 0 : build_attrmap_by_name(RelationGetDescr(rootRelation),
4386 : : RelationGetDescr(firstResultRel),
4387 : : false);
4388 : :
4389 : : returningList = (List *)
395 dean.a.rasheed@gmail 4390 :CBC 4 : map_variable_attnos((Node *) returningList,
4391 : : firstVarno, 0,
4392 : : part_attmap,
4393 : 4 : RelationGetForm(rootRelation)->reltype,
4394 : : &found_whole_row);
4395 : : }
4396 : 4 : rootRelInfo->ri_returningList = returningList;
4397 : :
4398 : : /* Initialize the RETURNING projection */
4399 : 4 : rootRelInfo->ri_projectReturning =
4400 : 4 : ExecBuildProjectionInfo(returningList, econtext,
4401 : : mtstate->ps.ps_ResultTupleSlot,
4402 : : &mtstate->ps,
4403 : : RelationGetDescr(rootRelation));
4404 : : }
4405 : : }
4406 : : }
4407 : :
4408 : : /*
4409 : : * Initializes the tuple slots in a ResultRelInfo for any MERGE action.
4410 : : *
4411 : : * We mark 'projectNewInfoValid' even though the projections themselves
4412 : : * are not initialized here.
4413 : : */
4414 : : void
1555 alvherre@alvh.no-ip. 4415 : 1227 : ExecInitMergeTupleSlots(ModifyTableState *mtstate,
4416 : : ResultRelInfo *resultRelInfo)
4417 : : {
4418 : 1227 : EState *estate = mtstate->ps.state;
4419 : :
4420 [ - + ]: 1227 : Assert(!resultRelInfo->ri_projectNewInfoValid);
4421 : :
4422 : 1227 : resultRelInfo->ri_oldTupleSlot =
4423 : 1227 : table_slot_create(resultRelInfo->ri_RelationDesc,
4424 : : &estate->es_tupleTable);
4425 : 1227 : resultRelInfo->ri_newTupleSlot =
4426 : 1227 : table_slot_create(resultRelInfo->ri_RelationDesc,
4427 : : &estate->es_tupleTable);
4428 : 1227 : resultRelInfo->ri_projectNewInfoValid = true;
4429 : 1227 : }
4430 : :
4431 : : /*
4432 : : * Process BEFORE EACH STATEMENT triggers
4433 : : */
4434 : : static void
6107 tgl@sss.pgh.pa.us 4435 : 74479 : fireBSTriggers(ModifyTableState *node)
4436 : : {
3025 alvherre@alvh.no-ip. 4437 : 74479 : ModifyTable *plan = (ModifyTable *) node->ps.plan;
2080 heikki.linnakangas@i 4438 : 74479 : ResultRelInfo *resultRelInfo = node->rootResultRelInfo;
4439 : :
6107 tgl@sss.pgh.pa.us 4440 [ + + + + : 74479 : switch (node->operation)
- ]
4441 : : {
4442 : 56187 : case CMD_INSERT:
3347 rhaas@postgresql.org 4443 : 56187 : ExecBSInsertTriggers(node->ps.state, resultRelInfo);
3025 alvherre@alvh.no-ip. 4444 [ + + ]: 56179 : if (plan->onConflictAction == ONCONFLICT_UPDATE)
4071 andres@anarazel.de 4445 : 617 : ExecBSUpdateTriggers(node->ps.state,
4446 : : resultRelInfo);
6107 tgl@sss.pgh.pa.us 4447 : 56179 : break;
4448 : 8994 : case CMD_UPDATE:
3347 rhaas@postgresql.org 4449 : 8994 : ExecBSUpdateTriggers(node->ps.state, resultRelInfo);
6107 tgl@sss.pgh.pa.us 4450 : 8994 : break;
4451 : 8341 : case CMD_DELETE:
3347 rhaas@postgresql.org 4452 : 8341 : ExecBSDeleteTriggers(node->ps.state, resultRelInfo);
6107 tgl@sss.pgh.pa.us 4453 : 8341 : break;
1555 alvherre@alvh.no-ip. 4454 : 957 : case CMD_MERGE:
4455 [ + + ]: 957 : if (node->mt_merge_subcommands & MERGE_INSERT)
4456 : 523 : ExecBSInsertTriggers(node->ps.state, resultRelInfo);
4457 [ + + ]: 957 : if (node->mt_merge_subcommands & MERGE_UPDATE)
4458 : 629 : ExecBSUpdateTriggers(node->ps.state, resultRelInfo);
4459 [ + + ]: 957 : if (node->mt_merge_subcommands & MERGE_DELETE)
4460 : 259 : ExecBSDeleteTriggers(node->ps.state, resultRelInfo);
4461 : 957 : break;
6107 tgl@sss.pgh.pa.us 4462 :UBC 0 : default:
4463 [ # # ]: 0 : elog(ERROR, "unknown operation");
4464 : : break;
4465 : : }
6107 tgl@sss.pgh.pa.us 4466 :CBC 74471 : }
4467 : :
4468 : : /*
4469 : : * Process AFTER EACH STATEMENT triggers
4470 : : */
4471 : : static void
3289 rhodiumtoad@postgres 4472 : 72182 : fireASTriggers(ModifyTableState *node)
4473 : : {
3025 alvherre@alvh.no-ip. 4474 : 72182 : ModifyTable *plan = (ModifyTable *) node->ps.plan;
2080 heikki.linnakangas@i 4475 : 72182 : ResultRelInfo *resultRelInfo = node->rootResultRelInfo;
4476 : :
6107 tgl@sss.pgh.pa.us 4477 [ + + + + : 72182 : switch (node->operation)
- ]
4478 : : {
4479 : 54598 : case CMD_INSERT:
3025 alvherre@alvh.no-ip. 4480 [ + + ]: 54598 : if (plan->onConflictAction == ONCONFLICT_UPDATE)
4071 andres@anarazel.de 4481 : 545 : ExecASUpdateTriggers(node->ps.state,
4482 : : resultRelInfo,
3209 tgl@sss.pgh.pa.us 4483 : 545 : node->mt_oc_transition_capture);
3289 rhodiumtoad@postgres 4484 : 54598 : ExecASInsertTriggers(node->ps.state, resultRelInfo,
4485 : 54598 : node->mt_transition_capture);
6107 tgl@sss.pgh.pa.us 4486 : 54598 : break;
4487 : 8488 : case CMD_UPDATE:
3289 rhodiumtoad@postgres 4488 : 8488 : ExecASUpdateTriggers(node->ps.state, resultRelInfo,
4489 : 8488 : node->mt_transition_capture);
6107 tgl@sss.pgh.pa.us 4490 : 8488 : break;
4491 : 8241 : case CMD_DELETE:
3289 rhodiumtoad@postgres 4492 : 8241 : ExecASDeleteTriggers(node->ps.state, resultRelInfo,
4493 : 8241 : node->mt_transition_capture);
6107 tgl@sss.pgh.pa.us 4494 : 8241 : break;
1555 alvherre@alvh.no-ip. 4495 : 855 : case CMD_MERGE:
4496 [ + + ]: 855 : if (node->mt_merge_subcommands & MERGE_DELETE)
4497 : 234 : ExecASDeleteTriggers(node->ps.state, resultRelInfo,
4498 : 234 : node->mt_transition_capture);
4499 [ + + ]: 855 : if (node->mt_merge_subcommands & MERGE_UPDATE)
4500 : 564 : ExecASUpdateTriggers(node->ps.state, resultRelInfo,
4501 : 564 : node->mt_transition_capture);
4502 [ + + ]: 855 : if (node->mt_merge_subcommands & MERGE_INSERT)
4503 : 478 : ExecASInsertTriggers(node->ps.state, resultRelInfo,
4504 : 478 : node->mt_transition_capture);
4505 : 855 : break;
6107 tgl@sss.pgh.pa.us 4506 :UBC 0 : default:
4507 [ # # ]: 0 : elog(ERROR, "unknown operation");
4508 : : break;
4509 : : }
6107 tgl@sss.pgh.pa.us 4510 :CBC 72182 : }
4511 : :
4512 : : /*
4513 : : * Set up the state needed for collecting transition tuples for AFTER
4514 : : * triggers.
4515 : : */
4516 : : static void
3289 rhodiumtoad@postgres 4517 : 74737 : ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
4518 : : {
3025 alvherre@alvh.no-ip. 4519 : 74737 : ModifyTable *plan = (ModifyTable *) mtstate->ps.plan;
2080 heikki.linnakangas@i 4520 : 74737 : ResultRelInfo *targetRelInfo = mtstate->rootResultRelInfo;
4521 : :
4522 : : /* Check for transition tables on the directly targeted relation. */
3289 rhodiumtoad@postgres 4523 : 74737 : mtstate->mt_transition_capture =
3209 tgl@sss.pgh.pa.us 4524 : 74737 : MakeTransitionCaptureState(targetRelInfo->ri_TrigDesc,
4525 : 74737 : RelationGetRelid(targetRelInfo->ri_RelationDesc),
4526 : : mtstate->operation);
3025 alvherre@alvh.no-ip. 4527 [ + + ]: 74737 : if (plan->operation == CMD_INSERT &&
4528 [ + + ]: 55061 : plan->onConflictAction == ONCONFLICT_UPDATE)
3209 tgl@sss.pgh.pa.us 4529 : 621 : mtstate->mt_oc_transition_capture =
4530 : 621 : MakeTransitionCaptureState(targetRelInfo->ri_TrigDesc,
4531 : 621 : RelationGetRelid(targetRelInfo->ri_RelationDesc),
4532 : : CMD_UPDATE);
3084 rhaas@postgresql.org 4533 : 74737 : }
4534 : :
4535 : : /*
4536 : : * ExecPrepareTupleRouting --- prepare for routing one tuple
4537 : : *
4538 : : * Determine the partition in which the tuple in slot is to be inserted,
4539 : : * and return its ResultRelInfo in *partRelInfo. The return value is
4540 : : * a slot holding the tuple of the partition rowtype.
4541 : : *
4542 : : * This also sets the transition table information in mtstate based on the
4543 : : * selected partition.
4544 : : */
4545 : : static TupleTableSlot *
3025 alvherre@alvh.no-ip. 4546 : 481061 : ExecPrepareTupleRouting(ModifyTableState *mtstate,
4547 : : EState *estate,
4548 : : PartitionTupleRouting *proute,
4549 : : ResultRelInfo *targetRelInfo,
4550 : : TupleTableSlot *slot,
4551 : : ResultRelInfo **partRelInfo)
4552 : : {
4553 : : ResultRelInfo *partrel;
4554 : : TupleConversionMap *map;
4555 : :
4556 : : /*
4557 : : * Lookup the target partition's ResultRelInfo. If ExecFindPartition does
4558 : : * not find a valid partition for the tuple in 'slot' then an error is
4559 : : * raised. An error may also be raised if the found partition is not a
4560 : : * valid target for INSERTs. This is required since a partitioned table
4561 : : * UPDATE to another partition becomes a DELETE+INSERT.
4562 : : */
2783 4563 : 481061 : partrel = ExecFindPartition(mtstate, targetRelInfo, proute, slot, estate);
4564 : :
4565 : : /*
4566 : : * If we're capturing transition tuples, we might need to convert from the
4567 : : * partition rowtype to root partitioned table's rowtype. But if there
4568 : : * are no BEFORE triggers on the partition that could change the tuple, we
4569 : : * can just remember the original unconverted tuple to avoid a needless
4570 : : * round trip conversion.
4571 : : */
3025 4572 [ + + ]: 480917 : if (mtstate->mt_transition_capture != NULL)
4573 : : {
4574 : : bool has_before_insert_row_trig;
4575 : :
2080 heikki.linnakangas@i 4576 [ + + ]: 130 : has_before_insert_row_trig = (partrel->ri_TrigDesc &&
4577 [ + + ]: 28 : partrel->ri_TrigDesc->trig_insert_before_row);
4578 : :
4579 : 102 : mtstate->mt_transition_capture->tcs_original_insert_tuple =
4580 [ + + ]: 102 : !has_before_insert_row_trig ? slot : NULL;
4581 : : }
4582 : :
4583 : : /*
4584 : : * Convert the tuple, if necessary.
4585 : : */
1306 alvherre@alvh.no-ip. 4586 : 480917 : map = ExecGetRootToChildMap(partrel, estate);
2828 andres@anarazel.de 4587 [ + + ]: 480917 : if (map != NULL)
4588 : : {
2080 heikki.linnakangas@i 4589 : 45768 : TupleTableSlot *new_slot = partrel->ri_PartitionTupleSlot;
4590 : :
2828 andres@anarazel.de 4591 : 45768 : slot = execute_attr_map_slot(map->attrMap, slot, new_slot);
4592 : : }
4593 : :
2085 heikki.linnakangas@i 4594 : 480917 : *partRelInfo = partrel;
3025 alvherre@alvh.no-ip. 4595 : 480917 : return slot;
4596 : : }
4597 : :
4598 : : /* ----------------------------------------------------------------
4599 : : * ExecModifyTable
4600 : : *
4601 : : * Perform table modifications as required, and return RETURNING results
4602 : : * if needed.
4603 : : * ----------------------------------------------------------------
4604 : : */
4605 : : static TupleTableSlot *
3270 andres@anarazel.de 4606 : 79791 : ExecModifyTable(PlanState *pstate)
4607 : : {
4608 : 79791 : ModifyTableState *node = castNode(ModifyTableState, pstate);
4609 : : ModifyTableContext context;
5968 bruce@momjian.us 4610 : 79791 : EState *estate = node->ps.state;
4611 : 79791 : CmdType operation = node->operation;
4612 : : ResultRelInfo *resultRelInfo;
4613 : : PlanState *subplanstate;
4614 : : TupleTableSlot *slot;
4615 : : TupleTableSlot *oldSlot;
4616 : : ItemPointerData tuple_ctid;
4617 : : HeapTupleData oldtupdata;
4618 : : HeapTuple oldtuple;
4619 : : ItemPointer tupleid;
4620 : : bool tuplock;
4621 : :
3262 andres@anarazel.de 4622 [ + + ]: 79791 : CHECK_FOR_INTERRUPTS();
4623 : :
4624 : : /*
4625 : : * This should NOT get called during EvalPlanQual; we should have passed a
4626 : : * subplan tree to EvalPlanQual, instead. Use a runtime test not just
4627 : : * Assert because this condition is easy to miss in testing. (Note:
4628 : : * although ModifyTable should not get executed within an EvalPlanQual
4629 : : * operation, we do have to allow it to be initialized and shut down in
4630 : : * case it is within a CTE subplan. Hence this test must be here, not in
4631 : : * ExecInitModifyTable.)
4632 : : */
2490 4633 [ - + ]: 79791 : if (estate->es_epq_active != NULL)
5267 tgl@sss.pgh.pa.us 4634 [ # # ]:UBC 0 : elog(ERROR, "ModifyTable should not be called during EvalPlanQual");
4635 : :
4636 : : /*
4637 : : * If we've already completed processing, don't try to do more. We need
4638 : : * this test because ExecPostprocessPlan might call us an extra time, and
4639 : : * our subplan's nodes aren't necessarily robust against being called
4640 : : * extra times.
4641 : : */
5604 tgl@sss.pgh.pa.us 4642 [ + + ]:CBC 79791 : if (node->mt_done)
4643 : 584 : return NULL;
4644 : :
4645 : : /*
4646 : : * On first call, fire BEFORE STATEMENT triggers before proceeding.
4647 : : */
6107 4648 [ + + ]: 79207 : if (node->fireBSTriggers)
4649 : : {
4650 : 73348 : fireBSTriggers(node);
4651 : 73340 : node->fireBSTriggers = false;
4652 : : }
4653 : :
4654 : : /* Preload local variables */
1917 4655 : 79199 : resultRelInfo = node->resultRelInfo + node->mt_lastResultIndex;
4656 : 79199 : subplanstate = outerPlanState(node);
4657 : :
4658 : : /* Set global context */
1566 alvherre@alvh.no-ip. 4659 : 79199 : context.mtstate = node;
4660 : 79199 : context.epqstate = &node->mt_epqstate;
4661 : 79199 : context.estate = estate;
4662 : :
4663 : : /*
4664 : : * Fetch rows from subplan, and execute the required table modification
4665 : : * for each row.
4666 : : */
4667 : : for (;;)
4668 : : {
4669 : : /*
4670 : : * Reset the per-output-tuple exprcontext. This is needed because
4671 : : * triggers expect to use that context as workspace. It's a bit ugly
4672 : : * to do this below the top level of the plan, however. We might need
4673 : : * to rethink this later.
4674 : : */
5795 tgl@sss.pgh.pa.us 4675 [ + + ]: 11210249 : ResetPerTupleExprContext(estate);
4676 : :
4677 : : /*
4678 : : * Reset per-tuple memory context used for processing on conflict and
4679 : : * returning clauses, to free any expression evaluation storage
4680 : : * allocated in the previous cycle.
4681 : : */
2786 andres@anarazel.de 4682 [ + + ]: 11210249 : if (pstate->ps_ExprContext)
4683 : 2234029 : ResetExprContext(pstate->ps_ExprContext);
4684 : :
4685 : : /*
4686 : : * If there is a pending MERGE ... WHEN NOT MATCHED [BY TARGET] action
4687 : : * to execute, do so now --- see the comments in ExecMerge().
4688 : : */
822 dean.a.rasheed@gmail 4689 [ + + ]: 11210249 : if (node->mt_merge_pending_not_matched != NULL)
4690 : : {
4691 : 2 : context.planSlot = node->mt_merge_pending_not_matched;
530 4692 : 2 : context.cpDeletedSlot = NULL;
4693 : :
822 4694 : 2 : slot = ExecMergeNotMatched(&context, node->resultRelInfo,
4695 : 2 : node->canSetTag);
4696 : :
4697 : : /* Clear the pending action */
4698 : 2 : node->mt_merge_pending_not_matched = NULL;
4699 : :
4700 : : /*
4701 : : * If we got a RETURNING result, return it to the caller. We'll
4702 : : * continue the work on next call.
4703 : : */
4704 [ + - ]: 2 : if (slot)
4705 : 2 : return slot;
4706 : :
822 dean.a.rasheed@gmail 4707 :UBC 0 : continue; /* continue with the next tuple */
4708 : : }
4709 : :
4710 : : /* Fetch the next row from subplan */
1532 alvherre@alvh.no-ip. 4711 :CBC 11210247 : context.planSlot = ExecProcNode(subplanstate);
530 dean.a.rasheed@gmail 4712 : 11209949 : context.cpDeletedSlot = NULL;
4713 : :
4714 : : /* No more tuples to process? */
1532 alvherre@alvh.no-ip. 4715 [ + + + + ]: 11209949 : if (TupIsNull(context.planSlot))
4716 : : break;
4717 : :
4718 : : /*
4719 : : * When there are multiple result relations, each tuple contains a
4720 : : * junk column that gives the OID of the rel from which it came.
4721 : : * Extract it and select the correct result relation.
4722 : : */
1917 tgl@sss.pgh.pa.us 4723 [ + + ]: 11138881 : if (AttributeNumberIsValid(node->mt_resultOidAttno))
4724 : : {
4725 : : Datum datum;
4726 : : bool isNull;
4727 : : Oid resultoid;
4728 : :
1532 alvherre@alvh.no-ip. 4729 : 3445 : datum = ExecGetJunkAttribute(context.planSlot, node->mt_resultOidAttno,
4730 : : &isNull);
1917 tgl@sss.pgh.pa.us 4731 [ + + ]: 3445 : if (isNull)
4732 : : {
4733 : : /*
4734 : : * For commands other than MERGE, any tuples having InvalidOid
4735 : : * for tableoid are errors. For MERGE, we may need to handle
4736 : : * them as WHEN NOT MATCHED clauses if any, so do that.
4737 : : *
4738 : : * Note that we use the node's toplevel resultRelInfo, not any
4739 : : * specific partition's.
4740 : : */
1555 alvherre@alvh.no-ip. 4741 [ + - ]: 338 : if (operation == CMD_MERGE)
4742 : : {
1532 4743 : 338 : EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
4744 : :
835 dean.a.rasheed@gmail 4745 : 338 : slot = ExecMerge(&context, node->resultRelInfo,
4746 : 338 : NULL, NULL, node->canSetTag);
4747 : :
4748 : : /*
4749 : : * If we got a RETURNING result, return it to the caller.
4750 : : * We'll continue the work on next call.
4751 : : */
4752 [ + + ]: 330 : if (slot)
4753 : 25 : return slot;
4754 : :
4755 : 305 : continue; /* continue with the next tuple */
4756 : : }
4757 : :
1917 tgl@sss.pgh.pa.us 4758 [ # # ]:UBC 0 : elog(ERROR, "tableoid is NULL");
4759 : : }
1917 tgl@sss.pgh.pa.us 4760 :CBC 3107 : resultoid = DatumGetObjectId(datum);
4761 : :
4762 : : /* If it's not the same as last time, we need to locate the rel */
4763 [ + + ]: 3107 : if (resultoid != node->mt_lastResultOid)
1911 4764 : 2155 : resultRelInfo = ExecLookupResultRelByOid(node, resultoid,
4765 : : false, true);
4766 : : }
4767 : :
4768 : : /*
4769 : : * If we don't have a ForPortionOfState yet, we must be a partition or
4770 : : * inheritance child being hit for the first time. Make a copy from
4771 : : * the root, with our own TupleTableSlot. We do this lazily so that we
4772 : : * don't pay the price of unused partitions.
4773 : : */
22 peter@eisentraut.org 4774 [ + + ]:GNC 11138543 : if (((ModifyTable *) context.mtstate->ps.plan)->forPortionOf &&
4775 [ + + ]: 913 : !resultRelInfo->ri_forPortionOf)
4776 : 74 : ExecInitForPortionOf(context.mtstate, estate, resultRelInfo);
4777 : :
4778 : : /*
4779 : : * If resultRelInfo->ri_usesFdwDirectModify is true, all we need to do
4780 : : * here is compute the RETURNING expressions.
4781 : : */
3756 rhaas@postgresql.org 4782 [ + + ]:CBC 11138543 : if (resultRelInfo->ri_usesFdwDirectModify)
4783 : : {
4784 [ - + ]: 348 : Assert(resultRelInfo->ri_projectReturning);
4785 : :
4786 : : /*
4787 : : * A scan slot containing the data that was actually inserted,
4788 : : * updated or deleted has already been made available to
4789 : : * ExecProcessReturning by IterateDirectModify, so no need to
4790 : : * provide it here. The individual old and new slots are not
4791 : : * needed, since direct-modify is disabled if the RETURNING list
4792 : : * refers to OLD/NEW values.
4793 : : */
530 dean.a.rasheed@gmail 4794 [ + - - + ]: 348 : Assert((resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_OLD) == 0 &&
4795 : : (resultRelInfo->ri_projectReturning->pi_state.flags & EEO_FLAG_HAS_NEW) == 0);
4796 : :
138 dean.a.rasheed@gmail 4797 :GNC 348 : slot = ExecProcessReturning(&context, resultRelInfo,
4798 : : operation == CMD_DELETE,
4799 : : NULL, NULL, context.planSlot);
4800 : :
3756 rhaas@postgresql.org 4801 :CBC 348 : return slot;
4802 : : }
4803 : :
1532 alvherre@alvh.no-ip. 4804 : 11138195 : EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
4805 : 11138195 : slot = context.planSlot;
4806 : :
3137 tgl@sss.pgh.pa.us 4807 : 11138195 : tupleid = NULL;
4482 noah@leadboat.com 4808 : 11138195 : oldtuple = NULL;
4809 : :
4810 : : /*
4811 : : * For UPDATE/DELETE/MERGE, fetch the row identity info for the tuple
4812 : : * to be updated/deleted/merged. For a heap relation, that's a TID;
4813 : : * otherwise we may have a wholerow junk attr that carries the old
4814 : : * tuple in toto. Keep this in step with the part of
4815 : : * ExecInitModifyTable that sets up ri_RowIdAttNo.
4816 : : */
1555 alvherre@alvh.no-ip. 4817 [ + + + + : 11138195 : if (operation == CMD_UPDATE || operation == CMD_DELETE ||
+ + ]
4818 : : operation == CMD_MERGE)
4819 : : {
4820 : : char relkind;
4821 : : Datum datum;
4822 : : bool isNull;
4823 : :
1917 tgl@sss.pgh.pa.us 4824 : 3203647 : relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
4825 [ + + + + ]: 3203647 : if (relkind == RELKIND_RELATION ||
4826 [ + + ]: 339 : relkind == RELKIND_MATVIEW ||
4827 : : relkind == RELKIND_PARTITIONED_TABLE)
4828 : : {
4829 : : /*
4830 : : * ri_RowIdAttNo refers to a ctid attribute. See the comment
4831 : : * in ExecInitModifyTable().
4832 : : */
158 amitlan@postgresql.o 4833 [ - + - - ]: 3203312 : Assert(AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo) ||
4834 : : relkind == RELKIND_PARTITIONED_TABLE);
1917 tgl@sss.pgh.pa.us 4835 : 3203312 : datum = ExecGetJunkAttribute(slot,
4836 : 3203312 : resultRelInfo->ri_RowIdAttNo,
4837 : : &isNull);
4838 : :
4839 : : /*
4840 : : * For commands other than MERGE, any tuples having a null row
4841 : : * identifier are errors. For MERGE, we may need to handle
4842 : : * them as WHEN NOT MATCHED clauses if any, so do that.
4843 : : *
4844 : : * Note that we use the node's toplevel resultRelInfo, not any
4845 : : * specific partition's.
4846 : : */
4847 [ + + ]: 3203312 : if (isNull)
4848 : : {
1555 alvherre@alvh.no-ip. 4849 [ + - ]: 1421 : if (operation == CMD_MERGE)
4850 : : {
1532 4851 : 1421 : EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
4852 : :
835 dean.a.rasheed@gmail 4853 : 1421 : slot = ExecMerge(&context, node->resultRelInfo,
4854 : 1421 : NULL, NULL, node->canSetTag);
4855 : :
4856 : : /*
4857 : : * If we got a RETURNING result, return it to the
4858 : : * caller. We'll continue the work on next call.
4859 : : */
4860 [ + + ]: 1394 : if (slot)
4861 : 88 : return slot;
4862 : :
4863 : 1334 : continue; /* continue with the next tuple */
4864 : : }
4865 : :
1917 tgl@sss.pgh.pa.us 4866 [ # # ]:UBC 0 : elog(ERROR, "ctid is NULL");
4867 : : }
4868 : :
1917 tgl@sss.pgh.pa.us 4869 :CBC 3201891 : tupleid = (ItemPointer) DatumGetPointer(datum);
4870 : 3201891 : tuple_ctid = *tupleid; /* be sure we don't free ctid!! */
4871 : 3201891 : tupleid = &tuple_ctid;
4872 : : }
4873 : :
4874 : : /*
4875 : : * Use the wholerow attribute, when available, to reconstruct the
4876 : : * old relation tuple. The old tuple serves one or both of two
4877 : : * purposes: 1) it serves as the OLD tuple for row triggers, 2) it
4878 : : * provides values for any unchanged columns for the NEW tuple of
4879 : : * an UPDATE, because the subplan does not produce all the columns
4880 : : * of the target table.
4881 : : *
4882 : : * Note that the wholerow attribute does not carry system columns,
4883 : : * so foreign table triggers miss seeing those, except that we
4884 : : * know enough here to set t_tableOid. Quite separately from
4885 : : * this, the FDW may fetch its own junk attrs to identify the row.
4886 : : *
4887 : : * Other relevant relkinds, currently limited to views, always
4888 : : * have a wholerow attribute.
4889 : : */
4890 [ + + ]: 335 : else if (AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
4891 : : {
4892 : 320 : datum = ExecGetJunkAttribute(slot,
4893 : 320 : resultRelInfo->ri_RowIdAttNo,
4894 : : &isNull);
4895 : :
4896 : : /*
4897 : : * For commands other than MERGE, any tuples having a null row
4898 : : * identifier are errors. For MERGE, we may need to handle
4899 : : * them as WHEN NOT MATCHED clauses if any, so do that.
4900 : : *
4901 : : * Note that we use the node's toplevel resultRelInfo, not any
4902 : : * specific partition's.
4903 : : */
4904 [ + + ]: 320 : if (isNull)
4905 : : {
852 dean.a.rasheed@gmail 4906 [ + - ]: 32 : if (operation == CMD_MERGE)
4907 : : {
4908 : 32 : EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
4909 : :
835 4910 : 32 : slot = ExecMerge(&context, node->resultRelInfo,
4911 : 32 : NULL, NULL, node->canSetTag);
4912 : :
4913 : : /*
4914 : : * If we got a RETURNING result, return it to the
4915 : : * caller. We'll continue the work on next call.
4916 : : */
4917 [ + + ]: 28 : if (slot)
4918 : 8 : return slot;
4919 : :
4920 : 20 : continue; /* continue with the next tuple */
4921 : : }
4922 : :
1917 tgl@sss.pgh.pa.us 4923 [ # # ]:UBC 0 : elog(ERROR, "wholerow is NULL");
4924 : : }
4925 : :
1917 tgl@sss.pgh.pa.us 4926 :CBC 288 : oldtupdata.t_data = DatumGetHeapTupleHeader(datum);
4927 : 288 : oldtupdata.t_len =
4928 : 288 : HeapTupleHeaderGetDatumLength(oldtupdata.t_data);
4929 : 288 : ItemPointerSetInvalid(&(oldtupdata.t_self));
4930 : : /* Historically, view triggers see invalid t_tableOid. */
4931 : 288 : oldtupdata.t_tableOid =
4932 [ + + ]: 288 : (relkind == RELKIND_VIEW) ? InvalidOid :
4933 : 106 : RelationGetRelid(resultRelInfo->ri_RelationDesc);
4934 : :
4935 : 288 : oldtuple = &oldtupdata;
4936 : : }
4937 : : else
4938 : : {
4939 : : /* Only foreign tables are allowed to omit a row-ID attr */
4940 [ - + ]: 15 : Assert(relkind == RELKIND_FOREIGN_TABLE);
4941 : : }
4942 : : }
4943 : :
6107 4944 [ + + + + : 11136742 : switch (operation)
- ]
4945 : : {
4946 : 7934548 : case CMD_INSERT:
4947 : : /* Initialize projection info if first time for this table */
1911 4948 [ + + ]: 7934548 : if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
4949 : 54307 : ExecInitInsertProjection(node, resultRelInfo);
1532 alvherre@alvh.no-ip. 4950 : 7934548 : slot = ExecGetInsertNewTuple(resultRelInfo, context.planSlot);
1566 4951 : 7934548 : slot = ExecInsert(&context, resultRelInfo, slot,
1563 4952 : 7934548 : node->canSetTag, NULL, NULL);
6107 tgl@sss.pgh.pa.us 4953 : 7933116 : break;
4954 : :
4955 : 2209332 : case CMD_UPDATE:
644 noah@leadboat.com 4956 : 2209332 : tuplock = false;
4957 : :
4958 : : /* Initialize projection info if first time for this table */
1911 tgl@sss.pgh.pa.us 4959 [ + + ]: 2209332 : if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
4960 : 8735 : ExecInitUpdateProjection(node, resultRelInfo);
4961 : :
4962 : : /*
4963 : : * Make the new tuple by combining plan's output tuple with
4964 : : * the old tuple being updated.
4965 : : */
1917 4966 : 2209332 : oldSlot = resultRelInfo->ri_oldTupleSlot;
4967 [ + + ]: 2209332 : if (oldtuple != NULL)
4968 : : {
644 noah@leadboat.com 4969 [ - + ]: 180 : Assert(!resultRelInfo->ri_needLockTagTuple);
4970 : : /* Use the wholerow junk attr as the old tuple. */
1917 tgl@sss.pgh.pa.us 4971 : 180 : ExecForceStoreHeapTuple(oldtuple, oldSlot, false);
4972 : : }
4973 : : else
4974 : : {
4975 : : /* Fetch the most recent version of old tuple. */
4976 : 2209152 : Relation relation = resultRelInfo->ri_RelationDesc;
4977 : :
644 noah@leadboat.com 4978 [ + + ]: 2209152 : if (resultRelInfo->ri_needLockTagTuple)
4979 : : {
4980 : 15750 : LockTuple(relation, tupleid, InplaceUpdateTupleLock);
4981 : 15750 : tuplock = true;
4982 : : }
1917 tgl@sss.pgh.pa.us 4983 [ - + ]: 2209152 : if (!table_tuple_fetch_row_version(relation, tupleid,
4984 : : SnapshotAny,
4985 : : oldSlot))
1917 tgl@sss.pgh.pa.us 4986 [ # # ]:UBC 0 : elog(ERROR, "failed to fetch tuple being updated");
4987 : : }
1205 dean.a.rasheed@gmail 4988 :CBC 2209332 : slot = ExecGetUpdateNewTuple(resultRelInfo, context.planSlot,
4989 : : oldSlot);
4990 : :
4991 : : /* Now apply the update. */
1566 alvherre@alvh.no-ip. 4992 : 2209332 : slot = ExecUpdate(&context, resultRelInfo, tupleid, oldtuple,
530 dean.a.rasheed@gmail 4993 : 2209332 : oldSlot, slot, node->canSetTag);
644 noah@leadboat.com 4994 [ + + ]: 2208969 : if (tuplock)
4995 : 15750 : UnlockTuple(resultRelInfo->ri_RelationDesc, tupleid,
4996 : : InplaceUpdateTupleLock);
6107 tgl@sss.pgh.pa.us 4997 : 2208969 : break;
4998 : :
4999 : 984104 : case CMD_DELETE:
1566 alvherre@alvh.no-ip. 5000 : 984104 : slot = ExecDelete(&context, resultRelInfo, tupleid, oldtuple,
810 akorotkov@postgresql 5001 : 984104 : true, false, node->canSetTag, NULL, NULL, NULL);
6107 tgl@sss.pgh.pa.us 5002 : 984033 : break;
5003 : :
1555 alvherre@alvh.no-ip. 5004 : 8758 : case CMD_MERGE:
852 dean.a.rasheed@gmail 5005 : 8758 : slot = ExecMerge(&context, resultRelInfo, tupleid, oldtuple,
5006 : 8758 : node->canSetTag);
1555 alvherre@alvh.no-ip. 5007 : 8696 : break;
5008 : :
6107 tgl@sss.pgh.pa.us 5009 :UBC 0 : default:
5010 [ # # ]: 0 : elog(ERROR, "unknown operation");
5011 : : break;
5012 : : }
5013 : :
5014 : : /*
5015 : : * If we got a RETURNING result, return it to caller. We'll continue
5016 : : * the work on next call.
5017 : : */
6107 tgl@sss.pgh.pa.us 5018 [ + + ]:CBC 11134814 : if (slot)
5019 : 5403 : return slot;
5020 : : }
5021 : :
5022 : : /*
5023 : : * Insert remaining tuples for batch insert.
5024 : : */
1313 efujita@postgresql.o 5025 [ + + ]: 71068 : if (estate->es_insert_pending_result_relations != NIL)
5026 : 13 : ExecPendingInserts(estate);
5027 : :
5028 : : /*
5029 : : * We're done, but fire AFTER STATEMENT triggers before exiting.
5030 : : */
6107 tgl@sss.pgh.pa.us 5031 : 71067 : fireASTriggers(node);
5032 : :
5604 5033 : 71067 : node->mt_done = true;
5034 : :
6107 5035 : 71067 : return NULL;
5036 : : }
5037 : :
5038 : : /*
5039 : : * ExecLookupResultRelByOid
5040 : : * If the table with given OID is among the result relations to be
5041 : : * updated by the given ModifyTable node, return its ResultRelInfo.
5042 : : *
5043 : : * If not found, return NULL if missing_ok, else raise error.
5044 : : *
5045 : : * If update_cache is true, then upon successful lookup, update the node's
5046 : : * one-element cache. ONLY ExecModifyTable may pass true for this.
5047 : : */
5048 : : ResultRelInfo *
1911 5049 : 8176 : ExecLookupResultRelByOid(ModifyTableState *node, Oid resultoid,
5050 : : bool missing_ok, bool update_cache)
5051 : : {
5052 [ + + ]: 8176 : if (node->mt_resultOidHash)
5053 : : {
5054 : : /* Use the pre-built hash table to locate the rel */
5055 : : MTTargetRelLookup *mtlookup;
5056 : :
5057 : : mtlookup = (MTTargetRelLookup *)
5058 : 741 : hash_search(node->mt_resultOidHash, &resultoid, HASH_FIND, NULL);
5059 [ + - ]: 741 : if (mtlookup)
5060 : : {
5061 [ + + ]: 741 : if (update_cache)
5062 : : {
5063 : 541 : node->mt_lastResultOid = resultoid;
5064 : 541 : node->mt_lastResultIndex = mtlookup->relationIndex;
5065 : : }
5066 : 741 : return node->resultRelInfo + mtlookup->relationIndex;
5067 : : }
5068 : : }
5069 : : else
5070 : : {
5071 : : /* With few target rels, just search the ResultRelInfo array */
5072 [ + + ]: 13588 : for (int ndx = 0; ndx < node->mt_nrels; ndx++)
5073 : : {
5074 : 7973 : ResultRelInfo *rInfo = node->resultRelInfo + ndx;
5075 : :
5076 [ + + ]: 7973 : if (RelationGetRelid(rInfo->ri_RelationDesc) == resultoid)
5077 : : {
5078 [ + + ]: 1820 : if (update_cache)
5079 : : {
5080 : 1614 : node->mt_lastResultOid = resultoid;
5081 : 1614 : node->mt_lastResultIndex = ndx;
5082 : : }
5083 : 1820 : return rInfo;
5084 : : }
5085 : : }
5086 : : }
5087 : :
5088 [ - + ]: 5615 : if (!missing_ok)
1911 tgl@sss.pgh.pa.us 5089 [ # # ]:UBC 0 : elog(ERROR, "incorrect result relation OID %u", resultoid);
1911 tgl@sss.pgh.pa.us 5090 :CBC 5615 : return NULL;
5091 : : }
5092 : :
5093 : : /* ----------------------------------------------------------------
5094 : : * ExecInitModifyTable
5095 : : * ----------------------------------------------------------------
5096 : : */
5097 : : ModifyTableState *
6107 5098 : 74271 : ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
5099 : : {
5100 : : ModifyTableState *mtstate;
1917 5101 : 74271 : Plan *subplan = outerPlan(node);
6107 5102 : 74271 : CmdType operation = node->operation;
468 amitlan@postgresql.o 5103 : 74271 : int total_nrels = list_length(node->resultRelations);
5104 : : int nrels;
508 5105 : 74271 : List *resultRelations = NIL;
5106 : 74271 : List *withCheckOptionLists = NIL;
5107 : 74271 : List *returningLists = NIL;
5108 : 74271 : List *updateColnosLists = NIL;
498 5109 : 74271 : List *mergeActionLists = NIL;
5110 : 74271 : List *mergeJoinConditions = NIL;
7 5111 : 74271 : List *fdwPrivLists = NIL;
5112 : 74271 : Bitmapset *fdwDirectModifyPlans = NULL;
5113 : : ResultRelInfo *resultRelInfo;
5114 : : List *arowmarks;
5115 : : ListCell *l;
5116 : : int i;
5117 : : Relation rel;
5118 : :
5119 : : /* check for unsupported flags */
6107 tgl@sss.pgh.pa.us 5120 [ - + ]: 74271 : Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
5121 : :
5122 : : /*
5123 : : * Only consider unpruned relations for initializing their ResultRelInfo
5124 : : * struct and other fields such as withCheckOptions, etc.
5125 : : *
5126 : : * Note: We must avoid pruning every result relation. This is important
5127 : : * for MERGE, since even if every result relation is pruned from the
5128 : : * subplan, there might still be NOT MATCHED rows, for which there may be
5129 : : * INSERT actions to perform. To allow these actions to be found, at
5130 : : * least one result relation must be kept. Also, when inserting into a
5131 : : * partitioned table, ExecInitPartitionInfo() needs a ResultRelInfo struct
5132 : : * as a reference for building the ResultRelInfo of the target partition.
5133 : : * In either case, it doesn't matter which result relation is kept, so we
5134 : : * just keep the first one, if all others have been pruned. See also,
5135 : : * ExecDoInitialPruning(), which ensures that this first result relation
5136 : : * has been locked.
5137 : : */
508 amitlan@postgresql.o 5138 : 74271 : i = 0;
5139 [ + - + + : 150225 : foreach(l, node->resultRelations)
+ + ]
5140 : : {
5141 : 75954 : Index rti = lfirst_int(l);
5142 : : bool keep_rel;
5143 : :
468 5144 : 75954 : keep_rel = bms_is_member(rti, estate->es_unpruned_relids);
5145 [ + + + + : 75954 : if (!keep_rel && i == total_nrels - 1 && resultRelations == NIL)
+ + ]
5146 : : {
5147 : : /* all result relations pruned; keep the first one */
5148 : 32 : keep_rel = true;
5149 : 32 : rti = linitial_int(node->resultRelations);
5150 : 32 : i = 0;
5151 : : }
5152 : :
5153 [ + + ]: 75954 : if (keep_rel)
5154 : : {
7 5155 : 75893 : List *fdwPrivList = (List *) list_nth(node->fdwPrivLists, i);
5156 : :
508 5157 : 75893 : resultRelations = lappend_int(resultRelations, rti);
5158 [ + + ]: 75893 : if (node->withCheckOptionLists)
5159 : : {
5160 : 1052 : List *withCheckOptions = list_nth_node(List,
5161 : : node->withCheckOptionLists,
5162 : : i);
5163 : :
5164 : 1052 : withCheckOptionLists = lappend(withCheckOptionLists, withCheckOptions);
5165 : : }
5166 [ + + ]: 75893 : if (node->returningLists)
5167 : : {
5168 : 4074 : List *returningList = list_nth_node(List,
5169 : : node->returningLists,
5170 : : i);
5171 : :
5172 : 4074 : returningLists = lappend(returningLists, returningList);
5173 : : }
5174 [ + + ]: 75893 : if (node->updateColnosLists)
5175 : : {
5176 : 10598 : List *updateColnosList = list_nth(node->updateColnosLists, i);
5177 : :
5178 : 10598 : updateColnosLists = lappend(updateColnosLists, updateColnosList);
5179 : : }
498 5180 [ + + ]: 75893 : if (node->mergeActionLists)
5181 : : {
5182 : 1220 : List *mergeActionList = list_nth(node->mergeActionLists, i);
5183 : :
5184 : 1220 : mergeActionLists = lappend(mergeActionLists, mergeActionList);
5185 : : }
5186 [ + + ]: 75893 : if (node->mergeJoinConditions)
5187 : : {
5188 : 1220 : List *mergeJoinCondition = list_nth(node->mergeJoinConditions, i);
5189 : :
5190 : 1220 : mergeJoinConditions = lappend(mergeJoinConditions, mergeJoinCondition);
5191 : : }
5192 : :
5193 : : /*
5194 : : * fdwPrivLists/fdwDirectModifyPlans are re-indexed to match
5195 : : * resultRelations
5196 : : */
7 5197 : 75893 : fdwPrivLists = lappend(fdwPrivLists, fdwPrivList);
5198 [ + + ]: 75893 : if (bms_is_member(i, node->fdwDirectModifyPlans))
5199 : : {
5200 : 110 : int new_index = list_length(resultRelations) - 1;
5201 : :
5202 : 110 : fdwDirectModifyPlans = bms_add_member(fdwDirectModifyPlans,
5203 : : new_index);
5204 : : }
5205 : : }
508 5206 : 75954 : i++;
5207 : : }
5208 : 74271 : nrels = list_length(resultRelations);
468 5209 [ - + ]: 74271 : Assert(nrels > 0);
5210 : :
5211 : : /*
5212 : : * create state structure
5213 : : */
6107 tgl@sss.pgh.pa.us 5214 : 74271 : mtstate = makeNode(ModifyTableState);
5215 : 74271 : mtstate->ps.plan = (Plan *) node;
5216 : 74271 : mtstate->ps.state = estate;
3270 andres@anarazel.de 5217 : 74271 : mtstate->ps.ExecProcNode = ExecModifyTable;
5218 : :
5604 tgl@sss.pgh.pa.us 5219 : 74271 : mtstate->operation = operation;
5220 : 74271 : mtstate->canSetTag = node->canSetTag;
5221 : 74271 : mtstate->mt_done = false;
5222 : :
1917 5223 : 74271 : mtstate->mt_nrels = nrels;
202 michael@paquier.xyz 5224 :GNC 74271 : mtstate->resultRelInfo = palloc_array(ResultRelInfo, nrels);
5225 : :
822 dean.a.rasheed@gmail 5226 :CBC 74271 : mtstate->mt_merge_pending_not_matched = NULL;
1555 alvherre@alvh.no-ip. 5227 : 74271 : mtstate->mt_merge_inserted = 0;
5228 : 74271 : mtstate->mt_merge_updated = 0;
5229 : 74271 : mtstate->mt_merge_deleted = 0;
508 amitlan@postgresql.o 5230 : 74271 : mtstate->mt_updateColnosLists = updateColnosLists;
498 5231 : 74271 : mtstate->mt_mergeActionLists = mergeActionLists;
5232 : 74271 : mtstate->mt_mergeJoinConditions = mergeJoinConditions;
7 amitlan@postgresql.o 5233 :GNC 74271 : mtstate->mt_fdwPrivLists = fdwPrivLists;
5234 : :
5235 : : /*----------
5236 : : * Resolve the target relation. This is the same as:
5237 : : *
5238 : : * - the relation for which we will fire FOR STATEMENT triggers,
5239 : : * - the relation into whose tuple format all captured transition tuples
5240 : : * must be converted, and
5241 : : * - the root partitioned table used for tuple routing.
5242 : : *
5243 : : * If it's a partitioned or inherited table, the root partition or
5244 : : * appendrel RTE doesn't appear elsewhere in the plan and its RT index is
5245 : : * given explicitly in node->rootRelation. Otherwise, the target relation
5246 : : * is the sole relation in the node->resultRelations list and, since it can
5247 : : * never be pruned, also in the resultRelations list constructed above.
5248 : : *----------
5249 : : */
2086 heikki.linnakangas@i 5250 [ + + ]:CBC 74271 : if (node->rootRelation > 0)
5251 : : {
508 amitlan@postgresql.o 5252 [ - + ]: 1951 : Assert(bms_is_member(node->rootRelation, estate->es_unpruned_relids));
2086 heikki.linnakangas@i 5253 : 1951 : mtstate->rootResultRelInfo = makeNode(ResultRelInfo);
5254 : 1951 : ExecInitResultRelation(estate, mtstate->rootResultRelInfo,
5255 : : node->rootRelation);
5256 : : }
5257 : : else
5258 : : {
980 tgl@sss.pgh.pa.us 5259 [ - + ]: 72320 : Assert(list_length(node->resultRelations) == 1);
498 amitlan@postgresql.o 5260 [ - + ]: 72320 : Assert(list_length(resultRelations) == 1);
2080 heikki.linnakangas@i 5261 : 72320 : mtstate->rootResultRelInfo = mtstate->resultRelInfo;
5262 : 72320 : ExecInitResultRelation(estate, mtstate->resultRelInfo,
498 amitlan@postgresql.o 5263 : 72320 : linitial_int(resultRelations));
5264 : : }
5265 : :
5266 : : /* set up epqstate with dummy subplan data for the moment */
1138 tgl@sss.pgh.pa.us 5267 : 74271 : EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL,
5268 : : node->epqParam, resultRelations);
6107 5269 : 74271 : mtstate->fireBSTriggers = true;
5270 : :
5271 : : /*
5272 : : * Build state for collecting transition tuples. This requires having a
5273 : : * valid trigger query context, so skip it in explain-only mode.
5274 : : */
2080 heikki.linnakangas@i 5275 [ + + ]: 74271 : if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
5276 : 73606 : ExecSetupTransitionCaptureState(mtstate, estate);
5277 : :
5278 : : /*
5279 : : * Open all the result relations and initialize the ResultRelInfo structs.
5280 : : * (But root relation was initialized above, if it's part of the array.)
5281 : : * We must do this before initializing the subplan, because direct-modify
5282 : : * FDWs expect their ResultRelInfos to be available.
5283 : : */
5604 tgl@sss.pgh.pa.us 5284 : 74271 : resultRelInfo = mtstate->resultRelInfo;
6107 5285 : 74271 : i = 0;
508 amitlan@postgresql.o 5286 [ + - + + : 149932 : foreach(l, resultRelations)
+ + ]
5287 : : {
2086 heikki.linnakangas@i 5288 : 75889 : Index resultRelation = lfirst_int(l);
852 dean.a.rasheed@gmail 5289 : 75889 : List *mergeActions = NIL;
5290 : :
498 amitlan@postgresql.o 5291 [ + + ]: 75889 : if (mergeActionLists)
5292 : 1220 : mergeActions = list_nth(mergeActionLists, i);
5293 : :
2080 heikki.linnakangas@i 5294 [ + + ]: 75889 : if (resultRelInfo != mtstate->rootResultRelInfo)
5295 : : {
5296 : 3569 : ExecInitResultRelation(estate, resultRelInfo, resultRelation);
5297 : :
5298 : : /*
5299 : : * For child result relations, store the root result relation
5300 : : * pointer. We do so for the convenience of places that want to
5301 : : * look at the query's original target relation but don't have the
5302 : : * mtstate handy.
5303 : : */
1911 tgl@sss.pgh.pa.us 5304 : 3569 : resultRelInfo->ri_RootResultRelInfo = mtstate->rootResultRelInfo;
5305 : : }
5306 : :
5307 : : /* Initialize the usesFdwDirectModify flag */
1566 alvherre@alvh.no-ip. 5308 : 75889 : resultRelInfo->ri_usesFdwDirectModify =
7 amitlan@postgresql.o 5309 : 75889 : bms_is_member(i, fdwDirectModifyPlans);
5310 : :
5311 : : /*
5312 : : * Verify result relation is a valid target for the current operation
5313 : : */
299 dean.a.rasheed@gmail 5314 : 75889 : CheckValidResultRel(resultRelInfo, operation, node->onConflictAction,
5315 : : mergeActions, node);
5316 : :
1917 tgl@sss.pgh.pa.us 5317 : 75661 : resultRelInfo++;
5318 : 75661 : i++;
5319 : : }
5320 : :
5321 : : /*
5322 : : * Now we may initialize the subplan.
5323 : : */
5324 : 74043 : outerPlanState(mtstate) = ExecInitNode(subplan, estate, eflags);
5325 : :
5326 : : /*
5327 : : * Do additional per-result-relation initialization.
5328 : : */
5329 [ + + ]: 149682 : for (i = 0; i < nrels; i++)
5330 : : {
5331 : 75639 : resultRelInfo = &mtstate->resultRelInfo[i];
5332 : :
5333 : : /* Let FDWs init themselves for foreign-table result rels */
3756 rhaas@postgresql.org 5334 [ + + ]: 75639 : if (!resultRelInfo->ri_usesFdwDirectModify &&
5335 [ + + ]: 75533 : resultRelInfo->ri_FdwRoutine != NULL &&
4860 tgl@sss.pgh.pa.us 5336 [ + - ]: 172 : resultRelInfo->ri_FdwRoutine->BeginForeignModify != NULL)
5337 : : {
7 amitlan@postgresql.o 5338 : 172 : List *fdw_private = (List *) list_nth(fdwPrivLists, i);
5339 : :
4860 tgl@sss.pgh.pa.us 5340 : 172 : resultRelInfo->ri_FdwRoutine->BeginForeignModify(mtstate,
5341 : : resultRelInfo,
5342 : : fdw_private,
5343 : : i,
5344 : : eflags);
5345 : : }
5346 : :
5347 : : /*
5348 : : * For UPDATE/DELETE/MERGE, find the appropriate junk attr now, either
5349 : : * a 'ctid' or 'wholerow' attribute depending on relkind. For foreign
5350 : : * tables, the FDW might have created additional junk attr(s), but
5351 : : * those are no concern of ours.
5352 : : */
1555 alvherre@alvh.no-ip. 5353 [ + + + + : 75639 : if (operation == CMD_UPDATE || operation == CMD_DELETE ||
+ + ]
5354 : : operation == CMD_MERGE)
5355 : : {
5356 : : char relkind;
5357 : :
1911 tgl@sss.pgh.pa.us 5358 : 20397 : relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
5359 [ + + + + ]: 20397 : if (relkind == RELKIND_RELATION ||
5360 [ + + ]: 410 : relkind == RELKIND_MATVIEW ||
5361 : : relkind == RELKIND_PARTITIONED_TABLE)
5362 : : {
5363 : 20017 : resultRelInfo->ri_RowIdAttNo =
5364 : 20017 : ExecFindJunkAttributeInTlist(subplan->targetlist, "ctid");
5365 : :
5366 : : /*
5367 : : * For heap relations, a ctid junk attribute must be present.
5368 : : * Partitioned tables should only appear here when all leaf
5369 : : * partitions were pruned, in which case no rows can be
5370 : : * produced and ctid is not needed.
5371 : : */
158 amitlan@postgresql.o 5372 [ + + ]: 20017 : if (relkind == RELKIND_PARTITIONED_TABLE)
5373 [ - + ]: 30 : Assert(nrels == 1);
5374 [ - + ]: 19987 : else if (!AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
1911 tgl@sss.pgh.pa.us 5375 [ # # ]:UBC 0 : elog(ERROR, "could not find junk ctid column");
5376 : : }
1911 tgl@sss.pgh.pa.us 5377 [ + + ]:CBC 380 : else if (relkind == RELKIND_FOREIGN_TABLE)
5378 : : {
5379 : : /*
5380 : : * We don't support MERGE with foreign tables for now. (It's
5381 : : * problematic because the implementation uses CTID.)
5382 : : */
1555 alvherre@alvh.no-ip. 5383 [ - + ]: 190 : Assert(operation != CMD_MERGE);
5384 : :
5385 : : /*
5386 : : * When there is a row-level trigger, there should be a
5387 : : * wholerow attribute. We also require it to be present in
5388 : : * UPDATE and MERGE, so we can get the values of unchanged
5389 : : * columns.
5390 : : */
1911 tgl@sss.pgh.pa.us 5391 : 190 : resultRelInfo->ri_RowIdAttNo =
5392 : 190 : ExecFindJunkAttributeInTlist(subplan->targetlist,
5393 : : "wholerow");
1555 alvherre@alvh.no-ip. 5394 [ + + - + ]: 190 : if ((mtstate->operation == CMD_UPDATE || mtstate->operation == CMD_MERGE) &&
1911 tgl@sss.pgh.pa.us 5395 [ - + ]: 109 : !AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
1911 tgl@sss.pgh.pa.us 5396 [ # # ]:UBC 0 : elog(ERROR, "could not find junk wholerow column");
5397 : : }
5398 : : else
5399 : : {
5400 : : /* Other valid target relkinds must provide wholerow */
1911 tgl@sss.pgh.pa.us 5401 :CBC 190 : resultRelInfo->ri_RowIdAttNo =
5402 : 190 : ExecFindJunkAttributeInTlist(subplan->targetlist,
5403 : : "wholerow");
5404 [ - + ]: 190 : if (!AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
1911 tgl@sss.pgh.pa.us 5405 [ # # ]:UBC 0 : elog(ERROR, "could not find junk wholerow column");
5406 : : }
5407 : : }
5408 : : }
5409 : :
5410 : : /*
5411 : : * If this is an inherited update/delete/merge, there will be a junk
5412 : : * attribute named "tableoid" present in the subplan's targetlist. It
5413 : : * will be used to identify the result relation for a given tuple to be
5414 : : * updated/deleted/merged.
5415 : : */
1911 tgl@sss.pgh.pa.us 5416 :CBC 74043 : mtstate->mt_resultOidAttno =
5417 : 74043 : ExecFindJunkAttributeInTlist(subplan->targetlist, "tableoid");
468 amitlan@postgresql.o 5418 [ + + - + ]: 74043 : Assert(AttributeNumberIsValid(mtstate->mt_resultOidAttno) || total_nrels == 1);
1911 tgl@sss.pgh.pa.us 5419 : 74043 : mtstate->mt_lastResultOid = InvalidOid; /* force lookup at first tuple */
5420 : 74043 : mtstate->mt_lastResultIndex = 0; /* must be zero if no such attr */
5421 : :
5422 : : /* Get the root target relation */
2080 heikki.linnakangas@i 5423 : 74043 : rel = mtstate->rootResultRelInfo->ri_RelationDesc;
5424 : :
5425 : : /*
5426 : : * Build state for tuple routing if it's a partitioned INSERT. An UPDATE
5427 : : * or MERGE might need this too, but only if it actually moves tuples
5428 : : * between partitions; in that case setup is done by
5429 : : * ExecCrossPartitionUpdate.
5430 : : */
3084 rhaas@postgresql.org 5431 [ + + + + ]: 74043 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
5432 : : operation == CMD_INSERT)
3050 5433 : 3766 : mtstate->mt_partition_tuple_routing =
1911 tgl@sss.pgh.pa.us 5434 : 3766 : ExecSetupPartitionTupleRouting(estate, rel);
5435 : :
5436 : : /*
5437 : : * Initialize any WITH CHECK OPTION constraints if needed.
5438 : : */
4730 sfrost@snowman.net 5439 : 74043 : resultRelInfo = mtstate->resultRelInfo;
508 amitlan@postgresql.o 5440 [ + + + + : 75095 : foreach(l, withCheckOptionLists)
+ + ]
5441 : : {
4730 sfrost@snowman.net 5442 : 1052 : List *wcoList = (List *) lfirst(l);
5443 : 1052 : List *wcoExprs = NIL;
5444 : : ListCell *ll;
5445 : :
5446 [ + - + + : 3115 : foreach(ll, wcoList)
+ + ]
5447 : : {
5448 : 2063 : WithCheckOption *wco = (WithCheckOption *) lfirst(ll);
3395 andres@anarazel.de 5449 : 2063 : ExprState *wcoExpr = ExecInitQual((List *) wco->qual,
5450 : : &mtstate->ps);
5451 : :
4730 sfrost@snowman.net 5452 : 2063 : wcoExprs = lappend(wcoExprs, wcoExpr);
5453 : : }
5454 : :
5455 : 1052 : resultRelInfo->ri_WithCheckOptions = wcoList;
5456 : 1052 : resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
5457 : 1052 : resultRelInfo++;
5458 : : }
5459 : :
5460 : : /*
5461 : : * Initialize RETURNING projections if needed.
5462 : : */
508 amitlan@postgresql.o 5463 [ + + ]: 74043 : if (returningLists)
5464 : : {
5465 : : TupleTableSlot *slot;
5466 : : ExprContext *econtext;
5467 : :
5468 : : /*
5469 : : * Initialize result tuple slot and assign its rowtype using the plan
5470 : : * node's declared targetlist, which the planner set up to be the same
5471 : : * as the first (before runtime pruning) RETURNING list. We assume
5472 : : * all the result rels will produce compatible output.
5473 : : */
2784 andres@anarazel.de 5474 : 3859 : ExecInitResultTupleSlotTL(&mtstate->ps, &TTSOpsVirtual);
6107 tgl@sss.pgh.pa.us 5475 : 3859 : slot = mtstate->ps.ps_ResultTupleSlot;
5476 : :
5477 : : /* Need an econtext too */
3395 andres@anarazel.de 5478 [ + - ]: 3859 : if (mtstate->ps.ps_ExprContext == NULL)
5479 : 3859 : ExecAssignExprContext(estate, &mtstate->ps);
5480 : 3859 : econtext = mtstate->ps.ps_ExprContext;
5481 : :
5482 : : /*
5483 : : * Build a projection for each result rel.
5484 : : */
5604 tgl@sss.pgh.pa.us 5485 : 3859 : resultRelInfo = mtstate->resultRelInfo;
508 amitlan@postgresql.o 5486 [ + - + + : 7933 : foreach(l, returningLists)
+ + ]
5487 : : {
6107 tgl@sss.pgh.pa.us 5488 : 4074 : List *rlist = (List *) lfirst(l);
5489 : :
3007 rhaas@postgresql.org 5490 : 4074 : resultRelInfo->ri_returningList = rlist;
6107 tgl@sss.pgh.pa.us 5491 : 4074 : resultRelInfo->ri_projectReturning =
3395 andres@anarazel.de 5492 : 4074 : ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
3296 tgl@sss.pgh.pa.us 5493 : 4074 : resultRelInfo->ri_RelationDesc->rd_att);
6107 5494 : 4074 : resultRelInfo++;
5495 : : }
5496 : : }
5497 : : else
5498 : : {
5499 : : /*
5500 : : * We still must construct a dummy result tuple type, because InitPlan
5501 : : * expects one (maybe should change that?).
5502 : : */
2790 andres@anarazel.de 5503 : 70184 : ExecInitResultTypeTL(&mtstate->ps);
5504 : :
6107 tgl@sss.pgh.pa.us 5505 : 70184 : mtstate->ps.ps_ExprContext = NULL;
5506 : : }
5507 : :
5508 : : /* Set the list of arbiter indexes if needed for ON CONFLICT */
3018 alvherre@alvh.no-ip. 5509 : 74043 : resultRelInfo = mtstate->resultRelInfo;
5510 [ + + ]: 74043 : if (node->onConflictAction != ONCONFLICT_NONE)
5511 : : {
5512 : : /* insert may only have one relation, inheritance is not expanded */
468 amitlan@postgresql.o 5513 [ - + ]: 1193 : Assert(total_nrels == 1);
3018 alvherre@alvh.no-ip. 5514 : 1193 : resultRelInfo->ri_onConflictArbiterIndexes = node->arbiterIndexes;
5515 : : }
5516 : :
5517 : : /*
5518 : : * For ON CONFLICT DO SELECT/UPDATE, initialize the ON CONFLICT action
5519 : : * state.
5520 : : */
138 dean.a.rasheed@gmail 5521 [ + + ]:GNC 74043 : if (node->onConflictAction == ONCONFLICT_UPDATE ||
5522 [ + + ]: 73374 : node->onConflictAction == ONCONFLICT_SELECT)
5523 : : {
5524 : 889 : OnConflictActionState *onconfl = makeNode(OnConflictActionState);
5525 : :
5526 : : /* already exists if created by RETURNING processing above */
4071 andres@anarazel.de 5527 [ + + ]:CBC 889 : if (mtstate->ps.ps_ExprContext == NULL)
5528 : 453 : ExecAssignExprContext(estate, &mtstate->ps);
5529 : :
5530 : : /* action state for DO SELECT/UPDATE */
1877 tgl@sss.pgh.pa.us 5531 : 889 : resultRelInfo->ri_onConflict = onconfl;
5532 : :
5533 : : /* lock strength for DO SELECT [FOR UPDATE/SHARE] */
138 dean.a.rasheed@gmail 5534 :GNC 889 : onconfl->oc_LockStrength = node->onConflictLockStrength;
5535 : :
5536 : : /* initialize slot for the existing tuple */
1877 tgl@sss.pgh.pa.us 5537 :CBC 889 : onconfl->oc_Existing =
2668 andres@anarazel.de 5538 : 889 : table_slot_create(resultRelInfo->ri_RelationDesc,
5539 : 889 : &mtstate->ps.state->es_tupleTable);
5540 : :
5541 : : /*
5542 : : * For ON CONFLICT DO UPDATE, initialize target list and projection.
5543 : : */
138 dean.a.rasheed@gmail 5544 [ + + ]:GNC 889 : if (node->onConflictAction == ONCONFLICT_UPDATE)
5545 : : {
5546 : : ExprContext *econtext;
5547 : : TupleDesc relationDesc;
5548 : :
5549 : 669 : econtext = mtstate->ps.ps_ExprContext;
5550 : 669 : relationDesc = resultRelInfo->ri_RelationDesc->rd_att;
5551 : :
5552 : : /*
5553 : : * Create the tuple slot for the UPDATE SET projection. We want a
5554 : : * slot of the table's type here, because the slot will be used to
5555 : : * insert into the table, and for RETURNING processing - which may
5556 : : * access system attributes.
5557 : : */
5558 : 669 : onconfl->oc_ProjSlot =
5559 : 669 : table_slot_create(resultRelInfo->ri_RelationDesc,
5560 : 669 : &mtstate->ps.state->es_tupleTable);
5561 : :
5562 : : /* build UPDATE SET projection state */
5563 : 669 : onconfl->oc_ProjInfo =
5564 : 669 : ExecBuildUpdateProjection(node->onConflictSet,
5565 : : true,
5566 : : node->onConflictCols,
5567 : : relationDesc,
5568 : : econtext,
5569 : : onconfl->oc_ProjSlot,
5570 : : &mtstate->ps);
5571 : : }
5572 : :
5573 : : /* initialize state to evaluate the WHERE clause, if any */
4071 andres@anarazel.de 5574 [ + + ]:CBC 889 : if (node->onConflictWhere)
5575 : : {
5576 : : ExprState *qualexpr;
5577 : :
3395 5578 : 207 : qualexpr = ExecInitQual((List *) node->onConflictWhere,
5579 : : &mtstate->ps);
1877 tgl@sss.pgh.pa.us 5580 : 207 : onconfl->oc_WhereClause = qualexpr;
5581 : : }
5582 : : }
5583 : :
5584 : : /*
5585 : : * If needed, initialize the target range for FOR PORTION OF.
5586 : : */
90 peter@eisentraut.org 5587 [ + + ]:GNC 74043 : if (node->forPortionOf)
5588 : : {
5589 : : ResultRelInfo *rootRelInfo;
5590 : : TupleDesc tupDesc;
5591 : : ForPortionOfExpr *forPortionOf;
5592 : : Datum targetRange;
5593 : : bool isNull;
5594 : : ExprContext *econtext;
5595 : : ExprState *exprState;
5596 : : ForPortionOfState *fpoState;
5597 : :
5598 : 856 : rootRelInfo = mtstate->resultRelInfo;
5599 [ + + ]: 856 : if (rootRelInfo->ri_RootResultRelInfo)
5600 : 74 : rootRelInfo = rootRelInfo->ri_RootResultRelInfo;
5601 : :
5602 : 856 : tupDesc = rootRelInfo->ri_RelationDesc->rd_att;
5603 : 856 : forPortionOf = (ForPortionOfExpr *) node->forPortionOf;
5604 : :
5605 : : /* Eval the FOR PORTION OF target */
5606 [ + + ]: 856 : if (mtstate->ps.ps_ExprContext == NULL)
5607 : 836 : ExecAssignExprContext(estate, &mtstate->ps);
5608 : 856 : econtext = mtstate->ps.ps_ExprContext;
5609 : :
5610 : 856 : exprState = ExecPrepareExpr((Expr *) forPortionOf->targetRange, estate);
5611 : 856 : targetRange = ExecEvalExpr(exprState, econtext, &isNull);
5612 : :
5613 : : /*
5614 : : * FOR PORTION OF ... TO ... FROM should never give us a NULL target,
5615 : : * but FOR PORTION OF (...) could.
5616 : : */
5617 [ + + ]: 856 : if (isNull)
5618 [ + - ]: 16 : ereport(ERROR,
5619 : : (errmsg("FOR PORTION OF target was null")),
5620 : : executor_errposition(estate, forPortionOf->targetLocation));
5621 : :
5622 : : /* Create state for FOR PORTION OF operation */
5623 : :
5624 : 840 : fpoState = makeNode(ForPortionOfState);
5625 : 840 : fpoState->fp_rangeName = forPortionOf->range_name;
5626 : 840 : fpoState->fp_rangeType = forPortionOf->rangeType;
5627 : 840 : fpoState->fp_rangeAttno = forPortionOf->rangeVar->varattno;
5628 : 840 : fpoState->fp_targetRange = targetRange;
5629 : :
5630 : : /* Initialize slot for the existing tuple */
5631 : :
5632 : 840 : fpoState->fp_Existing =
5633 : 840 : table_slot_create(rootRelInfo->ri_RelationDesc,
5634 : 840 : &mtstate->ps.state->es_tupleTable);
5635 : :
5636 : : /* Create the tuple slot for INSERTing the temporal leftovers */
5637 : :
5638 : 840 : fpoState->fp_Leftover =
5639 : 840 : ExecInitExtraTupleSlot(mtstate->ps.state, tupDesc, &TTSOpsVirtual);
5640 : :
5641 : 840 : rootRelInfo->ri_forPortionOf = fpoState;
5642 : :
5643 : : /*
5644 : : * Make sure the root relation has the FOR PORTION OF clause too. Each
5645 : : * partition needs its own TupleTableSlot, since they can have
5646 : : * different descriptors, so they'll use the root fpoState to
5647 : : * initialize one if necessary.
5648 : : */
5649 [ + + ]: 840 : if (node->rootRelation > 0)
5650 : 74 : mtstate->rootResultRelInfo->ri_forPortionOf = fpoState;
5651 : :
5652 [ + + ]: 840 : if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
5653 [ + - ]: 58 : mtstate->mt_partition_tuple_routing == NULL)
5654 : : {
5655 : : /*
5656 : : * We will need tuple routing to insert temporal leftovers. Since
5657 : : * we are initializing things before ExecCrossPartitionUpdate
5658 : : * runs, we must do everything it needs as well.
5659 : : */
5660 : 58 : Relation rootRel = mtstate->rootResultRelInfo->ri_RelationDesc;
5661 : : MemoryContext oldcxt;
5662 : :
5663 : : /* Things built here have to last for the query duration. */
5664 : 58 : oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
5665 : :
5666 : 58 : mtstate->mt_partition_tuple_routing =
5667 : 58 : ExecSetupPartitionTupleRouting(estate, rootRel);
5668 : :
5669 : : /*
5670 : : * Before a partition's tuple can be re-routed, it must first be
5671 : : * converted to the root's format, so we'll need a slot for
5672 : : * storing such tuples.
5673 : : */
5674 [ - + ]: 58 : Assert(mtstate->mt_root_tuple_slot == NULL);
5675 : 58 : mtstate->mt_root_tuple_slot = table_slot_create(rootRel, NULL);
5676 : :
5677 : 58 : MemoryContextSwitchTo(oldcxt);
5678 : : }
5679 : :
5680 : : /*
5681 : : * Don't free the ExprContext here because the result must last for
5682 : : * the whole query.
5683 : : */
5684 : : }
5685 : :
5686 : : /*
5687 : : * If we have any secondary relations in an UPDATE or DELETE, they need to
5688 : : * be treated like non-locked relations in SELECT FOR UPDATE, i.e., the
5689 : : * EvalPlanQual mechanism needs to be told about them. This also goes for
5690 : : * the source relations in a MERGE. Locate the relevant ExecRowMarks.
5691 : : */
1917 tgl@sss.pgh.pa.us 5692 :CBC 74027 : arowmarks = NIL;
6091 5693 [ + + + + : 75914 : foreach(l, node->rowMarks)
+ + ]
5694 : : {
3368 5695 : 1887 : PlanRowMark *rc = lfirst_node(PlanRowMark, l);
165 amitlan@postgresql.o 5696 : 1887 : RangeTblEntry *rte = exec_rt_fetch(rc->rti, estate);
5697 : : ExecRowMark *erm;
5698 : : ExecAuxRowMark *aerm;
5699 : :
5700 : : /* ignore "parent" rowmarks; they are irrelevant at runtime */
5701 [ + + ]: 1887 : if (rc->isParent)
5702 : 94 : continue;
5703 : :
5704 : : /*
5705 : : * Also ignore rowmarks belonging to child tables that have been
5706 : : * pruned in ExecDoInitialPruning().
5707 : : */
5708 [ + + ]: 1793 : if (rte->rtekind == RTE_RELATION &&
508 5709 [ - + ]: 1418 : !bms_is_member(rc->rti, estate->es_unpruned_relids))
6091 tgl@sss.pgh.pa.us 5710 :UBC 0 : continue;
5711 : :
5712 : : /* Find ExecRowMark and build ExecAuxRowMark */
4067 tgl@sss.pgh.pa.us 5713 :CBC 1793 : erm = ExecFindRowMark(estate, rc->rti, false);
1917 5714 : 1793 : aerm = ExecBuildAuxRowMark(erm, subplan->targetlist);
5715 : 1793 : arowmarks = lappend(arowmarks, aerm);
5716 : : }
5717 : :
5718 : : /* For a MERGE command, initialize its state */
1555 alvherre@alvh.no-ip. 5719 [ + + ]: 74027 : if (mtstate->operation == CMD_MERGE)
5720 : 1056 : ExecInitMerge(mtstate, estate);
5721 : :
1917 tgl@sss.pgh.pa.us 5722 : 74027 : EvalPlanQualSetPlan(&mtstate->mt_epqstate, subplan, arowmarks);
5723 : :
5724 : : /*
5725 : : * If there are a lot of result relations, use a hash table to speed the
5726 : : * lookups. If there are not a lot, a simple linear search is faster.
5727 : : *
5728 : : * It's not clear where the threshold is, but try 64 for starters. In a
5729 : : * debugging build, use a small threshold so that we get some test
5730 : : * coverage of both code paths.
5731 : : */
5732 : : #ifdef USE_ASSERT_CHECKING
5733 : : #define MT_NRELS_HASH 4
5734 : : #else
5735 : : #define MT_NRELS_HASH 64
5736 : : #endif
5737 [ + + ]: 74027 : if (nrels >= MT_NRELS_HASH)
5738 : : {
5739 : : HASHCTL hash_ctl;
5740 : :
5741 : 219 : hash_ctl.keysize = sizeof(Oid);
5742 : 219 : hash_ctl.entrysize = sizeof(MTTargetRelLookup);
5743 : 219 : hash_ctl.hcxt = CurrentMemoryContext;
5744 : 219 : mtstate->mt_resultOidHash =
5745 : 219 : hash_create("ModifyTable target hash",
5746 : : nrels, &hash_ctl,
5747 : : HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
5748 [ + + ]: 1227 : for (i = 0; i < nrels; i++)
5749 : : {
5750 : : Oid hashkey;
5751 : : MTTargetRelLookup *mtlookup;
5752 : : bool found;
5753 : :
5754 : 1008 : resultRelInfo = &mtstate->resultRelInfo[i];
5755 : 1008 : hashkey = RelationGetRelid(resultRelInfo->ri_RelationDesc);
5756 : : mtlookup = (MTTargetRelLookup *)
5757 : 1008 : hash_search(mtstate->mt_resultOidHash, &hashkey,
5758 : : HASH_ENTER, &found);
5759 [ - + ]: 1008 : Assert(!found);
5760 : 1008 : mtlookup->relationIndex = i;
5761 : : }
5762 : : }
5763 : : else
5764 : 73808 : mtstate->mt_resultOidHash = NULL;
5765 : :
5766 : : /*
5767 : : * Determine if the FDW supports batch insert and determine the batch size
5768 : : * (a FDW may support batching, but it may be disabled for the
5769 : : * server/table).
5770 : : *
5771 : : * We only do this for INSERT, so that for UPDATE/DELETE the batch size
5772 : : * remains set to 0.
5773 : : */
1986 tomas.vondra@postgre 5774 [ + + ]: 74027 : if (operation == CMD_INSERT)
5775 : : {
5776 : : /* insert may only have one relation, inheritance is not expanded */
468 amitlan@postgresql.o 5777 [ - + ]: 55242 : Assert(total_nrels == 1);
1986 tomas.vondra@postgre 5778 : 55242 : resultRelInfo = mtstate->resultRelInfo;
1911 tgl@sss.pgh.pa.us 5779 [ + - ]: 55242 : if (!resultRelInfo->ri_usesFdwDirectModify &&
5780 [ + + ]: 55242 : resultRelInfo->ri_FdwRoutine != NULL &&
5781 [ + - ]: 88 : resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize &&
5782 [ + - ]: 88 : resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert)
5783 : : {
5784 : 88 : resultRelInfo->ri_BatchSize =
5785 : 88 : resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize(resultRelInfo);
1986 tomas.vondra@postgre 5786 [ - + ]: 88 : Assert(resultRelInfo->ri_BatchSize >= 1);
5787 : : }
5788 : : else
1911 tgl@sss.pgh.pa.us 5789 : 55154 : resultRelInfo->ri_BatchSize = 1;
5790 : : }
5791 : :
5792 : : /*
5793 : : * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it
5794 : : * to estate->es_auxmodifytables so that it will be run to completion by
5795 : : * ExecPostprocessPlan. (It'd actually work fine to add the primary
5796 : : * ModifyTable node too, but there's no need.) Note the use of lcons not
5797 : : * lappend: we need later-initialized ModifyTable nodes to be shut down
5798 : : * before earlier ones. This ensures that we don't throw away RETURNING
5799 : : * rows that need to be seen by a later CTE subplan.
5800 : : */
5604 5801 [ + + ]: 74027 : if (!mtstate->canSetTag)
5802 : 699 : estate->es_auxmodifytables = lcons(mtstate,
5803 : : estate->es_auxmodifytables);
5804 : :
6107 5805 : 74027 : return mtstate;
5806 : : }
5807 : :
5808 : : /* ----------------------------------------------------------------
5809 : : * ExecEndModifyTable
5810 : : *
5811 : : * Shuts down the plan.
5812 : : *
5813 : : * Returns nothing of interest.
5814 : : * ----------------------------------------------------------------
5815 : : */
5816 : : void
5817 : 70938 : ExecEndModifyTable(ModifyTableState *node)
5818 : : {
5819 : : int i;
5820 : :
5821 : : /*
5822 : : * Allow any FDWs to shut down
5823 : : */
1917 5824 [ + + ]: 143272 : for (i = 0; i < node->mt_nrels; i++)
5825 : : {
5826 : : int j;
4860 5827 : 72334 : ResultRelInfo *resultRelInfo = node->resultRelInfo + i;
5828 : :
3756 rhaas@postgresql.org 5829 [ + + ]: 72334 : if (!resultRelInfo->ri_usesFdwDirectModify &&
5830 [ + + ]: 72236 : resultRelInfo->ri_FdwRoutine != NULL &&
4860 tgl@sss.pgh.pa.us 5831 [ + - ]: 158 : resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
5832 : 158 : resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
5833 : : resultRelInfo);
5834 : :
5835 : : /*
5836 : : * Cleanup the initialized batch slots. This only matters for FDWs
5837 : : * with batching, but the other cases will have ri_NumSlotsInitialized
5838 : : * == 0.
5839 : : */
1845 tomas.vondra@postgre 5840 [ + + ]: 72362 : for (j = 0; j < resultRelInfo->ri_NumSlotsInitialized; j++)
5841 : : {
5842 : 28 : ExecDropSingleTupleTableSlot(resultRelInfo->ri_Slots[j]);
5843 : 28 : ExecDropSingleTupleTableSlot(resultRelInfo->ri_PlanSlots[j]);
5844 : : }
5845 : : }
5846 : :
5847 : : /*
5848 : : * Close all the partitioned tables, leaf partitions, and their indices
5849 : : * and release the slot used for tuple routing, if set.
5850 : : */
3099 rhaas@postgresql.org 5851 [ + + ]: 70938 : if (node->mt_partition_tuple_routing)
5852 : : {
3007 5853 : 3836 : ExecCleanupTupleRouting(node, node->mt_partition_tuple_routing);
5854 : :
2783 alvherre@alvh.no-ip. 5855 [ + + ]: 3836 : if (node->mt_root_tuple_slot)
5856 : 483 : ExecDropSingleTupleTableSlot(node->mt_root_tuple_slot);
5857 : : }
5858 : :
5859 : : /*
5860 : : * Terminate EPQ execution if active
5861 : : */
6091 tgl@sss.pgh.pa.us 5862 : 70938 : EvalPlanQualEnd(&node->mt_epqstate);
5863 : :
5864 : : /*
5865 : : * shut down subplan
5866 : : */
1917 5867 : 70938 : ExecEndNode(outerPlanState(node));
6107 5868 : 70938 : }
5869 : :
5870 : : void
5832 tgl@sss.pgh.pa.us 5871 :UBC 0 : ExecReScanModifyTable(ModifyTableState *node)
5872 : : {
5873 : : /*
5874 : : * Currently, we don't need to support rescan on ModifyTable nodes. The
5875 : : * semantics of that would be a bit debatable anyway.
5876 : : */
6107 5877 [ # # ]: 0 : elog(ERROR, "ExecReScanModifyTable is not implemented");
5878 : : }
5879 : :
5880 : : /* ----------------------------------------------------------------
5881 : : * ExecInitForPortionOf
5882 : : *
5883 : : * Initializes resultRelInfo->ri_forPortionOf for child tables.
5884 : : *
5885 : : * Partitions share the root leftover slot, since they must insert via
5886 : : * the root relation to get tuple routing. Plain inheritance children
5887 : : * must keep their own leftover slot and insert back into the child, or
5888 : : * else child-only column values and physical placement would be lost.
5889 : : * ----------------------------------------------------------------
5890 : : */
5891 : : static void
22 peter@eisentraut.org 5892 :GNC 74 : ExecInitForPortionOf(ModifyTableState *mtstate, EState *estate,
5893 : : ResultRelInfo *resultRelInfo)
5894 : : {
5895 : : MemoryContext oldcxt;
5896 : : ForPortionOfState *leafState;
5897 : 74 : ResultRelInfo *rootRelInfo = mtstate->rootResultRelInfo;
5898 : : ForPortionOfState *fpoState;
5899 : : TupleConversionMap *map;
5900 : :
5901 [ - + ]: 74 : if (!rootRelInfo)
22 peter@eisentraut.org 5902 [ # # ]:UNC 0 : elog(ERROR, "no root relation but ri_forPortionOf is uninitialized");
5903 : :
22 peter@eisentraut.org 5904 :GNC 74 : fpoState = rootRelInfo->ri_forPortionOf;
5905 [ - + ]: 74 : Assert(fpoState);
5906 : :
5907 : : /* Things built here have to last for the query duration. */
5908 : 74 : oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
5909 : :
5910 : 74 : leafState = makeNode(ForPortionOfState);
5911 : :
5912 : 74 : leafState->fp_rangeName = fpoState->fp_rangeName;
5913 : 74 : leafState->fp_rangeType = fpoState->fp_rangeType;
5914 : 74 : leafState->fp_targetRange = fpoState->fp_targetRange;
5915 : 74 : map = ExecGetChildToRootMap(resultRelInfo);
5916 : :
5917 : : /*
5918 : : * fp_rangeAttno must match the tuple layout used for reading the old
5919 : : * range value. The query uses the target relation's attno, so translate
5920 : : * it to the child attno when the child has a different column layout.
5921 : : */
5922 [ + + ]: 74 : if (map)
5923 : 32 : leafState->fp_rangeAttno = map->attrMap->attnums[fpoState->fp_rangeAttno - 1];
5924 : : else
5925 : 42 : leafState->fp_rangeAttno = fpoState->fp_rangeAttno;
5926 : :
5927 : : /*
5928 : : * For partitioned tables we must read the leftovers using the child
5929 : : * table's tuple descriptor, but then insert them into the root table
5930 : : * (using its tuple descriptor) so we get tuple routing.
5931 : : *
5932 : : * For traditional table inheritance, we read and insert directly into
5933 : : * this resultRelInfo; no tuple routing via the parent is required.
5934 : : */
5935 [ + + ]: 74 : if (rootRelInfo->ri_RelationDesc->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
5936 : 58 : leafState->fp_Leftover = fpoState->fp_Leftover;
5937 : : else
5938 : 16 : leafState->fp_Leftover =
5939 : 16 : ExecInitExtraTupleSlot(mtstate->ps.state,
5940 : 16 : RelationGetDescr(resultRelInfo->ri_RelationDesc),
5941 : : &TTSOpsVirtual);
5942 : :
5943 : : /* Each child relation needs a slot matching its tuple descriptor. */
5944 : 74 : leafState->fp_Existing =
5945 : 74 : table_slot_create(resultRelInfo->ri_RelationDesc,
5946 : 74 : &mtstate->ps.state->es_tupleTable);
5947 : :
5948 : 74 : resultRelInfo->ri_forPortionOf = leafState;
5949 : :
5950 : 74 : MemoryContextSwitchTo(oldcxt);
5951 : 74 : }
|