PostgreSQL Source Code  git master
nodeModifyTable.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeModifyTable.c
4  * routines to handle ModifyTable nodes.
5  *
6  * Portions Copyright (c) 1996-2024, 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 /* INTERFACE ROUTINES
16  * ExecInitModifyTable - initialize the ModifyTable node
17  * ExecModifyTable - retrieve the next tuple from the node
18  * ExecEndModifyTable - shut down the ModifyTable node
19  * ExecReScanModifyTable - rescan the ModifyTable node
20  *
21  * NOTES
22  * The ModifyTable node receives input from its outerPlan, which is
23  * the data to insert for INSERT cases, the changed columns' new
24  * values plus row-locating info for UPDATE and MERGE cases, or just the
25  * row-locating info for DELETE cases.
26  *
27  * The relation to modify can be an ordinary table, a foreign table, or a
28  * view. If it's a view, either it has sufficient INSTEAD OF triggers or
29  * this node executes only MERGE ... DO NOTHING. If the original MERGE
30  * targeted a view not in one of those two categories, earlier processing
31  * already pointed the ModifyTable result relation to an underlying
32  * relation of that other view. This node does process
33  * ri_WithCheckOptions, which may have expressions from those other,
34  * automatically updatable views.
35  *
36  * MERGE runs a join between the source relation and the target table.
37  * If any WHEN NOT MATCHED [BY TARGET] clauses are present, then the join
38  * is an outer join that might output tuples without a matching target
39  * tuple. In this case, any unmatched target tuples will have NULL
40  * row-locating info, and only INSERT can be run. But for matched target
41  * tuples, the row-locating info is used to determine the tuple to UPDATE
42  * or DELETE. When all clauses are WHEN MATCHED or WHEN NOT MATCHED BY
43  * SOURCE, all tuples produced by the join will include a matching target
44  * tuple, so all tuples contain row-locating info.
45  *
46  * If the query specifies RETURNING, then the ModifyTable returns a
47  * RETURNING tuple after completing each row insert, update, or delete.
48  * It must be called again to continue the operation. Without RETURNING,
49  * we just loop within the node until all the work is done, then
50  * return NULL. This avoids useless call/return overhead.
51  */
52 
53 #include "postgres.h"
54 
55 #include "access/htup_details.h"
56 #include "access/tableam.h"
57 #include "access/xact.h"
58 #include "commands/trigger.h"
59 #include "executor/execPartition.h"
60 #include "executor/executor.h"
62 #include "foreign/fdwapi.h"
63 #include "miscadmin.h"
64 #include "nodes/nodeFuncs.h"
65 #include "optimizer/optimizer.h"
66 #include "rewrite/rewriteHandler.h"
67 #include "storage/lmgr.h"
68 #include "utils/builtins.h"
69 #include "utils/datum.h"
70 #include "utils/rel.h"
71 #include "utils/snapmgr.h"
72 
73 
74 typedef struct MTTargetRelLookup
75 {
76  Oid relationOid; /* hash key, must be first */
77  int relationIndex; /* rel's index in resultRelInfo[] array */
79 
80 /*
81  * Context struct for a ModifyTable operation, containing basic execution
82  * state and some output variables populated by ExecUpdateAct() and
83  * ExecDeleteAct() to report the result of their actions to callers.
84  */
85 typedef struct ModifyTableContext
86 {
87  /* Operation state */
91 
92  /*
93  * Slot containing tuple obtained from ModifyTable's subplan. Used to
94  * access "junk" columns that are not going to be stored.
95  */
97 
98  /*
99  * Information about the changes that were made concurrently to a tuple
100  * being updated or deleted
101  */
103 
104  /*
105  * The tuple projected by the INSERT's RETURNING clause, when doing a
106  * cross-partition UPDATE
107  */
110 
111 /*
112  * Context struct containing output data specific to UPDATE operations.
113  */
114 typedef struct UpdateContext
115 {
116  bool crossPartUpdate; /* was it a cross-partition update? */
117  TU_UpdateIndexes updateIndexes; /* Which index updates are required? */
118 
119  /*
120  * Lock mode to acquire on the latest tuple version before performing
121  * EvalPlanQual on it
122  */
125 
126 
127 static void ExecBatchInsert(ModifyTableState *mtstate,
128  ResultRelInfo *resultRelInfo,
129  TupleTableSlot **slots,
130  TupleTableSlot **planSlots,
131  int numSlots,
132  EState *estate,
133  bool canSetTag);
134 static void ExecPendingInserts(EState *estate);
136  ResultRelInfo *sourcePartInfo,
137  ResultRelInfo *destPartInfo,
138  ItemPointer tupleid,
139  TupleTableSlot *oldslot,
140  TupleTableSlot *newslot);
142  ResultRelInfo *resultRelInfo,
143  ItemPointer conflictTid,
144  TupleTableSlot *excludedSlot,
145  bool canSetTag,
146  TupleTableSlot **returning);
148  EState *estate,
149  PartitionTupleRouting *proute,
150  ResultRelInfo *targetRelInfo,
151  TupleTableSlot *slot,
152  ResultRelInfo **partRelInfo);
153 
155  ResultRelInfo *resultRelInfo,
156  ItemPointer tupleid,
157  HeapTuple oldtuple,
158  bool canSetTag);
159 static void ExecInitMerge(ModifyTableState *mtstate, EState *estate);
161  ResultRelInfo *resultRelInfo,
162  ItemPointer tupleid,
163  HeapTuple oldtuple,
164  bool canSetTag,
165  bool *matched);
167  ResultRelInfo *resultRelInfo,
168  bool canSetTag);
169 
170 
171 /*
172  * Verify that the tuples to be produced by INSERT match the
173  * target relation's rowtype
174  *
175  * We do this to guard against stale plans. If plan invalidation is
176  * functioning properly then we should never get a failure here, but better
177  * safe than sorry. Note that this is called after we have obtained lock
178  * on the target rel, so the rowtype can't change underneath us.
179  *
180  * The plan output is represented by its targetlist, because that makes
181  * handling the dropped-column case easier.
182  *
183  * We used to use this for UPDATE as well, but now the equivalent checks
184  * are done in ExecBuildUpdateProjection.
185  */
186 static void
187 ExecCheckPlanOutput(Relation resultRel, List *targetList)
188 {
189  TupleDesc resultDesc = RelationGetDescr(resultRel);
190  int attno = 0;
191  ListCell *lc;
192 
193  foreach(lc, targetList)
194  {
195  TargetEntry *tle = (TargetEntry *) lfirst(lc);
196  Form_pg_attribute attr;
197 
198  Assert(!tle->resjunk); /* caller removed junk items already */
199 
200  if (attno >= resultDesc->natts)
201  ereport(ERROR,
202  (errcode(ERRCODE_DATATYPE_MISMATCH),
203  errmsg("table row type and query-specified row type do not match"),
204  errdetail("Query has too many columns.")));
205  attr = TupleDescAttr(resultDesc, attno);
206  attno++;
207 
208  if (!attr->attisdropped)
209  {
210  /* Normal case: demand type match */
211  if (exprType((Node *) tle->expr) != attr->atttypid)
212  ereport(ERROR,
213  (errcode(ERRCODE_DATATYPE_MISMATCH),
214  errmsg("table row type and query-specified row type do not match"),
215  errdetail("Table has type %s at ordinal position %d, but query expects %s.",
216  format_type_be(attr->atttypid),
217  attno,
218  format_type_be(exprType((Node *) tle->expr)))));
219  }
220  else
221  {
222  /*
223  * For a dropped column, we can't check atttypid (it's likely 0).
224  * In any case the planner has most likely inserted an INT4 null.
225  * What we insist on is just *some* NULL constant.
226  */
227  if (!IsA(tle->expr, Const) ||
228  !((Const *) tle->expr)->constisnull)
229  ereport(ERROR,
230  (errcode(ERRCODE_DATATYPE_MISMATCH),
231  errmsg("table row type and query-specified row type do not match"),
232  errdetail("Query provides a value for a dropped column at ordinal position %d.",
233  attno)));
234  }
235  }
236  if (attno != resultDesc->natts)
237  ereport(ERROR,
238  (errcode(ERRCODE_DATATYPE_MISMATCH),
239  errmsg("table row type and query-specified row type do not match"),
240  errdetail("Query has too few columns.")));
241 }
242 
243 /*
244  * ExecProcessReturning --- evaluate a RETURNING list
245  *
246  * resultRelInfo: current result rel
247  * tupleSlot: slot holding tuple actually inserted/updated/deleted
248  * planSlot: slot holding tuple returned by top subplan node
249  *
250  * Note: If tupleSlot is NULL, the FDW should have already provided econtext's
251  * scan tuple.
252  *
253  * Returns a slot holding the result tuple
254  */
255 static TupleTableSlot *
257  TupleTableSlot *tupleSlot,
258  TupleTableSlot *planSlot)
259 {
260  ProjectionInfo *projectReturning = resultRelInfo->ri_projectReturning;
261  ExprContext *econtext = projectReturning->pi_exprContext;
262 
263  /* Make tuple and any needed join variables available to ExecProject */
264  if (tupleSlot)
265  econtext->ecxt_scantuple = tupleSlot;
266  econtext->ecxt_outertuple = planSlot;
267 
268  /*
269  * RETURNING expressions might reference the tableoid column, so
270  * reinitialize tts_tableOid before evaluating them.
271  */
272  econtext->ecxt_scantuple->tts_tableOid =
273  RelationGetRelid(resultRelInfo->ri_RelationDesc);
274 
275  /* Compute the RETURNING expressions */
276  return ExecProject(projectReturning);
277 }
278 
279 /*
280  * ExecCheckTupleVisible -- verify tuple is visible
281  *
282  * It would not be consistent with guarantees of the higher isolation levels to
283  * proceed with avoiding insertion (taking speculative insertion's alternative
284  * path) on the basis of another tuple that is not visible to MVCC snapshot.
285  * Check for the need to raise a serialization failure, and do so as necessary.
286  */
287 static void
289  Relation rel,
290  TupleTableSlot *slot)
291 {
293  return;
294 
295  if (!table_tuple_satisfies_snapshot(rel, slot, estate->es_snapshot))
296  {
297  Datum xminDatum;
298  TransactionId xmin;
299  bool isnull;
300 
301  xminDatum = slot_getsysattr(slot, MinTransactionIdAttributeNumber, &isnull);
302  Assert(!isnull);
303  xmin = DatumGetTransactionId(xminDatum);
304 
305  /*
306  * We should not raise a serialization failure if the conflict is
307  * against a tuple inserted by our own transaction, even if it's not
308  * visible to our snapshot. (This would happen, for example, if
309  * conflicting keys are proposed for insertion in a single command.)
310  */
312  ereport(ERROR,
314  errmsg("could not serialize access due to concurrent update")));
315  }
316 }
317 
318 /*
319  * ExecCheckTIDVisible -- convenience variant of ExecCheckTupleVisible()
320  */
321 static void
323  ResultRelInfo *relinfo,
324  ItemPointer tid,
325  TupleTableSlot *tempSlot)
326 {
327  Relation rel = relinfo->ri_RelationDesc;
328 
329  /* Redundantly check isolation level */
331  return;
332 
333  if (!table_tuple_fetch_row_version(rel, tid, SnapshotAny, tempSlot))
334  elog(ERROR, "failed to fetch conflicting tuple for ON CONFLICT");
335  ExecCheckTupleVisible(estate, rel, tempSlot);
336  ExecClearTuple(tempSlot);
337 }
338 
339 /*
340  * Initialize to compute stored generated columns for a tuple
341  *
342  * This fills the resultRelInfo's ri_GeneratedExprsI/ri_NumGeneratedNeededI
343  * or ri_GeneratedExprsU/ri_NumGeneratedNeededU fields, depending on cmdtype.
344  * If cmdType == CMD_UPDATE, the ri_extraUpdatedCols field is filled too.
345  *
346  * Note: usually, a given query would need only one of ri_GeneratedExprsI and
347  * ri_GeneratedExprsU per result rel; but MERGE can need both, and so can
348  * cross-partition UPDATEs, since a partition might be the target of both
349  * UPDATE and INSERT actions.
350  */
351 void
353  EState *estate,
354  CmdType cmdtype)
355 {
356  Relation rel = resultRelInfo->ri_RelationDesc;
357  TupleDesc tupdesc = RelationGetDescr(rel);
358  int natts = tupdesc->natts;
359  ExprState **ri_GeneratedExprs;
360  int ri_NumGeneratedNeeded;
361  Bitmapset *updatedCols;
362  MemoryContext oldContext;
363 
364  /* Nothing to do if no generated columns */
365  if (!(tupdesc->constr && tupdesc->constr->has_generated_stored))
366  return;
367 
368  /*
369  * In an UPDATE, we can skip computing any generated columns that do not
370  * depend on any UPDATE target column. But if there is a BEFORE ROW
371  * UPDATE trigger, we cannot skip because the trigger might change more
372  * columns.
373  */
374  if (cmdtype == CMD_UPDATE &&
375  !(rel->trigdesc && rel->trigdesc->trig_update_before_row))
376  updatedCols = ExecGetUpdatedCols(resultRelInfo, estate);
377  else
378  updatedCols = NULL;
379 
380  /*
381  * Make sure these data structures are built in the per-query memory
382  * context so they'll survive throughout the query.
383  */
384  oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
385 
386  ri_GeneratedExprs = (ExprState **) palloc0(natts * sizeof(ExprState *));
387  ri_NumGeneratedNeeded = 0;
388 
389  for (int i = 0; i < natts; i++)
390  {
391  if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
392  {
393  Expr *expr;
394 
395  /* Fetch the GENERATED AS expression tree */
396  expr = (Expr *) build_column_default(rel, i + 1);
397  if (expr == NULL)
398  elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
399  i + 1, RelationGetRelationName(rel));
400 
401  /*
402  * If it's an update with a known set of update target columns,
403  * see if we can skip the computation.
404  */
405  if (updatedCols)
406  {
407  Bitmapset *attrs_used = NULL;
408 
409  pull_varattnos((Node *) expr, 1, &attrs_used);
410 
411  if (!bms_overlap(updatedCols, attrs_used))
412  continue; /* need not update this column */
413  }
414 
415  /* No luck, so prepare the expression for execution */
416  ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
417  ri_NumGeneratedNeeded++;
418 
419  /* If UPDATE, mark column in resultRelInfo->ri_extraUpdatedCols */
420  if (cmdtype == CMD_UPDATE)
421  resultRelInfo->ri_extraUpdatedCols =
422  bms_add_member(resultRelInfo->ri_extraUpdatedCols,
424  }
425  }
426 
427  /* Save in appropriate set of fields */
428  if (cmdtype == CMD_UPDATE)
429  {
430  /* Don't call twice */
431  Assert(resultRelInfo->ri_GeneratedExprsU == NULL);
432 
433  resultRelInfo->ri_GeneratedExprsU = ri_GeneratedExprs;
434  resultRelInfo->ri_NumGeneratedNeededU = ri_NumGeneratedNeeded;
435  }
436  else
437  {
438  /* Don't call twice */
439  Assert(resultRelInfo->ri_GeneratedExprsI == NULL);
440 
441  resultRelInfo->ri_GeneratedExprsI = ri_GeneratedExprs;
442  resultRelInfo->ri_NumGeneratedNeededI = ri_NumGeneratedNeeded;
443  }
444 
445  MemoryContextSwitchTo(oldContext);
446 }
447 
448 /*
449  * Compute stored generated columns for a tuple
450  */
451 void
453  EState *estate, TupleTableSlot *slot,
454  CmdType cmdtype)
455 {
456  Relation rel = resultRelInfo->ri_RelationDesc;
457  TupleDesc tupdesc = RelationGetDescr(rel);
458  int natts = tupdesc->natts;
459  ExprContext *econtext = GetPerTupleExprContext(estate);
460  ExprState **ri_GeneratedExprs;
461  MemoryContext oldContext;
462  Datum *values;
463  bool *nulls;
464 
465  /* We should not be called unless this is true */
466  Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
467 
468  /*
469  * Initialize the expressions if we didn't already, and check whether we
470  * can exit early because nothing needs to be computed.
471  */
472  if (cmdtype == CMD_UPDATE)
473  {
474  if (resultRelInfo->ri_GeneratedExprsU == NULL)
475  ExecInitStoredGenerated(resultRelInfo, estate, cmdtype);
476  if (resultRelInfo->ri_NumGeneratedNeededU == 0)
477  return;
478  ri_GeneratedExprs = resultRelInfo->ri_GeneratedExprsU;
479  }
480  else
481  {
482  if (resultRelInfo->ri_GeneratedExprsI == NULL)
483  ExecInitStoredGenerated(resultRelInfo, estate, cmdtype);
484  /* Early exit is impossible given the prior Assert */
485  Assert(resultRelInfo->ri_NumGeneratedNeededI > 0);
486  ri_GeneratedExprs = resultRelInfo->ri_GeneratedExprsI;
487  }
488 
489  oldContext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
490 
491  values = palloc(sizeof(*values) * natts);
492  nulls = palloc(sizeof(*nulls) * natts);
493 
494  slot_getallattrs(slot);
495  memcpy(nulls, slot->tts_isnull, sizeof(*nulls) * natts);
496 
497  for (int i = 0; i < natts; i++)
498  {
499  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
500 
501  if (ri_GeneratedExprs[i])
502  {
503  Datum val;
504  bool isnull;
505 
506  Assert(attr->attgenerated == ATTRIBUTE_GENERATED_STORED);
507 
508  econtext->ecxt_scantuple = slot;
509 
510  val = ExecEvalExpr(ri_GeneratedExprs[i], econtext, &isnull);
511 
512  /*
513  * We must make a copy of val as we have no guarantees about where
514  * memory for a pass-by-reference Datum is located.
515  */
516  if (!isnull)
517  val = datumCopy(val, attr->attbyval, attr->attlen);
518 
519  values[i] = val;
520  nulls[i] = isnull;
521  }
522  else
523  {
524  if (!nulls[i])
525  values[i] = datumCopy(slot->tts_values[i], attr->attbyval, attr->attlen);
526  }
527  }
528 
529  ExecClearTuple(slot);
530  memcpy(slot->tts_values, values, sizeof(*values) * natts);
531  memcpy(slot->tts_isnull, nulls, sizeof(*nulls) * natts);
532  ExecStoreVirtualTuple(slot);
533  ExecMaterializeSlot(slot);
534 
535  MemoryContextSwitchTo(oldContext);
536 }
537 
538 /*
539  * ExecInitInsertProjection
540  * Do one-time initialization of projection data for INSERT tuples.
541  *
542  * INSERT queries may need a projection to filter out junk attrs in the tlist.
543  *
544  * This is also a convenient place to verify that the
545  * output of an INSERT matches the target table.
546  */
547 static void
549  ResultRelInfo *resultRelInfo)
550 {
551  ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
552  Plan *subplan = outerPlan(node);
553  EState *estate = mtstate->ps.state;
554  List *insertTargetList = NIL;
555  bool need_projection = false;
556  ListCell *l;
557 
558  /* Extract non-junk columns of the subplan's result tlist. */
559  foreach(l, subplan->targetlist)
560  {
561  TargetEntry *tle = (TargetEntry *) lfirst(l);
562 
563  if (!tle->resjunk)
564  insertTargetList = lappend(insertTargetList, tle);
565  else
566  need_projection = true;
567  }
568 
569  /*
570  * The junk-free list must produce a tuple suitable for the result
571  * relation.
572  */
573  ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc, insertTargetList);
574 
575  /* We'll need a slot matching the table's format. */
576  resultRelInfo->ri_newTupleSlot =
577  table_slot_create(resultRelInfo->ri_RelationDesc,
578  &estate->es_tupleTable);
579 
580  /* Build ProjectionInfo if needed (it probably isn't). */
581  if (need_projection)
582  {
583  TupleDesc relDesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
584 
585  /* need an expression context to do the projection */
586  if (mtstate->ps.ps_ExprContext == NULL)
587  ExecAssignExprContext(estate, &mtstate->ps);
588 
589  resultRelInfo->ri_projectNew =
590  ExecBuildProjectionInfo(insertTargetList,
591  mtstate->ps.ps_ExprContext,
592  resultRelInfo->ri_newTupleSlot,
593  &mtstate->ps,
594  relDesc);
595  }
596 
597  resultRelInfo->ri_projectNewInfoValid = true;
598 }
599 
600 /*
601  * ExecInitUpdateProjection
602  * Do one-time initialization of projection data for UPDATE tuples.
603  *
604  * UPDATE always needs a projection, because (1) there's always some junk
605  * attrs, and (2) we may need to merge values of not-updated columns from
606  * the old tuple into the final tuple. In UPDATE, the tuple arriving from
607  * the subplan contains only new values for the changed columns, plus row
608  * identity info in the junk attrs.
609  *
610  * This is "one-time" for any given result rel, but we might touch more than
611  * one result rel in the course of an inherited UPDATE, and each one needs
612  * its own projection due to possible column order variation.
613  *
614  * This is also a convenient place to verify that the output of an UPDATE
615  * matches the target table (ExecBuildUpdateProjection does that).
616  */
617 static void
619  ResultRelInfo *resultRelInfo)
620 {
621  ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
622  Plan *subplan = outerPlan(node);
623  EState *estate = mtstate->ps.state;
624  TupleDesc relDesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
625  int whichrel;
626  List *updateColnos;
627 
628  /*
629  * Usually, mt_lastResultIndex matches the target rel. If it happens not
630  * to, we can get the index the hard way with an integer division.
631  */
632  whichrel = mtstate->mt_lastResultIndex;
633  if (resultRelInfo != mtstate->resultRelInfo + whichrel)
634  {
635  whichrel = resultRelInfo - mtstate->resultRelInfo;
636  Assert(whichrel >= 0 && whichrel < mtstate->mt_nrels);
637  }
638 
639  updateColnos = (List *) list_nth(node->updateColnosLists, whichrel);
640 
641  /*
642  * For UPDATE, we use the old tuple to fill up missing values in the tuple
643  * produced by the subplan to get the new tuple. We need two slots, both
644  * matching the table's desired format.
645  */
646  resultRelInfo->ri_oldTupleSlot =
647  table_slot_create(resultRelInfo->ri_RelationDesc,
648  &estate->es_tupleTable);
649  resultRelInfo->ri_newTupleSlot =
650  table_slot_create(resultRelInfo->ri_RelationDesc,
651  &estate->es_tupleTable);
652 
653  /* need an expression context to do the projection */
654  if (mtstate->ps.ps_ExprContext == NULL)
655  ExecAssignExprContext(estate, &mtstate->ps);
656 
657  resultRelInfo->ri_projectNew =
658  ExecBuildUpdateProjection(subplan->targetlist,
659  false, /* subplan did the evaluation */
660  updateColnos,
661  relDesc,
662  mtstate->ps.ps_ExprContext,
663  resultRelInfo->ri_newTupleSlot,
664  &mtstate->ps);
665 
666  resultRelInfo->ri_projectNewInfoValid = true;
667 }
668 
669 /*
670  * ExecGetInsertNewTuple
671  * This prepares a "new" tuple ready to be inserted into given result
672  * relation, by removing any junk columns of the plan's output tuple
673  * and (if necessary) coercing the tuple to the right tuple format.
674  */
675 static TupleTableSlot *
677  TupleTableSlot *planSlot)
678 {
679  ProjectionInfo *newProj = relinfo->ri_projectNew;
680  ExprContext *econtext;
681 
682  /*
683  * If there's no projection to be done, just make sure the slot is of the
684  * right type for the target rel. If the planSlot is the right type we
685  * can use it as-is, else copy the data into ri_newTupleSlot.
686  */
687  if (newProj == NULL)
688  {
689  if (relinfo->ri_newTupleSlot->tts_ops != planSlot->tts_ops)
690  {
691  ExecCopySlot(relinfo->ri_newTupleSlot, planSlot);
692  return relinfo->ri_newTupleSlot;
693  }
694  else
695  return planSlot;
696  }
697 
698  /*
699  * Else project; since the projection output slot is ri_newTupleSlot, this
700  * will also fix any slot-type problem.
701  *
702  * Note: currently, this is dead code, because INSERT cases don't receive
703  * any junk columns so there's never a projection to be done.
704  */
705  econtext = newProj->pi_exprContext;
706  econtext->ecxt_outertuple = planSlot;
707  return ExecProject(newProj);
708 }
709 
710 /*
711  * ExecGetUpdateNewTuple
712  * This prepares a "new" tuple by combining an UPDATE subplan's output
713  * tuple (which contains values of changed columns) with unchanged
714  * columns taken from the old tuple.
715  *
716  * The subplan tuple might also contain junk columns, which are ignored.
717  * Note that the projection also ensures we have a slot of the right type.
718  */
721  TupleTableSlot *planSlot,
722  TupleTableSlot *oldSlot)
723 {
724  ProjectionInfo *newProj = relinfo->ri_projectNew;
725  ExprContext *econtext;
726 
727  /* Use a few extra Asserts to protect against outside callers */
728  Assert(relinfo->ri_projectNewInfoValid);
729  Assert(planSlot != NULL && !TTS_EMPTY(planSlot));
730  Assert(oldSlot != NULL && !TTS_EMPTY(oldSlot));
731 
732  econtext = newProj->pi_exprContext;
733  econtext->ecxt_outertuple = planSlot;
734  econtext->ecxt_scantuple = oldSlot;
735  return ExecProject(newProj);
736 }
737 
738 /* ----------------------------------------------------------------
739  * ExecInsert
740  *
741  * For INSERT, we have to insert the tuple into the target relation
742  * (or partition thereof) and insert appropriate tuples into the index
743  * relations.
744  *
745  * slot contains the new tuple value to be stored.
746  *
747  * Returns RETURNING result if any, otherwise NULL.
748  * *inserted_tuple is the tuple that's effectively inserted;
749  * *insert_destrel is the relation where it was inserted.
750  * These are only set on success.
751  *
752  * This may change the currently active tuple conversion map in
753  * mtstate->mt_transition_capture, so the callers must take care to
754  * save the previous value to avoid losing track of it.
755  * ----------------------------------------------------------------
756  */
757 static TupleTableSlot *
759  ResultRelInfo *resultRelInfo,
760  TupleTableSlot *slot,
761  bool canSetTag,
762  TupleTableSlot **inserted_tuple,
763  ResultRelInfo **insert_destrel)
764 {
765  ModifyTableState *mtstate = context->mtstate;
766  EState *estate = context->estate;
767  Relation resultRelationDesc;
768  List *recheckIndexes = NIL;
769  TupleTableSlot *planSlot = context->planSlot;
770  TupleTableSlot *result = NULL;
771  TransitionCaptureState *ar_insert_trig_tcs;
772  ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
773  OnConflictAction onconflict = node->onConflictAction;
775  MemoryContext oldContext;
776 
777  /*
778  * If the input result relation is a partitioned table, find the leaf
779  * partition to insert the tuple into.
780  */
781  if (proute)
782  {
783  ResultRelInfo *partRelInfo;
784 
785  slot = ExecPrepareTupleRouting(mtstate, estate, proute,
786  resultRelInfo, slot,
787  &partRelInfo);
788  resultRelInfo = partRelInfo;
789  }
790 
791  ExecMaterializeSlot(slot);
792 
793  resultRelationDesc = resultRelInfo->ri_RelationDesc;
794 
795  /*
796  * Open the table's indexes, if we have not done so already, so that we
797  * can add new index entries for the inserted tuple.
798  */
799  if (resultRelationDesc->rd_rel->relhasindex &&
800  resultRelInfo->ri_IndexRelationDescs == NULL)
801  ExecOpenIndices(resultRelInfo, onconflict != ONCONFLICT_NONE);
802 
803  /*
804  * BEFORE ROW INSERT Triggers.
805  *
806  * Note: We fire BEFORE ROW TRIGGERS for every attempted insertion in an
807  * INSERT ... ON CONFLICT statement. We cannot check for constraint
808  * violations before firing these triggers, because they can change the
809  * values to insert. Also, they can run arbitrary user-defined code with
810  * side-effects that we can't cancel by just not inserting the tuple.
811  */
812  if (resultRelInfo->ri_TrigDesc &&
813  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
814  {
815  /* Flush any pending inserts, so rows are visible to the triggers */
817  ExecPendingInserts(estate);
818 
819  if (!ExecBRInsertTriggers(estate, resultRelInfo, slot))
820  return NULL; /* "do nothing" */
821  }
822 
823  /* INSTEAD OF ROW INSERT Triggers */
824  if (resultRelInfo->ri_TrigDesc &&
825  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
826  {
827  if (!ExecIRInsertTriggers(estate, resultRelInfo, slot))
828  return NULL; /* "do nothing" */
829  }
830  else if (resultRelInfo->ri_FdwRoutine)
831  {
832  /*
833  * GENERATED expressions might reference the tableoid column, so
834  * (re-)initialize tts_tableOid before evaluating them.
835  */
836  slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
837 
838  /*
839  * Compute stored generated columns
840  */
841  if (resultRelationDesc->rd_att->constr &&
842  resultRelationDesc->rd_att->constr->has_generated_stored)
843  ExecComputeStoredGenerated(resultRelInfo, estate, slot,
844  CMD_INSERT);
845 
846  /*
847  * If the FDW supports batching, and batching is requested, accumulate
848  * rows and insert them in batches. Otherwise use the per-row inserts.
849  */
850  if (resultRelInfo->ri_BatchSize > 1)
851  {
852  bool flushed = false;
853 
854  /*
855  * When we've reached the desired batch size, perform the
856  * insertion.
857  */
858  if (resultRelInfo->ri_NumSlots == resultRelInfo->ri_BatchSize)
859  {
860  ExecBatchInsert(mtstate, resultRelInfo,
861  resultRelInfo->ri_Slots,
862  resultRelInfo->ri_PlanSlots,
863  resultRelInfo->ri_NumSlots,
864  estate, canSetTag);
865  flushed = true;
866  }
867 
868  oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
869 
870  if (resultRelInfo->ri_Slots == NULL)
871  {
872  resultRelInfo->ri_Slots = palloc(sizeof(TupleTableSlot *) *
873  resultRelInfo->ri_BatchSize);
874  resultRelInfo->ri_PlanSlots = palloc(sizeof(TupleTableSlot *) *
875  resultRelInfo->ri_BatchSize);
876  }
877 
878  /*
879  * Initialize the batch slots. We don't know how many slots will
880  * be needed, so we initialize them as the batch grows, and we
881  * keep them across batches. To mitigate an inefficiency in how
882  * resource owner handles objects with many references (as with
883  * many slots all referencing the same tuple descriptor) we copy
884  * the appropriate tuple descriptor for each slot.
885  */
886  if (resultRelInfo->ri_NumSlots >= resultRelInfo->ri_NumSlotsInitialized)
887  {
889  TupleDesc plan_tdesc =
891 
892  resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots] =
893  MakeSingleTupleTableSlot(tdesc, slot->tts_ops);
894 
895  resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots] =
896  MakeSingleTupleTableSlot(plan_tdesc, planSlot->tts_ops);
897 
898  /* remember how many batch slots we initialized */
899  resultRelInfo->ri_NumSlotsInitialized++;
900  }
901 
902  ExecCopySlot(resultRelInfo->ri_Slots[resultRelInfo->ri_NumSlots],
903  slot);
904 
905  ExecCopySlot(resultRelInfo->ri_PlanSlots[resultRelInfo->ri_NumSlots],
906  planSlot);
907 
908  /*
909  * If these are the first tuples stored in the buffers, add the
910  * target rel and the mtstate to the
911  * es_insert_pending_result_relations and
912  * es_insert_pending_modifytables lists respectively, except in
913  * the case where flushing was done above, in which case they
914  * would already have been added to the lists, so no need to do
915  * this.
916  */
917  if (resultRelInfo->ri_NumSlots == 0 && !flushed)
918  {
920  resultRelInfo));
923  resultRelInfo);
925  lappend(estate->es_insert_pending_modifytables, mtstate);
926  }
928  resultRelInfo));
929 
930  resultRelInfo->ri_NumSlots++;
931 
932  MemoryContextSwitchTo(oldContext);
933 
934  return NULL;
935  }
936 
937  /*
938  * insert into foreign table: let the FDW do it
939  */
940  slot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
941  resultRelInfo,
942  slot,
943  planSlot);
944 
945  if (slot == NULL) /* "do nothing" */
946  return NULL;
947 
948  /*
949  * AFTER ROW Triggers or RETURNING expressions might reference the
950  * tableoid column, so (re-)initialize tts_tableOid before evaluating
951  * them. (This covers the case where the FDW replaced the slot.)
952  */
953  slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
954  }
955  else
956  {
957  WCOKind wco_kind;
958 
959  /*
960  * Constraints and GENERATED expressions might reference the tableoid
961  * column, so (re-)initialize tts_tableOid before evaluating them.
962  */
963  slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
964 
965  /*
966  * Compute stored generated columns
967  */
968  if (resultRelationDesc->rd_att->constr &&
969  resultRelationDesc->rd_att->constr->has_generated_stored)
970  ExecComputeStoredGenerated(resultRelInfo, estate, slot,
971  CMD_INSERT);
972 
973  /*
974  * Check any RLS WITH CHECK policies.
975  *
976  * Normally we should check INSERT policies. But if the insert is the
977  * result of a partition key update that moved the tuple to a new
978  * partition, we should instead check UPDATE policies, because we are
979  * executing policies defined on the target table, and not those
980  * defined on the child partitions.
981  *
982  * If we're running MERGE, we refer to the action that we're executing
983  * to know if we're doing an INSERT or UPDATE to a partition table.
984  */
985  if (mtstate->operation == CMD_UPDATE)
986  wco_kind = WCO_RLS_UPDATE_CHECK;
987  else if (mtstate->operation == CMD_MERGE)
988  wco_kind = (mtstate->mt_merge_action->mas_action->commandType == CMD_UPDATE) ?
990  else
991  wco_kind = WCO_RLS_INSERT_CHECK;
992 
993  /*
994  * ExecWithCheckOptions() will skip any WCOs which are not of the kind
995  * we are looking for at this point.
996  */
997  if (resultRelInfo->ri_WithCheckOptions != NIL)
998  ExecWithCheckOptions(wco_kind, resultRelInfo, slot, estate);
999 
1000  /*
1001  * Check the constraints of the tuple.
1002  */
1003  if (resultRelationDesc->rd_att->constr)
1004  ExecConstraints(resultRelInfo, slot, estate);
1005 
1006  /*
1007  * Also check the tuple against the partition constraint, if there is
1008  * one; except that if we got here via tuple-routing, we don't need to
1009  * if there's no BR trigger defined on the partition.
1010  */
1011  if (resultRelationDesc->rd_rel->relispartition &&
1012  (resultRelInfo->ri_RootResultRelInfo == NULL ||
1013  (resultRelInfo->ri_TrigDesc &&
1014  resultRelInfo->ri_TrigDesc->trig_insert_before_row)))
1015  ExecPartitionCheck(resultRelInfo, slot, estate, true);
1016 
1017  if (onconflict != ONCONFLICT_NONE && resultRelInfo->ri_NumIndices > 0)
1018  {
1019  /* Perform a speculative insertion. */
1020  uint32 specToken;
1021  ItemPointerData conflictTid;
1022  ItemPointerData invalidItemPtr;
1023  bool specConflict;
1024  List *arbiterIndexes;
1025 
1026  ItemPointerSetInvalid(&invalidItemPtr);
1027  arbiterIndexes = resultRelInfo->ri_onConflictArbiterIndexes;
1028 
1029  /*
1030  * Do a non-conclusive check for conflicts first.
1031  *
1032  * We're not holding any locks yet, so this doesn't guarantee that
1033  * the later insert won't conflict. But it avoids leaving behind
1034  * a lot of canceled speculative insertions, if you run a lot of
1035  * INSERT ON CONFLICT statements that do conflict.
1036  *
1037  * We loop back here if we find a conflict below, either during
1038  * the pre-check, or when we re-check after inserting the tuple
1039  * speculatively. Better allow interrupts in case some bug makes
1040  * this an infinite loop.
1041  */
1042  vlock:
1044  specConflict = false;
1045  if (!ExecCheckIndexConstraints(resultRelInfo, slot, estate,
1046  &conflictTid, &invalidItemPtr,
1047  arbiterIndexes))
1048  {
1049  /* committed conflict tuple found */
1050  if (onconflict == ONCONFLICT_UPDATE)
1051  {
1052  /*
1053  * In case of ON CONFLICT DO UPDATE, execute the UPDATE
1054  * part. Be prepared to retry if the UPDATE fails because
1055  * of another concurrent UPDATE/DELETE to the conflict
1056  * tuple.
1057  */
1058  TupleTableSlot *returning = NULL;
1059 
1060  if (ExecOnConflictUpdate(context, resultRelInfo,
1061  &conflictTid, slot, canSetTag,
1062  &returning))
1063  {
1064  InstrCountTuples2(&mtstate->ps, 1);
1065  return returning;
1066  }
1067  else
1068  goto vlock;
1069  }
1070  else
1071  {
1072  /*
1073  * In case of ON CONFLICT DO NOTHING, do nothing. However,
1074  * verify that the tuple is visible to the executor's MVCC
1075  * snapshot at higher isolation levels.
1076  *
1077  * Using ExecGetReturningSlot() to store the tuple for the
1078  * recheck isn't that pretty, but we can't trivially use
1079  * the input slot, because it might not be of a compatible
1080  * type. As there's no conflicting usage of
1081  * ExecGetReturningSlot() in the DO NOTHING case...
1082  */
1083  Assert(onconflict == ONCONFLICT_NOTHING);
1084  ExecCheckTIDVisible(estate, resultRelInfo, &conflictTid,
1085  ExecGetReturningSlot(estate, resultRelInfo));
1086  InstrCountTuples2(&mtstate->ps, 1);
1087  return NULL;
1088  }
1089  }
1090 
1091  /*
1092  * Before we start insertion proper, acquire our "speculative
1093  * insertion lock". Others can use that to wait for us to decide
1094  * if we're going to go ahead with the insertion, instead of
1095  * waiting for the whole transaction to complete.
1096  */
1098 
1099  /* insert the tuple, with the speculative token */
1100  table_tuple_insert_speculative(resultRelationDesc, slot,
1101  estate->es_output_cid,
1102  0,
1103  NULL,
1104  specToken);
1105 
1106  /* insert index entries for tuple */
1107  recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
1108  slot, estate, false, true,
1109  &specConflict,
1110  arbiterIndexes,
1111  false);
1112 
1113  /* adjust the tuple's state accordingly */
1114  table_tuple_complete_speculative(resultRelationDesc, slot,
1115  specToken, !specConflict);
1116 
1117  /*
1118  * Wake up anyone waiting for our decision. They will re-check
1119  * the tuple, see that it's no longer speculative, and wait on our
1120  * XID as if this was a regularly inserted tuple all along. Or if
1121  * we killed the tuple, they will see it's dead, and proceed as if
1122  * the tuple never existed.
1123  */
1125 
1126  /*
1127  * If there was a conflict, start from the beginning. We'll do
1128  * the pre-check again, which will now find the conflicting tuple
1129  * (unless it aborts before we get there).
1130  */
1131  if (specConflict)
1132  {
1133  list_free(recheckIndexes);
1134  goto vlock;
1135  }
1136 
1137  /* Since there was no insertion conflict, we're done */
1138  }
1139  else
1140  {
1141  /* insert the tuple normally */
1142  table_tuple_insert(resultRelationDesc, slot,
1143  estate->es_output_cid,
1144  0, NULL);
1145 
1146  /* insert index entries for tuple */
1147  if (resultRelInfo->ri_NumIndices > 0)
1148  recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
1149  slot, estate, false,
1150  false, NULL, NIL,
1151  false);
1152  }
1153  }
1154 
1155  if (canSetTag)
1156  (estate->es_processed)++;
1157 
1158  /*
1159  * If this insert is the result of a partition key update that moved the
1160  * tuple to a new partition, put this row into the transition NEW TABLE,
1161  * if there is one. We need to do this separately for DELETE and INSERT
1162  * because they happen on different tables.
1163  */
1164  ar_insert_trig_tcs = mtstate->mt_transition_capture;
1165  if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture
1167  {
1168  ExecARUpdateTriggers(estate, resultRelInfo,
1169  NULL, NULL,
1170  NULL,
1171  NULL,
1172  slot,
1173  NULL,
1174  mtstate->mt_transition_capture,
1175  false);
1176 
1177  /*
1178  * We've already captured the NEW TABLE row, so make sure any AR
1179  * INSERT trigger fired below doesn't capture it again.
1180  */
1181  ar_insert_trig_tcs = NULL;
1182  }
1183 
1184  /* AFTER ROW INSERT Triggers */
1185  ExecARInsertTriggers(estate, resultRelInfo, slot, recheckIndexes,
1186  ar_insert_trig_tcs);
1187 
1188  list_free(recheckIndexes);
1189 
1190  /*
1191  * Check any WITH CHECK OPTION constraints from parent views. We are
1192  * required to do this after testing all constraints and uniqueness
1193  * violations per the SQL spec, so we do it after actually inserting the
1194  * record into the heap and all indexes.
1195  *
1196  * ExecWithCheckOptions will elog(ERROR) if a violation is found, so the
1197  * tuple will never be seen, if it violates the WITH CHECK OPTION.
1198  *
1199  * ExecWithCheckOptions() will skip any WCOs which are not of the kind we
1200  * are looking for at this point.
1201  */
1202  if (resultRelInfo->ri_WithCheckOptions != NIL)
1203  ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);
1204 
1205  /* Process RETURNING if present */
1206  if (resultRelInfo->ri_projectReturning)
1207  result = ExecProcessReturning(resultRelInfo, slot, planSlot);
1208 
1209  if (inserted_tuple)
1210  *inserted_tuple = slot;
1211  if (insert_destrel)
1212  *insert_destrel = resultRelInfo;
1213 
1214  return result;
1215 }
1216 
1217 /* ----------------------------------------------------------------
1218  * ExecBatchInsert
1219  *
1220  * Insert multiple tuples in an efficient way.
1221  * Currently, this handles inserting into a foreign table without
1222  * RETURNING clause.
1223  * ----------------------------------------------------------------
1224  */
1225 static void
1227  ResultRelInfo *resultRelInfo,
1228  TupleTableSlot **slots,
1229  TupleTableSlot **planSlots,
1230  int numSlots,
1231  EState *estate,
1232  bool canSetTag)
1233 {
1234  int i;
1235  int numInserted = numSlots;
1236  TupleTableSlot *slot = NULL;
1237  TupleTableSlot **rslots;
1238 
1239  /*
1240  * insert into foreign table: let the FDW do it
1241  */
1242  rslots = resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert(estate,
1243  resultRelInfo,
1244  slots,
1245  planSlots,
1246  &numInserted);
1247 
1248  for (i = 0; i < numInserted; i++)
1249  {
1250  slot = rslots[i];
1251 
1252  /*
1253  * AFTER ROW Triggers might reference the tableoid column, so
1254  * (re-)initialize tts_tableOid before evaluating them.
1255  */
1256  slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
1257 
1258  /* AFTER ROW INSERT Triggers */
1259  ExecARInsertTriggers(estate, resultRelInfo, slot, NIL,
1260  mtstate->mt_transition_capture);
1261 
1262  /*
1263  * Check any WITH CHECK OPTION constraints from parent views. See the
1264  * comment in ExecInsert.
1265  */
1266  if (resultRelInfo->ri_WithCheckOptions != NIL)
1267  ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);
1268  }
1269 
1270  if (canSetTag && numInserted > 0)
1271  estate->es_processed += numInserted;
1272 
1273  /* Clean up all the slots, ready for the next batch */
1274  for (i = 0; i < numSlots; i++)
1275  {
1276  ExecClearTuple(slots[i]);
1277  ExecClearTuple(planSlots[i]);
1278  }
1279  resultRelInfo->ri_NumSlots = 0;
1280 }
1281 
1282 /*
1283  * ExecPendingInserts -- flushes all pending inserts to the foreign tables
1284  */
1285 static void
1287 {
1288  ListCell *l1,
1289  *l2;
1290 
1292  l2, estate->es_insert_pending_modifytables)
1293  {
1294  ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l1);
1295  ModifyTableState *mtstate = (ModifyTableState *) lfirst(l2);
1296 
1297  Assert(mtstate);
1298  ExecBatchInsert(mtstate, resultRelInfo,
1299  resultRelInfo->ri_Slots,
1300  resultRelInfo->ri_PlanSlots,
1301  resultRelInfo->ri_NumSlots,
1302  estate, mtstate->canSetTag);
1303  }
1304 
1309 }
1310 
1311 /*
1312  * ExecDeletePrologue -- subroutine for ExecDelete
1313  *
1314  * Prepare executor state for DELETE. Actually, the only thing we have to do
1315  * here is execute BEFORE ROW triggers. We return false if one of them makes
1316  * the delete a no-op; otherwise, return true.
1317  */
1318 static bool
1320  ItemPointer tupleid, HeapTuple oldtuple,
1321  TupleTableSlot **epqreturnslot, TM_Result *result)
1322 {
1323  if (result)
1324  *result = TM_Ok;
1325 
1326  /* BEFORE ROW DELETE triggers */
1327  if (resultRelInfo->ri_TrigDesc &&
1328  resultRelInfo->ri_TrigDesc->trig_delete_before_row)
1329  {
1330  /* Flush any pending inserts, so rows are visible to the triggers */
1331  if (context->estate->es_insert_pending_result_relations != NIL)
1332  ExecPendingInserts(context->estate);
1333 
1334  return ExecBRDeleteTriggers(context->estate, context->epqstate,
1335  resultRelInfo, tupleid, oldtuple,
1336  epqreturnslot, result, &context->tmfd);
1337  }
1338 
1339  return true;
1340 }
1341 
1342 /*
1343  * ExecDeleteAct -- subroutine for ExecDelete
1344  *
1345  * Actually delete the tuple from a plain table.
1346  *
1347  * Caller is in charge of doing EvalPlanQual as necessary
1348  */
1349 static TM_Result
1351  ItemPointer tupleid, bool changingPart)
1352 {
1353  EState *estate = context->estate;
1354 
1355  return table_tuple_delete(resultRelInfo->ri_RelationDesc, tupleid,
1356  estate->es_output_cid,
1357  estate->es_snapshot,
1358  estate->es_crosscheck_snapshot,
1359  true /* wait for commit */ ,
1360  &context->tmfd,
1361  changingPart);
1362 }
1363 
1364 /*
1365  * ExecDeleteEpilogue -- subroutine for ExecDelete
1366  *
1367  * Closing steps of tuple deletion; this invokes AFTER FOR EACH ROW triggers,
1368  * including the UPDATE triggers if the deletion is being done as part of a
1369  * cross-partition tuple move.
1370  */
1371 static void
1373  ItemPointer tupleid, HeapTuple oldtuple, bool changingPart)
1374 {
1375  ModifyTableState *mtstate = context->mtstate;
1376  EState *estate = context->estate;
1377  TransitionCaptureState *ar_delete_trig_tcs;
1378 
1379  /*
1380  * If this delete is the result of a partition key update that moved the
1381  * tuple to a new partition, put this row into the transition OLD TABLE,
1382  * if there is one. We need to do this separately for DELETE and INSERT
1383  * because they happen on different tables.
1384  */
1385  ar_delete_trig_tcs = mtstate->mt_transition_capture;
1386  if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture &&
1388  {
1389  ExecARUpdateTriggers(estate, resultRelInfo,
1390  NULL, NULL,
1391  tupleid, oldtuple,
1392  NULL, NULL, mtstate->mt_transition_capture,
1393  false);
1394 
1395  /*
1396  * We've already captured the OLD TABLE row, so make sure any AR
1397  * DELETE trigger fired below doesn't capture it again.
1398  */
1399  ar_delete_trig_tcs = NULL;
1400  }
1401 
1402  /* AFTER ROW DELETE Triggers */
1403  ExecARDeleteTriggers(estate, resultRelInfo, tupleid, oldtuple,
1404  ar_delete_trig_tcs, changingPart);
1405 }
1406 
1407 /* ----------------------------------------------------------------
1408  * ExecDelete
1409  *
1410  * DELETE is like UPDATE, except that we delete the tuple and no
1411  * index modifications are needed.
1412  *
1413  * When deleting from a table, tupleid identifies the tuple to delete and
1414  * oldtuple is NULL. When deleting through a view INSTEAD OF trigger,
1415  * oldtuple is passed to the triggers and identifies what to delete, and
1416  * tupleid is invalid. When deleting from a foreign table, tupleid is
1417  * invalid; the FDW has to figure out which row to delete using data from
1418  * the planSlot. oldtuple is passed to foreign table triggers; it is
1419  * NULL when the foreign table has no relevant triggers. We use
1420  * tupleDeleted to indicate whether the tuple is actually deleted,
1421  * callers can use it to decide whether to continue the operation. When
1422  * this DELETE is a part of an UPDATE of partition-key, then the slot
1423  * returned by EvalPlanQual() is passed back using output parameter
1424  * epqreturnslot.
1425  *
1426  * Returns RETURNING result if any, otherwise NULL.
1427  * ----------------------------------------------------------------
1428  */
1429 static TupleTableSlot *
1431  ResultRelInfo *resultRelInfo,
1432  ItemPointer tupleid,
1433  HeapTuple oldtuple,
1434  bool processReturning,
1435  bool changingPart,
1436  bool canSetTag,
1437  TM_Result *tmresult,
1438  bool *tupleDeleted,
1439  TupleTableSlot **epqreturnslot)
1440 {
1441  EState *estate = context->estate;
1442  Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
1443  TupleTableSlot *slot = NULL;
1444  TM_Result result;
1445 
1446  if (tupleDeleted)
1447  *tupleDeleted = false;
1448 
1449  /*
1450  * Prepare for the delete. This includes BEFORE ROW triggers, so we're
1451  * done if it says we are.
1452  */
1453  if (!ExecDeletePrologue(context, resultRelInfo, tupleid, oldtuple,
1454  epqreturnslot, tmresult))
1455  return NULL;
1456 
1457  /* INSTEAD OF ROW DELETE Triggers */
1458  if (resultRelInfo->ri_TrigDesc &&
1459  resultRelInfo->ri_TrigDesc->trig_delete_instead_row)
1460  {
1461  bool dodelete;
1462 
1463  Assert(oldtuple != NULL);
1464  dodelete = ExecIRDeleteTriggers(estate, resultRelInfo, oldtuple);
1465 
1466  if (!dodelete) /* "do nothing" */
1467  return NULL;
1468  }
1469  else if (resultRelInfo->ri_FdwRoutine)
1470  {
1471  /*
1472  * delete from foreign table: let the FDW do it
1473  *
1474  * We offer the returning slot as a place to store RETURNING data,
1475  * although the FDW can return some other slot if it wants.
1476  */
1477  slot = ExecGetReturningSlot(estate, resultRelInfo);
1478  slot = resultRelInfo->ri_FdwRoutine->ExecForeignDelete(estate,
1479  resultRelInfo,
1480  slot,
1481  context->planSlot);
1482 
1483  if (slot == NULL) /* "do nothing" */
1484  return NULL;
1485 
1486  /*
1487  * RETURNING expressions might reference the tableoid column, so
1488  * (re)initialize tts_tableOid before evaluating them.
1489  */
1490  if (TTS_EMPTY(slot))
1491  ExecStoreAllNullTuple(slot);
1492 
1493  slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
1494  }
1495  else
1496  {
1497  /*
1498  * delete the tuple
1499  *
1500  * Note: if context->estate->es_crosscheck_snapshot isn't
1501  * InvalidSnapshot, we check that the row to be deleted is visible to
1502  * that snapshot, and throw a can't-serialize error if not. This is a
1503  * special-case behavior needed for referential integrity updates in
1504  * transaction-snapshot mode transactions.
1505  */
1506 ldelete:
1507  result = ExecDeleteAct(context, resultRelInfo, tupleid, changingPart);
1508 
1509  if (tmresult)
1510  *tmresult = result;
1511 
1512  switch (result)
1513  {
1514  case TM_SelfModified:
1515 
1516  /*
1517  * The target tuple was already updated or deleted by the
1518  * current command, or by a later command in the current
1519  * transaction. The former case is possible in a join DELETE
1520  * where multiple tuples join to the same target tuple. This
1521  * is somewhat questionable, but Postgres has always allowed
1522  * it: we just ignore additional deletion attempts.
1523  *
1524  * The latter case arises if the tuple is modified by a
1525  * command in a BEFORE trigger, or perhaps by a command in a
1526  * volatile function used in the query. In such situations we
1527  * should not ignore the deletion, but it is equally unsafe to
1528  * proceed. We don't want to discard the original DELETE
1529  * while keeping the triggered actions based on its deletion;
1530  * and it would be no better to allow the original DELETE
1531  * while discarding updates that it triggered. The row update
1532  * carries some information that might be important according
1533  * to business rules; so throwing an error is the only safe
1534  * course.
1535  *
1536  * If a trigger actually intends this type of interaction, it
1537  * can re-execute the DELETE and then return NULL to cancel
1538  * the outer delete.
1539  */
1540  if (context->tmfd.cmax != estate->es_output_cid)
1541  ereport(ERROR,
1542  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
1543  errmsg("tuple to be deleted was already modified by an operation triggered by the current command"),
1544  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
1545 
1546  /* Else, already deleted by self; nothing to do */
1547  return NULL;
1548 
1549  case TM_Ok:
1550  break;
1551 
1552  case TM_Updated:
1553  {
1554  TupleTableSlot *inputslot;
1555  TupleTableSlot *epqslot;
1556 
1558  ereport(ERROR,
1560  errmsg("could not serialize access due to concurrent update")));
1561 
1562  /*
1563  * Already know that we're going to need to do EPQ, so
1564  * fetch tuple directly into the right slot.
1565  */
1566  EvalPlanQualBegin(context->epqstate);
1567  inputslot = EvalPlanQualSlot(context->epqstate, resultRelationDesc,
1568  resultRelInfo->ri_RangeTableIndex);
1569 
1570  result = table_tuple_lock(resultRelationDesc, tupleid,
1571  estate->es_snapshot,
1572  inputslot, estate->es_output_cid,
1575  &context->tmfd);
1576 
1577  switch (result)
1578  {
1579  case TM_Ok:
1580  Assert(context->tmfd.traversed);
1581  epqslot = EvalPlanQual(context->epqstate,
1582  resultRelationDesc,
1583  resultRelInfo->ri_RangeTableIndex,
1584  inputslot);
1585  if (TupIsNull(epqslot))
1586  /* Tuple not passing quals anymore, exiting... */
1587  return NULL;
1588 
1589  /*
1590  * If requested, skip delete and pass back the
1591  * updated row.
1592  */
1593  if (epqreturnslot)
1594  {
1595  *epqreturnslot = epqslot;
1596  return NULL;
1597  }
1598  else
1599  goto ldelete;
1600 
1601  case TM_SelfModified:
1602 
1603  /*
1604  * This can be reached when following an update
1605  * chain from a tuple updated by another session,
1606  * reaching a tuple that was already updated in
1607  * this transaction. If previously updated by this
1608  * command, ignore the delete, otherwise error
1609  * out.
1610  *
1611  * See also TM_SelfModified response to
1612  * table_tuple_delete() above.
1613  */
1614  if (context->tmfd.cmax != estate->es_output_cid)
1615  ereport(ERROR,
1616  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
1617  errmsg("tuple to be deleted was already modified by an operation triggered by the current command"),
1618  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
1619  return NULL;
1620 
1621  case TM_Deleted:
1622  /* tuple already deleted; nothing to do */
1623  return NULL;
1624 
1625  default:
1626 
1627  /*
1628  * TM_Invisible should be impossible because we're
1629  * waiting for updated row versions, and would
1630  * already have errored out if the first version
1631  * is invisible.
1632  *
1633  * TM_Updated should be impossible, because we're
1634  * locking the latest version via
1635  * TUPLE_LOCK_FLAG_FIND_LAST_VERSION.
1636  */
1637  elog(ERROR, "unexpected table_tuple_lock status: %u",
1638  result);
1639  return NULL;
1640  }
1641 
1642  Assert(false);
1643  break;
1644  }
1645 
1646  case TM_Deleted:
1648  ereport(ERROR,
1650  errmsg("could not serialize access due to concurrent delete")));
1651  /* tuple already deleted; nothing to do */
1652  return NULL;
1653 
1654  default:
1655  elog(ERROR, "unrecognized table_tuple_delete status: %u",
1656  result);
1657  return NULL;
1658  }
1659 
1660  /*
1661  * Note: Normally one would think that we have to delete index tuples
1662  * associated with the heap tuple now...
1663  *
1664  * ... but in POSTGRES, we have no need to do this because VACUUM will
1665  * take care of it later. We can't delete index tuples immediately
1666  * anyway, since the tuple is still visible to other transactions.
1667  */
1668  }
1669 
1670  if (canSetTag)
1671  (estate->es_processed)++;
1672 
1673  /* Tell caller that the delete actually happened. */
1674  if (tupleDeleted)
1675  *tupleDeleted = true;
1676 
1677  ExecDeleteEpilogue(context, resultRelInfo, tupleid, oldtuple, changingPart);
1678 
1679  /* Process RETURNING if present and if requested */
1680  if (processReturning && resultRelInfo->ri_projectReturning)
1681  {
1682  /*
1683  * We have to put the target tuple into a slot, which means first we
1684  * gotta fetch it. We can use the trigger tuple slot.
1685  */
1686  TupleTableSlot *rslot;
1687 
1688  if (resultRelInfo->ri_FdwRoutine)
1689  {
1690  /* FDW must have provided a slot containing the deleted row */
1691  Assert(!TupIsNull(slot));
1692  }
1693  else
1694  {
1695  slot = ExecGetReturningSlot(estate, resultRelInfo);
1696  if (oldtuple != NULL)
1697  {
1698  ExecForceStoreHeapTuple(oldtuple, slot, false);
1699  }
1700  else
1701  {
1702  if (!table_tuple_fetch_row_version(resultRelationDesc, tupleid,
1703  SnapshotAny, slot))
1704  elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
1705  }
1706  }
1707 
1708  rslot = ExecProcessReturning(resultRelInfo, slot, context->planSlot);
1709 
1710  /*
1711  * Before releasing the target tuple again, make sure rslot has a
1712  * local copy of any pass-by-reference values.
1713  */
1714  ExecMaterializeSlot(rslot);
1715 
1716  ExecClearTuple(slot);
1717 
1718  return rslot;
1719  }
1720 
1721  return NULL;
1722 }
1723 
1724 /*
1725  * ExecCrossPartitionUpdate --- Move an updated tuple to another partition.
1726  *
1727  * This works by first deleting the old tuple from the current partition,
1728  * followed by inserting the new tuple into the root parent table, that is,
1729  * mtstate->rootResultRelInfo. It will be re-routed from there to the
1730  * correct partition.
1731  *
1732  * Returns true if the tuple has been successfully moved, or if it's found
1733  * that the tuple was concurrently deleted so there's nothing more to do
1734  * for the caller.
1735  *
1736  * False is returned if the tuple we're trying to move is found to have been
1737  * concurrently updated. In that case, the caller must check if the updated
1738  * tuple that's returned in *retry_slot still needs to be re-routed, and call
1739  * this function again or perform a regular update accordingly. For MERGE,
1740  * the updated tuple is not returned in *retry_slot; it has its own retry
1741  * logic.
1742  */
1743 static bool
1745  ResultRelInfo *resultRelInfo,
1746  ItemPointer tupleid, HeapTuple oldtuple,
1747  TupleTableSlot *slot,
1748  bool canSetTag,
1749  UpdateContext *updateCxt,
1750  TM_Result *tmresult,
1751  TupleTableSlot **retry_slot,
1752  TupleTableSlot **inserted_tuple,
1753  ResultRelInfo **insert_destrel)
1754 {
1755  ModifyTableState *mtstate = context->mtstate;
1756  EState *estate = mtstate->ps.state;
1757  TupleConversionMap *tupconv_map;
1758  bool tuple_deleted;
1759  TupleTableSlot *epqslot = NULL;
1760 
1761  context->cpUpdateReturningSlot = NULL;
1762  *retry_slot = NULL;
1763 
1764  /*
1765  * Disallow an INSERT ON CONFLICT DO UPDATE that causes the original row
1766  * to migrate to a different partition. Maybe this can be implemented
1767  * some day, but it seems a fringe feature with little redeeming value.
1768  */
1769  if (((ModifyTable *) mtstate->ps.plan)->onConflictAction == ONCONFLICT_UPDATE)
1770  ereport(ERROR,
1771  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1772  errmsg("invalid ON UPDATE specification"),
1773  errdetail("The result tuple would appear in a different partition than the original tuple.")));
1774 
1775  /*
1776  * When an UPDATE is run directly on a leaf partition, simply fail with a
1777  * partition constraint violation error.
1778  */
1779  if (resultRelInfo == mtstate->rootResultRelInfo)
1780  ExecPartitionCheckEmitError(resultRelInfo, slot, estate);
1781 
1782  /* Initialize tuple routing info if not already done. */
1783  if (mtstate->mt_partition_tuple_routing == NULL)
1784  {
1785  Relation rootRel = mtstate->rootResultRelInfo->ri_RelationDesc;
1786  MemoryContext oldcxt;
1787 
1788  /* Things built here have to last for the query duration. */
1789  oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
1790 
1791  mtstate->mt_partition_tuple_routing =
1792  ExecSetupPartitionTupleRouting(estate, rootRel);
1793 
1794  /*
1795  * Before a partition's tuple can be re-routed, it must first be
1796  * converted to the root's format, so we'll need a slot for storing
1797  * such tuples.
1798  */
1799  Assert(mtstate->mt_root_tuple_slot == NULL);
1800  mtstate->mt_root_tuple_slot = table_slot_create(rootRel, NULL);
1801 
1802  MemoryContextSwitchTo(oldcxt);
1803  }
1804 
1805  /*
1806  * Row movement, part 1. Delete the tuple, but skip RETURNING processing.
1807  * We want to return rows from INSERT.
1808  */
1809  ExecDelete(context, resultRelInfo,
1810  tupleid, oldtuple,
1811  false, /* processReturning */
1812  true, /* changingPart */
1813  false, /* canSetTag */
1814  tmresult, &tuple_deleted, &epqslot);
1815 
1816  /*
1817  * For some reason if DELETE didn't happen (e.g. trigger prevented it, or
1818  * it was already deleted by self, or it was concurrently deleted by
1819  * another transaction), then we should skip the insert as well;
1820  * otherwise, an UPDATE could cause an increase in the total number of
1821  * rows across all partitions, which is clearly wrong.
1822  *
1823  * For a normal UPDATE, the case where the tuple has been the subject of a
1824  * concurrent UPDATE or DELETE would be handled by the EvalPlanQual
1825  * machinery, but for an UPDATE that we've translated into a DELETE from
1826  * this partition and an INSERT into some other partition, that's not
1827  * available, because CTID chains can't span relation boundaries. We
1828  * mimic the semantics to a limited extent by skipping the INSERT if the
1829  * DELETE fails to find a tuple. This ensures that two concurrent
1830  * attempts to UPDATE the same tuple at the same time can't turn one tuple
1831  * into two, and that an UPDATE of a just-deleted tuple can't resurrect
1832  * it.
1833  */
1834  if (!tuple_deleted)
1835  {
1836  /*
1837  * epqslot will be typically NULL. But when ExecDelete() finds that
1838  * another transaction has concurrently updated the same row, it
1839  * re-fetches the row, skips the delete, and epqslot is set to the
1840  * re-fetched tuple slot. In that case, we need to do all the checks
1841  * again. For MERGE, we leave everything to the caller (it must do
1842  * additional rechecking, and might end up executing a different
1843  * action entirely).
1844  */
1845  if (mtstate->operation == CMD_MERGE)
1846  return *tmresult == TM_Ok;
1847  else if (TupIsNull(epqslot))
1848  return true;
1849  else
1850  {
1851  /* Fetch the most recent version of old tuple. */
1852  TupleTableSlot *oldSlot;
1853 
1854  /* ... but first, make sure ri_oldTupleSlot is initialized. */
1855  if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
1856  ExecInitUpdateProjection(mtstate, resultRelInfo);
1857  oldSlot = resultRelInfo->ri_oldTupleSlot;
1858  if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc,
1859  tupleid,
1860  SnapshotAny,
1861  oldSlot))
1862  elog(ERROR, "failed to fetch tuple being updated");
1863  /* and project the new tuple to retry the UPDATE with */
1864  *retry_slot = ExecGetUpdateNewTuple(resultRelInfo, epqslot,
1865  oldSlot);
1866  return false;
1867  }
1868  }
1869 
1870  /*
1871  * resultRelInfo is one of the per-relation resultRelInfos. So we should
1872  * convert the tuple into root's tuple descriptor if needed, since
1873  * ExecInsert() starts the search from root.
1874  */
1875  tupconv_map = ExecGetChildToRootMap(resultRelInfo);
1876  if (tupconv_map != NULL)
1877  slot = execute_attr_map_slot(tupconv_map->attrMap,
1878  slot,
1879  mtstate->mt_root_tuple_slot);
1880 
1881  /* Tuple routing starts from the root table. */
1882  context->cpUpdateReturningSlot =
1883  ExecInsert(context, mtstate->rootResultRelInfo, slot, canSetTag,
1884  inserted_tuple, insert_destrel);
1885 
1886  /*
1887  * Reset the transition state that may possibly have been written by
1888  * INSERT.
1889  */
1890  if (mtstate->mt_transition_capture)
1892 
1893  /* We're done moving. */
1894  return true;
1895 }
1896 
1897 /*
1898  * ExecUpdatePrologue -- subroutine for ExecUpdate
1899  *
1900  * Prepare executor state for UPDATE. This includes running BEFORE ROW
1901  * triggers. We return false if one of them makes the update a no-op;
1902  * otherwise, return true.
1903  */
1904 static bool
1906  ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
1907  TM_Result *result)
1908 {
1909  Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
1910 
1911  if (result)
1912  *result = TM_Ok;
1913 
1914  ExecMaterializeSlot(slot);
1915 
1916  /*
1917  * Open the table's indexes, if we have not done so already, so that we
1918  * can add new index entries for the updated tuple.
1919  */
1920  if (resultRelationDesc->rd_rel->relhasindex &&
1921  resultRelInfo->ri_IndexRelationDescs == NULL)
1922  ExecOpenIndices(resultRelInfo, false);
1923 
1924  /* BEFORE ROW UPDATE triggers */
1925  if (resultRelInfo->ri_TrigDesc &&
1926  resultRelInfo->ri_TrigDesc->trig_update_before_row)
1927  {
1928  /* Flush any pending inserts, so rows are visible to the triggers */
1929  if (context->estate->es_insert_pending_result_relations != NIL)
1930  ExecPendingInserts(context->estate);
1931 
1932  return ExecBRUpdateTriggers(context->estate, context->epqstate,
1933  resultRelInfo, tupleid, oldtuple, slot,
1934  result, &context->tmfd);
1935  }
1936 
1937  return true;
1938 }
1939 
1940 /*
1941  * ExecUpdatePrepareSlot -- subroutine for ExecUpdateAct
1942  *
1943  * Apply the final modifications to the tuple slot before the update.
1944  * (This is split out because we also need it in the foreign-table code path.)
1945  */
1946 static void
1948  TupleTableSlot *slot,
1949  EState *estate)
1950 {
1951  Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
1952 
1953  /*
1954  * Constraints and GENERATED expressions might reference the tableoid
1955  * column, so (re-)initialize tts_tableOid before evaluating them.
1956  */
1957  slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
1958 
1959  /*
1960  * Compute stored generated columns
1961  */
1962  if (resultRelationDesc->rd_att->constr &&
1963  resultRelationDesc->rd_att->constr->has_generated_stored)
1964  ExecComputeStoredGenerated(resultRelInfo, estate, slot,
1965  CMD_UPDATE);
1966 }
1967 
1968 /*
1969  * ExecUpdateAct -- subroutine for ExecUpdate
1970  *
1971  * Actually update the tuple, when operating on a plain table. If the
1972  * table is a partition, and the command was called referencing an ancestor
1973  * partitioned table, this routine migrates the resulting tuple to another
1974  * partition.
1975  *
1976  * The caller is in charge of keeping indexes current as necessary. The
1977  * caller is also in charge of doing EvalPlanQual if the tuple is found to
1978  * be concurrently updated. However, in case of a cross-partition update,
1979  * this routine does it.
1980  */
1981 static TM_Result
1983  ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
1984  bool canSetTag, UpdateContext *updateCxt)
1985 {
1986  EState *estate = context->estate;
1987  Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
1988  bool partition_constraint_failed;
1989  TM_Result result;
1990 
1991  updateCxt->crossPartUpdate = false;
1992 
1993  /*
1994  * If we move the tuple to a new partition, we loop back here to recompute
1995  * GENERATED values (which are allowed to be different across partitions)
1996  * and recheck any RLS policies and constraints. We do not fire any
1997  * BEFORE triggers of the new partition, however.
1998  */
1999 lreplace:
2000  /* Fill in GENERATEd columns */
2001  ExecUpdatePrepareSlot(resultRelInfo, slot, estate);
2002 
2003  /* ensure slot is independent, consider e.g. EPQ */
2004  ExecMaterializeSlot(slot);
2005 
2006  /*
2007  * If partition constraint fails, this row might get moved to another
2008  * partition, in which case we should check the RLS CHECK policy just
2009  * before inserting into the new partition, rather than doing it here.
2010  * This is because a trigger on that partition might again change the row.
2011  * So skip the WCO checks if the partition constraint fails.
2012  */
2013  partition_constraint_failed =
2014  resultRelationDesc->rd_rel->relispartition &&
2015  !ExecPartitionCheck(resultRelInfo, slot, estate, false);
2016 
2017  /* Check any RLS UPDATE WITH CHECK policies */
2018  if (!partition_constraint_failed &&
2019  resultRelInfo->ri_WithCheckOptions != NIL)
2020  {
2021  /*
2022  * ExecWithCheckOptions() will skip any WCOs which are not of the kind
2023  * we are looking for at this point.
2024  */
2026  resultRelInfo, slot, estate);
2027  }
2028 
2029  /*
2030  * If a partition check failed, try to move the row into the right
2031  * partition.
2032  */
2033  if (partition_constraint_failed)
2034  {
2035  TupleTableSlot *inserted_tuple,
2036  *retry_slot;
2037  ResultRelInfo *insert_destrel = NULL;
2038 
2039  /*
2040  * ExecCrossPartitionUpdate will first DELETE the row from the
2041  * partition it's currently in and then insert it back into the root
2042  * table, which will re-route it to the correct partition. However,
2043  * if the tuple has been concurrently updated, a retry is needed.
2044  */
2045  if (ExecCrossPartitionUpdate(context, resultRelInfo,
2046  tupleid, oldtuple, slot,
2047  canSetTag, updateCxt,
2048  &result,
2049  &retry_slot,
2050  &inserted_tuple,
2051  &insert_destrel))
2052  {
2053  /* success! */
2054  updateCxt->crossPartUpdate = true;
2055 
2056  /*
2057  * If the partitioned table being updated is referenced in foreign
2058  * keys, queue up trigger events to check that none of them were
2059  * violated. No special treatment is needed in
2060  * non-cross-partition update situations, because the leaf
2061  * partition's AR update triggers will take care of that. During
2062  * cross-partition updates implemented as delete on the source
2063  * partition followed by insert on the destination partition,
2064  * AR-UPDATE triggers of the root table (that is, the table
2065  * mentioned in the query) must be fired.
2066  *
2067  * NULL insert_destrel means that the move failed to occur, that
2068  * is, the update failed, so no need to anything in that case.
2069  */
2070  if (insert_destrel &&
2071  resultRelInfo->ri_TrigDesc &&
2072  resultRelInfo->ri_TrigDesc->trig_update_after_row)
2074  resultRelInfo,
2075  insert_destrel,
2076  tupleid, slot,
2077  inserted_tuple);
2078 
2079  return TM_Ok;
2080  }
2081 
2082  /*
2083  * No luck, a retry is needed. If running MERGE, we do not do so
2084  * here; instead let it handle that on its own rules.
2085  */
2086  if (context->mtstate->operation == CMD_MERGE)
2087  return result;
2088 
2089  /*
2090  * ExecCrossPartitionUpdate installed an updated version of the new
2091  * tuple in the retry slot; start over.
2092  */
2093  slot = retry_slot;
2094  goto lreplace;
2095  }
2096 
2097  /*
2098  * Check the constraints of the tuple. We've already checked the
2099  * partition constraint above; however, we must still ensure the tuple
2100  * passes all other constraints, so we will call ExecConstraints() and
2101  * have it validate all remaining checks.
2102  */
2103  if (resultRelationDesc->rd_att->constr)
2104  ExecConstraints(resultRelInfo, slot, estate);
2105 
2106  /*
2107  * replace the heap tuple
2108  *
2109  * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
2110  * the row to be updated is visible to that snapshot, and throw a
2111  * can't-serialize error if not. This is a special-case behavior needed
2112  * for referential integrity updates in transaction-snapshot mode
2113  * transactions.
2114  */
2115  result = table_tuple_update(resultRelationDesc, tupleid, slot,
2116  estate->es_output_cid,
2117  estate->es_snapshot,
2118  estate->es_crosscheck_snapshot,
2119  true /* wait for commit */ ,
2120  &context->tmfd, &updateCxt->lockmode,
2121  &updateCxt->updateIndexes);
2122 
2123  return result;
2124 }
2125 
2126 /*
2127  * ExecUpdateEpilogue -- subroutine for ExecUpdate
2128  *
2129  * Closing steps of updating a tuple. Must be called if ExecUpdateAct
2130  * returns indicating that the tuple was updated.
2131  */
2132 static void
2134  ResultRelInfo *resultRelInfo, ItemPointer tupleid,
2135  HeapTuple oldtuple, TupleTableSlot *slot)
2136 {
2137  ModifyTableState *mtstate = context->mtstate;
2138  List *recheckIndexes = NIL;
2139 
2140  /* insert index entries for tuple if necessary */
2141  if (resultRelInfo->ri_NumIndices > 0 && (updateCxt->updateIndexes != TU_None))
2142  recheckIndexes = ExecInsertIndexTuples(resultRelInfo,
2143  slot, context->estate,
2144  true, false,
2145  NULL, NIL,
2146  (updateCxt->updateIndexes == TU_Summarizing));
2147 
2148  /* AFTER ROW UPDATE Triggers */
2149  ExecARUpdateTriggers(context->estate, resultRelInfo,
2150  NULL, NULL,
2151  tupleid, oldtuple, slot,
2152  recheckIndexes,
2153  mtstate->operation == CMD_INSERT ?
2154  mtstate->mt_oc_transition_capture :
2155  mtstate->mt_transition_capture,
2156  false);
2157 
2158  list_free(recheckIndexes);
2159 
2160  /*
2161  * Check any WITH CHECK OPTION constraints from parent views. We are
2162  * required to do this after testing all constraints and uniqueness
2163  * violations per the SQL spec, so we do it after actually updating the
2164  * record in the heap and all indexes.
2165  *
2166  * ExecWithCheckOptions() will skip any WCOs which are not of the kind we
2167  * are looking for at this point.
2168  */
2169  if (resultRelInfo->ri_WithCheckOptions != NIL)
2170  ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo,
2171  slot, context->estate);
2172 }
2173 
2174 /*
2175  * Queues up an update event using the target root partitioned table's
2176  * trigger to check that a cross-partition update hasn't broken any foreign
2177  * keys pointing into it.
2178  */
2179 static void
2181  ResultRelInfo *sourcePartInfo,
2182  ResultRelInfo *destPartInfo,
2183  ItemPointer tupleid,
2184  TupleTableSlot *oldslot,
2185  TupleTableSlot *newslot)
2186 {
2187  ListCell *lc;
2188  ResultRelInfo *rootRelInfo;
2189  List *ancestorRels;
2190 
2191  rootRelInfo = sourcePartInfo->ri_RootResultRelInfo;
2192  ancestorRels = ExecGetAncestorResultRels(context->estate, sourcePartInfo);
2193 
2194  /*
2195  * For any foreign keys that point directly into a non-root ancestors of
2196  * the source partition, we can in theory fire an update event to enforce
2197  * those constraints using their triggers, if we could tell that both the
2198  * source and the destination partitions are under the same ancestor. But
2199  * for now, we simply report an error that those cannot be enforced.
2200  */
2201  foreach(lc, ancestorRels)
2202  {
2203  ResultRelInfo *rInfo = lfirst(lc);
2204  TriggerDesc *trigdesc = rInfo->ri_TrigDesc;
2205  bool has_noncloned_fkey = false;
2206 
2207  /* Root ancestor's triggers will be processed. */
2208  if (rInfo == rootRelInfo)
2209  continue;
2210 
2211  if (trigdesc && trigdesc->trig_update_after_row)
2212  {
2213  for (int i = 0; i < trigdesc->numtriggers; i++)
2214  {
2215  Trigger *trig = &trigdesc->triggers[i];
2216 
2217  if (!trig->tgisclone &&
2219  {
2220  has_noncloned_fkey = true;
2221  break;
2222  }
2223  }
2224  }
2225 
2226  if (has_noncloned_fkey)
2227  ereport(ERROR,
2228  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2229  errmsg("cannot move tuple across partitions when a non-root ancestor of the source partition is directly referenced in a foreign key"),
2230  errdetail("A foreign key points to ancestor \"%s\" but not the root ancestor \"%s\".",
2233  errhint("Consider defining the foreign key on table \"%s\".",
2234  RelationGetRelationName(rootRelInfo->ri_RelationDesc))));
2235  }
2236 
2237  /* Perform the root table's triggers. */
2238  ExecARUpdateTriggers(context->estate,
2239  rootRelInfo, sourcePartInfo, destPartInfo,
2240  tupleid, NULL, newslot, NIL, NULL, true);
2241 }
2242 
2243 /* ----------------------------------------------------------------
2244  * ExecUpdate
2245  *
2246  * note: we can't run UPDATE queries with transactions
2247  * off because UPDATEs are actually INSERTs and our
2248  * scan will mistakenly loop forever, updating the tuple
2249  * it just inserted.. This should be fixed but until it
2250  * is, we don't want to get stuck in an infinite loop
2251  * which corrupts your database..
2252  *
2253  * When updating a table, tupleid identifies the tuple to update and
2254  * oldtuple is NULL. When updating through a view INSTEAD OF trigger,
2255  * oldtuple is passed to the triggers and identifies what to update, and
2256  * tupleid is invalid. When updating a foreign table, tupleid is
2257  * invalid; the FDW has to figure out which row to update using data from
2258  * the planSlot. oldtuple is passed to foreign table triggers; it is
2259  * NULL when the foreign table has no relevant triggers.
2260  *
2261  * slot contains the new tuple value to be stored.
2262  * planSlot is the output of the ModifyTable's subplan; we use it
2263  * to access values from other input tables (for RETURNING),
2264  * row-ID junk columns, etc.
2265  *
2266  * Returns RETURNING result if any, otherwise NULL. On exit, if tupleid
2267  * had identified the tuple to update, it will identify the tuple
2268  * actually updated after EvalPlanQual.
2269  * ----------------------------------------------------------------
2270  */
2271 static TupleTableSlot *
2273  ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot,
2274  bool canSetTag)
2275 {
2276  EState *estate = context->estate;
2277  Relation resultRelationDesc = resultRelInfo->ri_RelationDesc;
2278  UpdateContext updateCxt = {0};
2279  TM_Result result;
2280 
2281  /*
2282  * abort the operation if not running transactions
2283  */
2285  elog(ERROR, "cannot UPDATE during bootstrap");
2286 
2287  /*
2288  * Prepare for the update. This includes BEFORE ROW triggers, so we're
2289  * done if it says we are.
2290  */
2291  if (!ExecUpdatePrologue(context, resultRelInfo, tupleid, oldtuple, slot, NULL))
2292  return NULL;
2293 
2294  /* INSTEAD OF ROW UPDATE Triggers */
2295  if (resultRelInfo->ri_TrigDesc &&
2296  resultRelInfo->ri_TrigDesc->trig_update_instead_row)
2297  {
2298  if (!ExecIRUpdateTriggers(estate, resultRelInfo,
2299  oldtuple, slot))
2300  return NULL; /* "do nothing" */
2301  }
2302  else if (resultRelInfo->ri_FdwRoutine)
2303  {
2304  /* Fill in GENERATEd columns */
2305  ExecUpdatePrepareSlot(resultRelInfo, slot, estate);
2306 
2307  /*
2308  * update in foreign table: let the FDW do it
2309  */
2310  slot = resultRelInfo->ri_FdwRoutine->ExecForeignUpdate(estate,
2311  resultRelInfo,
2312  slot,
2313  context->planSlot);
2314 
2315  if (slot == NULL) /* "do nothing" */
2316  return NULL;
2317 
2318  /*
2319  * AFTER ROW Triggers or RETURNING expressions might reference the
2320  * tableoid column, so (re-)initialize tts_tableOid before evaluating
2321  * them. (This covers the case where the FDW replaced the slot.)
2322  */
2323  slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
2324  }
2325  else
2326  {
2327  /*
2328  * If we generate a new candidate tuple after EvalPlanQual testing, we
2329  * must loop back here to try again. (We don't need to redo triggers,
2330  * however. If there are any BEFORE triggers then trigger.c will have
2331  * done table_tuple_lock to lock the correct tuple, so there's no need
2332  * to do them again.)
2333  */
2334 redo_act:
2335  result = ExecUpdateAct(context, resultRelInfo, tupleid, oldtuple, slot,
2336  canSetTag, &updateCxt);
2337 
2338  /*
2339  * If ExecUpdateAct reports that a cross-partition update was done,
2340  * then the RETURNING tuple (if any) has been projected and there's
2341  * nothing else for us to do.
2342  */
2343  if (updateCxt.crossPartUpdate)
2344  return context->cpUpdateReturningSlot;
2345 
2346  switch (result)
2347  {
2348  case TM_SelfModified:
2349 
2350  /*
2351  * The target tuple was already updated or deleted by the
2352  * current command, or by a later command in the current
2353  * transaction. The former case is possible in a join UPDATE
2354  * where multiple tuples join to the same target tuple. This
2355  * is pretty questionable, but Postgres has always allowed it:
2356  * we just execute the first update action and ignore
2357  * additional update attempts.
2358  *
2359  * The latter case arises if the tuple is modified by a
2360  * command in a BEFORE trigger, or perhaps by a command in a
2361  * volatile function used in the query. In such situations we
2362  * should not ignore the update, but it is equally unsafe to
2363  * proceed. We don't want to discard the original UPDATE
2364  * while keeping the triggered actions based on it; and we
2365  * have no principled way to merge this update with the
2366  * previous ones. So throwing an error is the only safe
2367  * course.
2368  *
2369  * If a trigger actually intends this type of interaction, it
2370  * can re-execute the UPDATE (assuming it can figure out how)
2371  * and then return NULL to cancel the outer update.
2372  */
2373  if (context->tmfd.cmax != estate->es_output_cid)
2374  ereport(ERROR,
2375  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
2376  errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
2377  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
2378 
2379  /* Else, already updated by self; nothing to do */
2380  return NULL;
2381 
2382  case TM_Ok:
2383  break;
2384 
2385  case TM_Updated:
2386  {
2387  TupleTableSlot *inputslot;
2388  TupleTableSlot *epqslot;
2389  TupleTableSlot *oldSlot;
2390 
2392  ereport(ERROR,
2394  errmsg("could not serialize access due to concurrent update")));
2395 
2396  /*
2397  * Already know that we're going to need to do EPQ, so
2398  * fetch tuple directly into the right slot.
2399  */
2400  inputslot = EvalPlanQualSlot(context->epqstate, resultRelationDesc,
2401  resultRelInfo->ri_RangeTableIndex);
2402 
2403  result = table_tuple_lock(resultRelationDesc, tupleid,
2404  estate->es_snapshot,
2405  inputslot, estate->es_output_cid,
2406  updateCxt.lockmode, LockWaitBlock,
2408  &context->tmfd);
2409 
2410  switch (result)
2411  {
2412  case TM_Ok:
2413  Assert(context->tmfd.traversed);
2414 
2415  epqslot = EvalPlanQual(context->epqstate,
2416  resultRelationDesc,
2417  resultRelInfo->ri_RangeTableIndex,
2418  inputslot);
2419  if (TupIsNull(epqslot))
2420  /* Tuple not passing quals anymore, exiting... */
2421  return NULL;
2422 
2423  /* Make sure ri_oldTupleSlot is initialized. */
2424  if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
2426  resultRelInfo);
2427 
2428  /* Fetch the most recent version of old tuple. */
2429  oldSlot = resultRelInfo->ri_oldTupleSlot;
2430  if (!table_tuple_fetch_row_version(resultRelationDesc,
2431  tupleid,
2432  SnapshotAny,
2433  oldSlot))
2434  elog(ERROR, "failed to fetch tuple being updated");
2435  slot = ExecGetUpdateNewTuple(resultRelInfo,
2436  epqslot, oldSlot);
2437  goto redo_act;
2438 
2439  case TM_Deleted:
2440  /* tuple already deleted; nothing to do */
2441  return NULL;
2442 
2443  case TM_SelfModified:
2444 
2445  /*
2446  * This can be reached when following an update
2447  * chain from a tuple updated by another session,
2448  * reaching a tuple that was already updated in
2449  * this transaction. If previously modified by
2450  * this command, ignore the redundant update,
2451  * otherwise error out.
2452  *
2453  * See also TM_SelfModified response to
2454  * table_tuple_update() above.
2455  */
2456  if (context->tmfd.cmax != estate->es_output_cid)
2457  ereport(ERROR,
2458  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
2459  errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
2460  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
2461  return NULL;
2462 
2463  default:
2464  /* see table_tuple_lock call in ExecDelete() */
2465  elog(ERROR, "unexpected table_tuple_lock status: %u",
2466  result);
2467  return NULL;
2468  }
2469  }
2470 
2471  break;
2472 
2473  case TM_Deleted:
2475  ereport(ERROR,
2477  errmsg("could not serialize access due to concurrent delete")));
2478  /* tuple already deleted; nothing to do */
2479  return NULL;
2480 
2481  default:
2482  elog(ERROR, "unrecognized table_tuple_update status: %u",
2483  result);
2484  return NULL;
2485  }
2486  }
2487 
2488  if (canSetTag)
2489  (estate->es_processed)++;
2490 
2491  ExecUpdateEpilogue(context, &updateCxt, resultRelInfo, tupleid, oldtuple,
2492  slot);
2493 
2494  /* Process RETURNING if present */
2495  if (resultRelInfo->ri_projectReturning)
2496  return ExecProcessReturning(resultRelInfo, slot, context->planSlot);
2497 
2498  return NULL;
2499 }
2500 
2501 /*
2502  * ExecOnConflictUpdate --- execute UPDATE of INSERT ON CONFLICT DO UPDATE
2503  *
2504  * Try to lock tuple for update as part of speculative insertion. If
2505  * a qual originating from ON CONFLICT DO UPDATE is satisfied, update
2506  * (but still lock row, even though it may not satisfy estate's
2507  * snapshot).
2508  *
2509  * Returns true if we're done (with or without an update), or false if
2510  * the caller must retry the INSERT from scratch.
2511  */
2512 static bool
2514  ResultRelInfo *resultRelInfo,
2515  ItemPointer conflictTid,
2516  TupleTableSlot *excludedSlot,
2517  bool canSetTag,
2518  TupleTableSlot **returning)
2519 {
2520  ModifyTableState *mtstate = context->mtstate;
2521  ExprContext *econtext = mtstate->ps.ps_ExprContext;
2522  Relation relation = resultRelInfo->ri_RelationDesc;
2523  ExprState *onConflictSetWhere = resultRelInfo->ri_onConflict->oc_WhereClause;
2524  TupleTableSlot *existing = resultRelInfo->ri_onConflict->oc_Existing;
2525  TM_FailureData tmfd;
2526  LockTupleMode lockmode;
2527  TM_Result test;
2528  Datum xminDatum;
2529  TransactionId xmin;
2530  bool isnull;
2531 
2532  /* Determine lock mode to use */
2533  lockmode = ExecUpdateLockMode(context->estate, resultRelInfo);
2534 
2535  /*
2536  * Lock tuple for update. Don't follow updates when tuple cannot be
2537  * locked without doing so. A row locking conflict here means our
2538  * previous conclusion that the tuple is conclusively committed is not
2539  * true anymore.
2540  */
2541  test = table_tuple_lock(relation, conflictTid,
2542  context->estate->es_snapshot,
2543  existing, context->estate->es_output_cid,
2544  lockmode, LockWaitBlock, 0,
2545  &tmfd);
2546  switch (test)
2547  {
2548  case TM_Ok:
2549  /* success! */
2550  break;
2551 
2552  case TM_Invisible:
2553 
2554  /*
2555  * This can occur when a just inserted tuple is updated again in
2556  * the same command. E.g. because multiple rows with the same
2557  * conflicting key values are inserted.
2558  *
2559  * This is somewhat similar to the ExecUpdate() TM_SelfModified
2560  * case. We do not want to proceed because it would lead to the
2561  * same row being updated a second time in some unspecified order,
2562  * and in contrast to plain UPDATEs there's no historical behavior
2563  * to break.
2564  *
2565  * It is the user's responsibility to prevent this situation from
2566  * occurring. These problems are why the SQL standard similarly
2567  * specifies that for SQL MERGE, an exception must be raised in
2568  * the event of an attempt to update the same row twice.
2569  */
2570  xminDatum = slot_getsysattr(existing,
2572  &isnull);
2573  Assert(!isnull);
2574  xmin = DatumGetTransactionId(xminDatum);
2575 
2577  ereport(ERROR,
2578  (errcode(ERRCODE_CARDINALITY_VIOLATION),
2579  /* translator: %s is a SQL command name */
2580  errmsg("%s command cannot affect row a second time",
2581  "ON CONFLICT DO UPDATE"),
2582  errhint("Ensure that no rows proposed for insertion within the same command have duplicate constrained values.")));
2583 
2584  /* This shouldn't happen */
2585  elog(ERROR, "attempted to lock invisible tuple");
2586  break;
2587 
2588  case TM_SelfModified:
2589 
2590  /*
2591  * This state should never be reached. As a dirty snapshot is used
2592  * to find conflicting tuples, speculative insertion wouldn't have
2593  * seen this row to conflict with.
2594  */
2595  elog(ERROR, "unexpected self-updated tuple");
2596  break;
2597 
2598  case TM_Updated:
2600  ereport(ERROR,
2602  errmsg("could not serialize access due to concurrent update")));
2603 
2604  /*
2605  * As long as we don't support an UPDATE of INSERT ON CONFLICT for
2606  * a partitioned table we shouldn't reach to a case where tuple to
2607  * be lock is moved to another partition due to concurrent update
2608  * of the partition key.
2609  */
2611 
2612  /*
2613  * Tell caller to try again from the very start.
2614  *
2615  * It does not make sense to use the usual EvalPlanQual() style
2616  * loop here, as the new version of the row might not conflict
2617  * anymore, or the conflicting tuple has actually been deleted.
2618  */
2619  ExecClearTuple(existing);
2620  return false;
2621 
2622  case TM_Deleted:
2624  ereport(ERROR,
2626  errmsg("could not serialize access due to concurrent delete")));
2627 
2628  /* see TM_Updated case */
2630  ExecClearTuple(existing);
2631  return false;
2632 
2633  default:
2634  elog(ERROR, "unrecognized table_tuple_lock status: %u", test);
2635  }
2636 
2637  /* Success, the tuple is locked. */
2638 
2639  /*
2640  * Verify that the tuple is visible to our MVCC snapshot if the current
2641  * isolation level mandates that.
2642  *
2643  * It's not sufficient to rely on the check within ExecUpdate() as e.g.
2644  * CONFLICT ... WHERE clause may prevent us from reaching that.
2645  *
2646  * This means we only ever continue when a new command in the current
2647  * transaction could see the row, even though in READ COMMITTED mode the
2648  * tuple will not be visible according to the current statement's
2649  * snapshot. This is in line with the way UPDATE deals with newer tuple
2650  * versions.
2651  */
2652  ExecCheckTupleVisible(context->estate, relation, existing);
2653 
2654  /*
2655  * Make tuple and any needed join variables available to ExecQual and
2656  * ExecProject. The EXCLUDED tuple is installed in ecxt_innertuple, while
2657  * the target's existing tuple is installed in the scantuple. EXCLUDED
2658  * has been made to reference INNER_VAR in setrefs.c, but there is no
2659  * other redirection.
2660  */
2661  econtext->ecxt_scantuple = existing;
2662  econtext->ecxt_innertuple = excludedSlot;
2663  econtext->ecxt_outertuple = NULL;
2664 
2665  if (!ExecQual(onConflictSetWhere, econtext))
2666  {
2667  ExecClearTuple(existing); /* see return below */
2668  InstrCountFiltered1(&mtstate->ps, 1);
2669  return true; /* done with the tuple */
2670  }
2671 
2672  if (resultRelInfo->ri_WithCheckOptions != NIL)
2673  {
2674  /*
2675  * Check target's existing tuple against UPDATE-applicable USING
2676  * security barrier quals (if any), enforced here as RLS checks/WCOs.
2677  *
2678  * The rewriter creates UPDATE RLS checks/WCOs for UPDATE security
2679  * quals, and stores them as WCOs of "kind" WCO_RLS_CONFLICT_CHECK,
2680  * but that's almost the extent of its special handling for ON
2681  * CONFLICT DO UPDATE.
2682  *
2683  * The rewriter will also have associated UPDATE applicable straight
2684  * RLS checks/WCOs for the benefit of the ExecUpdate() call that
2685  * follows. INSERTs and UPDATEs naturally have mutually exclusive WCO
2686  * kinds, so there is no danger of spurious over-enforcement in the
2687  * INSERT or UPDATE path.
2688  */
2690  existing,
2691  mtstate->ps.state);
2692  }
2693 
2694  /* Project the new tuple version */
2695  ExecProject(resultRelInfo->ri_onConflict->oc_ProjInfo);
2696 
2697  /*
2698  * Note that it is possible that the target tuple has been modified in
2699  * this session, after the above table_tuple_lock. We choose to not error
2700  * out in that case, in line with ExecUpdate's treatment of similar cases.
2701  * This can happen if an UPDATE is triggered from within ExecQual(),
2702  * ExecWithCheckOptions() or ExecProject() above, e.g. by selecting from a
2703  * wCTE in the ON CONFLICT's SET.
2704  */
2705 
2706  /* Execute UPDATE with projection */
2707  *returning = ExecUpdate(context, resultRelInfo,
2708  conflictTid, NULL,
2709  resultRelInfo->ri_onConflict->oc_ProjSlot,
2710  canSetTag);
2711 
2712  /*
2713  * Clear out existing tuple, as there might not be another conflict among
2714  * the next input rows. Don't want to hold resources till the end of the
2715  * query.
2716  */
2717  ExecClearTuple(existing);
2718  return true;
2719 }
2720 
2721 /*
2722  * Perform MERGE.
2723  */
2724 static TupleTableSlot *
2726  ItemPointer tupleid, HeapTuple oldtuple, bool canSetTag)
2727 {
2728  TupleTableSlot *rslot = NULL;
2729  bool matched;
2730 
2731  /*-----
2732  * If we are dealing with a WHEN MATCHED case, tupleid or oldtuple is
2733  * valid, depending on whether the result relation is a table or a view.
2734  * We execute the first action for which the additional WHEN MATCHED AND
2735  * quals pass. If an action without quals is found, that action is
2736  * executed.
2737  *
2738  * Similarly, in the WHEN NOT MATCHED BY SOURCE case, tupleid or oldtuple
2739  * is valid, and we look at the given WHEN NOT MATCHED BY SOURCE actions
2740  * in sequence until one passes. This is almost identical to the WHEN
2741  * MATCHED case, and both cases are handled by ExecMergeMatched().
2742  *
2743  * Finally, in the WHEN NOT MATCHED [BY TARGET] case, both tupleid and
2744  * oldtuple are invalid, and we look at the given WHEN NOT MATCHED [BY
2745  * TARGET] actions in sequence until one passes.
2746  *
2747  * Things get interesting in case of concurrent update/delete of the
2748  * target tuple. Such concurrent update/delete is detected while we are
2749  * executing a WHEN MATCHED or WHEN NOT MATCHED BY SOURCE action.
2750  *
2751  * A concurrent update can:
2752  *
2753  * 1. modify the target tuple so that the results from checking any
2754  * additional quals attached to WHEN MATCHED or WHEN NOT MATCHED BY
2755  * SOURCE actions potentially change, but the result from the join
2756  * quals does not change.
2757  *
2758  * In this case, we are still dealing with the same kind of match
2759  * (MATCHED or NOT MATCHED BY SOURCE). We recheck the same list of
2760  * actions from the start and choose the first one that satisfies the
2761  * new target tuple.
2762  *
2763  * 2. modify the target tuple in the WHEN MATCHED case so that the join
2764  * quals no longer pass and hence the source and target tuples no
2765  * longer match.
2766  *
2767  * In this case, we are now dealing with a NOT MATCHED case, and we
2768  * process both WHEN NOT MATCHED BY SOURCE and WHEN NOT MATCHED [BY
2769  * TARGET] actions. First ExecMergeMatched() processes the list of
2770  * WHEN NOT MATCHED BY SOURCE actions in sequence until one passes,
2771  * then ExecMergeNotMatched() processes any WHEN NOT MATCHED [BY
2772  * TARGET] actions in sequence until one passes. Thus we may execute
2773  * two actions; one of each kind.
2774  *
2775  * Thus we support concurrent updates that turn MATCHED candidate rows
2776  * into NOT MATCHED rows. However, we do not attempt to support cases
2777  * that would turn NOT MATCHED rows into MATCHED rows, or which would
2778  * cause a target row to match a different source row.
2779  *
2780  * A concurrent delete changes a WHEN MATCHED case to WHEN NOT MATCHED
2781  * [BY TARGET].
2782  *
2783  * ExecMergeMatched() takes care of following the update chain and
2784  * re-finding the qualifying WHEN MATCHED or WHEN NOT MATCHED BY SOURCE
2785  * action, as long as the target tuple still exists. If the target tuple
2786  * gets deleted or a concurrent update causes the join quals to fail, it
2787  * returns a matched status of false and we call ExecMergeNotMatched().
2788  * Given that ExecMergeMatched() always makes progress by following the
2789  * update chain and we never switch from ExecMergeNotMatched() to
2790  * ExecMergeMatched(), there is no risk of a livelock.
2791  */
2792  matched = tupleid != NULL || oldtuple != NULL;
2793  if (matched)
2794  rslot = ExecMergeMatched(context, resultRelInfo, tupleid, oldtuple,
2795  canSetTag, &matched);
2796 
2797  /*
2798  * Deal with the NOT MATCHED case (either a NOT MATCHED tuple from the
2799  * join, or a previously MATCHED tuple for which ExecMergeMatched() set
2800  * "matched" to false, indicating that it no longer matches).
2801  */
2802  if (!matched)
2803  {
2804  /*
2805  * If a concurrent update turned a MATCHED case into a NOT MATCHED
2806  * case, and we have both WHEN NOT MATCHED BY SOURCE and WHEN NOT
2807  * MATCHED [BY TARGET] actions, and there is a RETURNING clause,
2808  * ExecMergeMatched() may have already executed a WHEN NOT MATCHED BY
2809  * SOURCE action, and computed the row to return. If so, we cannot
2810  * execute a WHEN NOT MATCHED [BY TARGET] action now, so mark it as
2811  * pending (to be processed on the next call to ExecModifyTable()).
2812  * Otherwise, just process the action now.
2813  */
2814  if (rslot == NULL)
2815  rslot = ExecMergeNotMatched(context, resultRelInfo, canSetTag);
2816  else
2817  context->mtstate->mt_merge_pending_not_matched = context->planSlot;
2818  }
2819 
2820  return rslot;
2821 }
2822 
2823 /*
2824  * Check and execute the first qualifying MATCHED or NOT MATCHED BY SOURCE
2825  * action, depending on whether the join quals are satisfied. If the target
2826  * relation is a table, the current target tuple is identified by tupleid.
2827  * Otherwise, if the target relation is a view, oldtuple is the current target
2828  * tuple from the view.
2829  *
2830  * We start from the first WHEN MATCHED or WHEN NOT MATCHED BY SOURCE action
2831  * and check if the WHEN quals pass, if any. If the WHEN quals for the first
2832  * action do not pass, we check the second, then the third and so on. If we
2833  * reach the end without finding a qualifying action, we return NULL.
2834  * Otherwise, we execute the qualifying action and return its RETURNING
2835  * result, if any, or NULL.
2836  *
2837  * On entry, "*matched" is assumed to be true. If a concurrent update or
2838  * delete is detected that causes the join quals to no longer pass, we set it
2839  * to false, indicating that the caller should process any NOT MATCHED [BY
2840  * TARGET] actions.
2841  *
2842  * After a concurrent update, we restart from the first action to look for a
2843  * new qualifying action to execute. If the join quals originally passed, and
2844  * the concurrent update caused them to no longer pass, then we switch from
2845  * the MATCHED to the NOT MATCHED BY SOURCE list of actions before restarting
2846  * (and setting "*matched" to false). As a result we may execute a WHEN NOT
2847  * MATCHED BY SOURCE action, and set "*matched" to false, causing the caller
2848  * to also execute a WHEN NOT MATCHED [BY TARGET] action.
2849  */
2850 static TupleTableSlot *
2852  ItemPointer tupleid, HeapTuple oldtuple, bool canSetTag,
2853  bool *matched)
2854 {
2855  ModifyTableState *mtstate = context->mtstate;
2856  List **mergeActions = resultRelInfo->ri_MergeActions;
2857  List *actionStates;
2858  TupleTableSlot *newslot = NULL;
2859  TupleTableSlot *rslot = NULL;
2860  EState *estate = context->estate;
2861  ExprContext *econtext = mtstate->ps.ps_ExprContext;
2862  bool isNull;
2863  EPQState *epqstate = &mtstate->mt_epqstate;
2864  ListCell *l;
2865 
2866  /* Expect matched to be true on entry */
2867  Assert(*matched);
2868 
2869  /*
2870  * If there are no WHEN MATCHED or WHEN NOT MATCHED BY SOURCE actions, we
2871  * are done.
2872  */
2873  if (mergeActions[MERGE_WHEN_MATCHED] == NIL &&
2874  mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] == NIL)
2875  return NULL;
2876 
2877  /*
2878  * Make tuple and any needed join variables available to ExecQual and
2879  * ExecProject. The target's existing tuple is installed in the scantuple.
2880  * This target relation's slot is required only in the case of a MATCHED
2881  * or NOT MATCHED BY SOURCE tuple and UPDATE/DELETE actions.
2882  */
2883  econtext->ecxt_scantuple = resultRelInfo->ri_oldTupleSlot;
2884  econtext->ecxt_innertuple = context->planSlot;
2885  econtext->ecxt_outertuple = NULL;
2886 
2887  /*
2888  * This routine is only invoked for matched target rows, so we should
2889  * either have the tupleid of the target row, or an old tuple from the
2890  * target wholerow junk attr.
2891  */
2892  Assert(tupleid != NULL || oldtuple != NULL);
2893  if (oldtuple != NULL)
2894  ExecForceStoreHeapTuple(oldtuple, resultRelInfo->ri_oldTupleSlot,
2895  false);
2896  else if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc,
2897  tupleid,
2898  SnapshotAny,
2899  resultRelInfo->ri_oldTupleSlot))
2900  elog(ERROR, "failed to fetch the target tuple");
2901 
2902  /*
2903  * Test the join condition. If it's satisfied, perform a MATCHED action.
2904  * Otherwise, perform a NOT MATCHED BY SOURCE action.
2905  *
2906  * Note that this join condition will be NULL if there are no NOT MATCHED
2907  * BY SOURCE actions --- see transform_MERGE_to_join(). In that case, we
2908  * need only consider MATCHED actions here.
2909  */
2910  if (ExecQual(resultRelInfo->ri_MergeJoinCondition, econtext))
2911  actionStates = mergeActions[MERGE_WHEN_MATCHED];
2912  else
2913  actionStates = mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE];
2914 
2915 lmerge_matched:
2916 
2917  foreach(l, actionStates)
2918  {
2919  MergeActionState *relaction = (MergeActionState *) lfirst(l);
2920  CmdType commandType = relaction->mas_action->commandType;
2921  TM_Result result;
2922  UpdateContext updateCxt = {0};
2923 
2924  /*
2925  * Test condition, if any.
2926  *
2927  * In the absence of any condition, we perform the action
2928  * unconditionally (no need to check separately since ExecQual() will
2929  * return true if there are no conditions to evaluate).
2930  */
2931  if (!ExecQual(relaction->mas_whenqual, econtext))
2932  continue;
2933 
2934  /*
2935  * Check if the existing target tuple meets the USING checks of
2936  * UPDATE/DELETE RLS policies. If those checks fail, we throw an
2937  * error.
2938  *
2939  * The WITH CHECK quals for UPDATE RLS policies are applied in
2940  * ExecUpdateAct() and hence we need not do anything special to handle
2941  * them.
2942  *
2943  * NOTE: We must do this after WHEN quals are evaluated, so that we
2944  * check policies only when they matter.
2945  */
2946  if (resultRelInfo->ri_WithCheckOptions && commandType != CMD_NOTHING)
2947  {
2948  ExecWithCheckOptions(commandType == CMD_UPDATE ?
2950  resultRelInfo,
2951  resultRelInfo->ri_oldTupleSlot,
2952  context->mtstate->ps.state);
2953  }
2954 
2955  /* Perform stated action */
2956  switch (commandType)
2957  {
2958  case CMD_UPDATE:
2959 
2960  /*
2961  * Project the output tuple, and use that to update the table.
2962  * We don't need to filter out junk attributes, because the
2963  * UPDATE action's targetlist doesn't have any.
2964  */
2965  newslot = ExecProject(relaction->mas_proj);
2966 
2967  mtstate->mt_merge_action = relaction;
2968  if (!ExecUpdatePrologue(context, resultRelInfo,
2969  tupleid, NULL, newslot, &result))
2970  {
2971  if (result == TM_Ok)
2972  return NULL; /* "do nothing" */
2973 
2974  break; /* concurrent update/delete */
2975  }
2976 
2977  /* INSTEAD OF ROW UPDATE Triggers */
2978  if (resultRelInfo->ri_TrigDesc &&
2979  resultRelInfo->ri_TrigDesc->trig_update_instead_row)
2980  {
2981  if (!ExecIRUpdateTriggers(estate, resultRelInfo,
2982  oldtuple, newslot))
2983  return NULL; /* "do nothing" */
2984  }
2985  else
2986  {
2987  /* called table_tuple_fetch_row_version() above */
2988  Assert(oldtuple == NULL);
2989 
2990  result = ExecUpdateAct(context, resultRelInfo, tupleid,
2991  NULL, newslot, canSetTag,
2992  &updateCxt);
2993 
2994  /*
2995  * As in ExecUpdate(), if ExecUpdateAct() reports that a
2996  * cross-partition update was done, then there's nothing
2997  * else for us to do --- the UPDATE has been turned into a
2998  * DELETE and an INSERT, and we must not perform any of
2999  * the usual post-update tasks. Also, the RETURNING tuple
3000  * (if any) has been projected, so we can just return
3001  * that.
3002  */
3003  if (updateCxt.crossPartUpdate)
3004  {
3005  mtstate->mt_merge_updated += 1;
3006  return context->cpUpdateReturningSlot;
3007  }
3008  }
3009 
3010  if (result == TM_Ok)
3011  {
3012  ExecUpdateEpilogue(context, &updateCxt, resultRelInfo,
3013  tupleid, NULL, newslot);
3014  mtstate->mt_merge_updated += 1;
3015  }
3016  break;
3017 
3018  case CMD_DELETE:
3019  mtstate->mt_merge_action = relaction;
3020  if (!ExecDeletePrologue(context, resultRelInfo, tupleid,
3021  NULL, NULL, &result))
3022  {
3023  if (result == TM_Ok)
3024  return NULL; /* "do nothing" */
3025 
3026  break; /* concurrent update/delete */
3027  }
3028 
3029  /* INSTEAD OF ROW DELETE Triggers */
3030  if (resultRelInfo->ri_TrigDesc &&
3031  resultRelInfo->ri_TrigDesc->trig_delete_instead_row)
3032  {
3033  if (!ExecIRDeleteTriggers(estate, resultRelInfo,
3034  oldtuple))
3035  return NULL; /* "do nothing" */
3036  }
3037  else
3038  {
3039  /* called table_tuple_fetch_row_version() above */
3040  Assert(oldtuple == NULL);
3041 
3042  result = ExecDeleteAct(context, resultRelInfo, tupleid,
3043  false);
3044  }
3045 
3046  if (result == TM_Ok)
3047  {
3048  ExecDeleteEpilogue(context, resultRelInfo, tupleid, NULL,
3049  false);
3050  mtstate->mt_merge_deleted += 1;
3051  }
3052  break;
3053 
3054  case CMD_NOTHING:
3055  /* Doing nothing is always OK */
3056  result = TM_Ok;
3057  break;
3058 
3059  default:
3060  elog(ERROR, "unknown action in MERGE WHEN clause");
3061  }
3062 
3063  switch (result)
3064  {
3065  case TM_Ok:
3066  /* all good; perform final actions */
3067  if (canSetTag && commandType != CMD_NOTHING)
3068  (estate->es_processed)++;
3069 
3070  break;
3071 
3072  case TM_SelfModified:
3073 
3074  /*
3075  * The target tuple was already updated or deleted by the
3076  * current command, or by a later command in the current
3077  * transaction. The former case is explicitly disallowed by
3078  * the SQL standard for MERGE, which insists that the MERGE
3079  * join condition should not join a target row to more than
3080  * one source row.
3081  *
3082  * The latter case arises if the tuple is modified by a
3083  * command in a BEFORE trigger, or perhaps by a command in a
3084  * volatile function used in the query. In such situations we
3085  * should not ignore the MERGE action, but it is equally
3086  * unsafe to proceed. We don't want to discard the original
3087  * MERGE action while keeping the triggered actions based on
3088  * it; and it would be no better to allow the original MERGE
3089  * action while discarding the updates that it triggered. So
3090  * throwing an error is the only safe course.
3091  */
3092  if (context->tmfd.cmax != estate->es_output_cid)
3093  ereport(ERROR,
3094  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
3095  errmsg("tuple to be updated or deleted was already modified by an operation triggered by the current command"),
3096  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
3097 
3099  ereport(ERROR,
3100  (errcode(ERRCODE_CARDINALITY_VIOLATION),
3101  /* translator: %s is a SQL command name */
3102  errmsg("%s command cannot affect row a second time",
3103  "MERGE"),
3104  errhint("Ensure that not more than one source row matches any one target row.")));
3105 
3106  /* This shouldn't happen */
3107  elog(ERROR, "attempted to update or delete invisible tuple");
3108  break;
3109 
3110  case TM_Deleted:
3112  ereport(ERROR,
3114  errmsg("could not serialize access due to concurrent delete")));
3115 
3116  /*
3117  * If the tuple was already deleted, set matched to false to
3118  * let caller handle it under NOT MATCHED [BY TARGET] clauses.
3119  */
3120  *matched = false;
3121  return NULL;
3122 
3123  case TM_Updated:
3124  {
3125  bool was_matched;
3126  Relation resultRelationDesc;
3127  TupleTableSlot *epqslot,
3128  *inputslot;
3129  LockTupleMode lockmode;
3130 
3131  /*
3132  * The target tuple was concurrently updated by some other
3133  * transaction. If we are currently processing a MATCHED
3134  * action, use EvalPlanQual() with the new version of the
3135  * tuple and recheck the join qual, to detect a change
3136  * from the MATCHED to the NOT MATCHED cases. If we are
3137  * already processing a NOT MATCHED BY SOURCE action, we
3138  * skip this (cannot switch from NOT MATCHED BY SOURCE to
3139  * MATCHED).
3140  */
3141  was_matched = relaction->mas_action->matchKind == MERGE_WHEN_MATCHED;
3142  resultRelationDesc = resultRelInfo->ri_RelationDesc;
3143  lockmode = ExecUpdateLockMode(estate, resultRelInfo);
3144 
3145  if (was_matched)
3146  inputslot = EvalPlanQualSlot(epqstate, resultRelationDesc,
3147  resultRelInfo->ri_RangeTableIndex);
3148  else
3149  inputslot = resultRelInfo->ri_oldTupleSlot;
3150 
3151  result = table_tuple_lock(resultRelationDesc, tupleid,
3152  estate->es_snapshot,
3153  inputslot, estate->es_output_cid,
3154  lockmode, LockWaitBlock,
3156  &context->tmfd);
3157  switch (result)
3158  {
3159  case TM_Ok:
3160 
3161  /*
3162  * If the tuple was updated and migrated to
3163  * another partition concurrently, the current
3164  * MERGE implementation can't follow. There's
3165  * probably a better way to handle this case, but
3166  * it'd require recognizing the relation to which
3167  * the tuple moved, and setting our current
3168  * resultRelInfo to that.
3169  */
3171  ereport(ERROR,
3173  errmsg("tuple to be merged was already moved to another partition due to concurrent update")));
3174 
3175  /*
3176  * If this was a MATCHED case, use EvalPlanQual()
3177  * to recheck the join condition.
3178  */
3179  if (was_matched)
3180  {
3181  epqslot = EvalPlanQual(epqstate,
3182  resultRelationDesc,
3183  resultRelInfo->ri_RangeTableIndex,
3184  inputslot);
3185 
3186  /*
3187  * If the subplan didn't return a tuple, then
3188  * we must be dealing with an inner join for
3189  * which the join condition no longer matches.
3190  * This can only happen if there are no NOT
3191  * MATCHED actions, and so there is nothing
3192  * more to do.
3193  */
3194  if (TupIsNull(epqslot))
3195  return NULL;
3196 
3197  /*
3198  * If we got a NULL ctid from the subplan, the
3199  * join quals no longer pass and we switch to
3200  * the NOT MATCHED BY SOURCE case.
3201  */
3202  (void) ExecGetJunkAttribute(epqslot,
3203  resultRelInfo->ri_RowIdAttNo,
3204  &isNull);
3205  if (isNull)
3206  *matched = false;
3207 
3208  /*
3209  * Otherwise, recheck the join quals to see if
3210  * we need to switch to the NOT MATCHED BY
3211  * SOURCE case.
3212  */
3213  if (!table_tuple_fetch_row_version(resultRelationDesc,
3214  &context->tmfd.ctid,
3215  SnapshotAny,
3216  resultRelInfo->ri_oldTupleSlot))
3217  elog(ERROR, "failed to fetch the target tuple");
3218 
3219  if (*matched)
3220  *matched = ExecQual(resultRelInfo->ri_MergeJoinCondition,
3221  econtext);
3222 
3223  /* Switch lists, if necessary */
3224  if (!*matched)
3225  actionStates = mergeActions[MERGE_WHEN_NOT_MATCHED_BY_SOURCE];
3226  }
3227 
3228  /*
3229  * Loop back and process the MATCHED or NOT
3230  * MATCHED BY SOURCE actions from the start.
3231  */
3232  goto lmerge_matched;
3233 
3234  case TM_Deleted:
3235 
3236  /*
3237  * tuple already deleted; tell caller to run NOT
3238  * MATCHED [BY TARGET] actions
3239  */
3240  *matched = false;
3241  return NULL;
3242 
3243  case TM_SelfModified:
3244 
3245  /*
3246  * This can be reached when following an update
3247  * chain from a tuple updated by another session,
3248  * reaching a tuple that was already updated or
3249  * deleted by the current command, or by a later
3250  * command in the current transaction. As above,
3251  * this should always be treated as an error.
3252  */
3253  if (context->tmfd.cmax != estate->es_output_cid)
3254  ereport(ERROR,
3255  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
3256  errmsg("tuple to be updated or deleted was already modified by an operation triggered by the current command"),
3257  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
3258 
3260  ereport(ERROR,
3261  (errcode(ERRCODE_CARDINALITY_VIOLATION),
3262  /* translator: %s is a SQL command name */
3263  errmsg("%s command cannot affect row a second time",
3264  "MERGE"),
3265  errhint("Ensure that not more than one source row matches any one target row.")));
3266 
3267  /* This shouldn't happen */
3268  elog(ERROR, "attempted to update or delete invisible tuple");
3269  return NULL;
3270 
3271  default:
3272  /* see table_tuple_lock call in ExecDelete() */
3273  elog(ERROR, "unexpected table_tuple_lock status: %u",
3274  result);
3275  return NULL;
3276  }
3277  }
3278 
3279  case TM_Invisible:
3280  case TM_WouldBlock:
3281  case TM_BeingModified:
3282  /* these should not occur */
3283  elog(ERROR, "unexpected tuple operation result: %d", result);
3284  break;
3285  }
3286 
3287  /* Process RETURNING if present */
3288  if (resultRelInfo->ri_projectReturning)
3289  {
3290  switch (commandType)
3291  {
3292  case CMD_UPDATE:
3293  rslot = ExecProcessReturning(resultRelInfo, newslot,
3294  context->planSlot);
3295  break;
3296 
3297  case CMD_DELETE:
3298  rslot = ExecProcessReturning(resultRelInfo,
3299  resultRelInfo->ri_oldTupleSlot,
3300  context->planSlot);
3301  break;
3302 
3303  case CMD_NOTHING:
3304  break;
3305 
3306  default:
3307  elog(ERROR, "unrecognized commandType: %d",
3308  (int) commandType);
3309  }
3310  }
3311 
3312  /*
3313  * We've activated one of the WHEN clauses, so we don't search
3314  * further. This is required behaviour, not an optimization.
3315  */
3316  break;
3317  }
3318 
3319  /*
3320  * Successfully executed an action or no qualifying action was found.
3321  */
3322  return rslot;
3323 }
3324 
3325 /*
3326  * Execute the first qualifying NOT MATCHED [BY TARGET] action.
3327  */
3328 static TupleTableSlot *
3330  bool canSetTag)
3331 {
3332  ModifyTableState *mtstate = context->mtstate;
3333  ExprContext *econtext = mtstate->ps.ps_ExprContext;
3334  List *actionStates;
3335  TupleTableSlot *rslot = NULL;
3336  ListCell *l;
3337 
3338  /*
3339  * For INSERT actions, the root relation's merge action is OK since the
3340  * INSERT's targetlist and the WHEN conditions can only refer to the
3341  * source relation and hence it does not matter which result relation we
3342  * work with.
3343  *
3344  * XXX does this mean that we can avoid creating copies of actionStates on
3345  * partitioned tables, for not-matched actions?
3346  */
3347  actionStates = resultRelInfo->ri_MergeActions[MERGE_WHEN_NOT_MATCHED_BY_TARGET];
3348 
3349  /*
3350  * Make source tuple available to ExecQual and ExecProject. We don't need
3351  * the target tuple, since the WHEN quals and targetlist can't refer to
3352  * the target columns.
3353  */
3354  econtext->ecxt_scantuple = NULL;
3355  econtext->ecxt_innertuple = context->planSlot;
3356  econtext->ecxt_outertuple = NULL;
3357 
3358  foreach(l, actionStates)
3359  {
3361  CmdType commandType = action->mas_action->commandType;
3362  TupleTableSlot *newslot;
3363 
3364  /*
3365  * Test condition, if any.
3366  *
3367  * In the absence of any condition, we perform the action
3368  * unconditionally (no need to check separately since ExecQual() will
3369  * return true if there are no conditions to evaluate).
3370  */
3371  if (!ExecQual(action->mas_whenqual, econtext))
3372  continue;
3373 
3374  /* Perform stated action */
3375  switch (commandType)
3376  {
3377  case CMD_INSERT:
3378 
3379  /*
3380  * Project the tuple. In case of a partitioned table, the
3381  * projection was already built to use the root's descriptor,
3382  * so we don't need to map the tuple here.
3383  */
3384  newslot = ExecProject(action->mas_proj);
3385  mtstate->mt_merge_action = action;
3386 
3387  rslot = ExecInsert(context, mtstate->rootResultRelInfo,
3388  newslot, canSetTag, NULL, NULL);
3389  mtstate->mt_merge_inserted += 1;
3390  break;
3391  case CMD_NOTHING:
3392  /* Do nothing */
3393  break;
3394  default:
3395  elog(ERROR, "unknown action in MERGE WHEN NOT MATCHED clause");
3396  }
3397 
3398  /*
3399  * We've activated one of the WHEN clauses, so we don't search
3400  * further. This is required behaviour, not an optimization.
3401  */
3402  break;
3403  }
3404 
3405  return rslot;
3406 }
3407 
3408 /*
3409  * Initialize state for execution of MERGE.
3410  */
3411 void
3413 {
3414  ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
3415  ResultRelInfo *rootRelInfo = mtstate->rootResultRelInfo;
3416  ResultRelInfo *resultRelInfo;
3417  ExprContext *econtext;
3418  ListCell *lc;
3419  int i;
3420 
3421  if (node->mergeActionLists == NIL)
3422  return;
3423 
3424  mtstate->mt_merge_subcommands = 0;
3425 
3426  if (mtstate->ps.ps_ExprContext == NULL)
3427  ExecAssignExprContext(estate, &mtstate->ps);
3428  econtext = mtstate->ps.ps_ExprContext;
3429 
3430  /*
3431  * Create a MergeActionState for each action on the mergeActionList and
3432  * add it to either a list of matched actions or not-matched actions.
3433  *
3434  * Similar logic appears in ExecInitPartitionInfo(), so if changing
3435  * anything here, do so there too.
3436  */
3437  i = 0;
3438  foreach(lc, node->mergeActionLists)
3439  {
3440  List *mergeActionList = lfirst(lc);
3441  Node *joinCondition;
3442  TupleDesc relationDesc;
3443  ListCell *l;
3444 
3445  joinCondition = (Node *) list_nth(node->mergeJoinConditions, i);
3446  resultRelInfo = mtstate->resultRelInfo + i;
3447  i++;
3448  relationDesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
3449 
3450  /* initialize slots for MERGE fetches from this rel */
3451  if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
3452  ExecInitMergeTupleSlots(mtstate, resultRelInfo);
3453 
3454  /* initialize state for join condition checking */
3455  resultRelInfo->ri_MergeJoinCondition =
3456  ExecInitQual((List *) joinCondition, &mtstate->ps);
3457 
3458  foreach(l, mergeActionList)
3459  {
3461  MergeActionState *action_state;
3462  TupleTableSlot *tgtslot;
3463  TupleDesc tgtdesc;
3464 
3465  /*
3466  * Build action merge state for this rel. (For partitions,
3467  * equivalent code exists in ExecInitPartitionInfo.)
3468  */
3469  action_state = makeNode(MergeActionState);
3470  action_state->mas_action = action;
3471  action_state->mas_whenqual = ExecInitQual((List *) action->qual,
3472  &mtstate->ps);
3473 
3474  /*
3475  * We create three lists - one for each MergeMatchKind - and stick
3476  * the MergeActionState into the appropriate list.
3477  */
3478  resultRelInfo->ri_MergeActions[action->matchKind] =
3479  lappend(resultRelInfo->ri_MergeActions[action->matchKind],
3480  action_state);
3481 
3482  switch (action->commandType)
3483  {
3484  case CMD_INSERT:
3485  ExecCheckPlanOutput(rootRelInfo->ri_RelationDesc,
3486  action->targetList);
3487 
3488  /*
3489  * If the MERGE targets a partitioned table, any INSERT
3490  * actions must be routed through it, not the child
3491  * relations. Initialize the routing struct and the root
3492  * table's "new" tuple slot for that, if not already done.
3493  * The projection we prepare, for all relations, uses the
3494  * root relation descriptor, and targets the plan's root
3495  * slot. (This is consistent with the fact that we
3496  * checked the plan output to match the root relation,
3497  * above.)
3498  */
3499  if (rootRelInfo->ri_RelationDesc->rd_rel->relkind ==
3500  RELKIND_PARTITIONED_TABLE)
3501  {
3502  if (mtstate->mt_partition_tuple_routing == NULL)
3503  {
3504  /*
3505  * Initialize planstate for routing if not already
3506  * done.
3507  *
3508  * Note that the slot is managed as a standalone
3509  * slot belonging to ModifyTableState, so we pass
3510  * NULL for the 2nd argument.
3511  */
3512  mtstate->mt_root_tuple_slot =
3513  table_slot_create(rootRelInfo->ri_RelationDesc,
3514  NULL);
3515  mtstate->mt_partition_tuple_routing =
3517  rootRelInfo->ri_RelationDesc);
3518  }
3519  tgtslot = mtstate->mt_root_tuple_slot;
3520  tgtdesc = RelationGetDescr(rootRelInfo->ri_RelationDesc);
3521  }
3522  else
3523  {
3524  /* not partitioned? use the stock relation and slot */
3525  tgtslot = resultRelInfo->ri_newTupleSlot;
3526  tgtdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
3527  }
3528 
3529  action_state->mas_proj =
3530  ExecBuildProjectionInfo(action->targetList, econtext,
3531  tgtslot,
3532  &mtstate->ps,
3533  tgtdesc);
3534 
3535  mtstate->mt_merge_subcommands |= MERGE_INSERT;
3536  break;
3537  case CMD_UPDATE:
3538  action_state->mas_proj =
3539  ExecBuildUpdateProjection(action->targetList,
3540  true,
3541  action->updateColnos,
3542  relationDesc,
3543  econtext,
3544  resultRelInfo->ri_newTupleSlot,
3545  &mtstate->ps);
3546  mtstate->mt_merge_subcommands |= MERGE_UPDATE;
3547  break;
3548  case CMD_DELETE:
3549  mtstate->mt_merge_subcommands |= MERGE_DELETE;
3550  break;
3551  case CMD_NOTHING:
3552  break;
3553  default:
3554  elog(ERROR, "unknown operation");
3555  break;
3556  }
3557  }
3558  }
3559 }
3560 
3561 /*
3562  * Initializes the tuple slots in a ResultRelInfo for any MERGE action.
3563  *
3564  * We mark 'projectNewInfoValid' even though the projections themselves
3565  * are not initialized here.
3566  */
3567 void
3569  ResultRelInfo *resultRelInfo)
3570 {
3571  EState *estate = mtstate->ps.state;
3572 
3573  Assert(!resultRelInfo->ri_projectNewInfoValid);
3574 
3575  resultRelInfo->ri_oldTupleSlot =
3576  table_slot_create(resultRelInfo->ri_RelationDesc,
3577  &estate->es_tupleTable);
3578  resultRelInfo->ri_newTupleSlot =
3579  table_slot_create(resultRelInfo->ri_RelationDesc,
3580  &estate->es_tupleTable);
3581  resultRelInfo->ri_projectNewInfoValid = true;
3582 }
3583 
3584 /*
3585  * Process BEFORE EACH STATEMENT triggers
3586  */
3587 static void
3589 {
3590  ModifyTable *plan = (ModifyTable *) node->ps.plan;
3591  ResultRelInfo *resultRelInfo = node->rootResultRelInfo;
3592 
3593  switch (node->operation)
3594  {
3595  case CMD_INSERT:
3596  ExecBSInsertTriggers(node->ps.state, resultRelInfo);
3597  if (plan->onConflictAction == ONCONFLICT_UPDATE)
3599  resultRelInfo);
3600  break;
3601  case CMD_UPDATE:
3602  ExecBSUpdateTriggers(node->ps.state, resultRelInfo);
3603  break;
3604  case CMD_DELETE:
3605  ExecBSDeleteTriggers(node->ps.state, resultRelInfo);
3606  break;
3607  case CMD_MERGE:
3608  if (node->mt_merge_subcommands & MERGE_INSERT)
3609  ExecBSInsertTriggers(node->ps.state, resultRelInfo);
3610  if (node->mt_merge_subcommands & MERGE_UPDATE)
3611  ExecBSUpdateTriggers(node->ps.state, resultRelInfo);
3612  if (node->mt_merge_subcommands & MERGE_DELETE)
3613  ExecBSDeleteTriggers(node->ps.state, resultRelInfo);
3614  break;
3615  default:
3616  elog(ERROR, "unknown operation");
3617  break;
3618  }
3619 }
3620 
3621 /*
3622  * Process AFTER EACH STATEMENT triggers
3623  */
3624 static void
3626 {
3627  ModifyTable *plan = (ModifyTable *) node->ps.plan;
3628  ResultRelInfo *resultRelInfo = node->rootResultRelInfo;
3629 
3630  switch (node->operation)
3631  {
3632  case CMD_INSERT:
3633  if (plan->onConflictAction == ONCONFLICT_UPDATE)
3635  resultRelInfo,
3636  node->mt_oc_transition_capture);
3637  ExecASInsertTriggers(node->ps.state, resultRelInfo,
3638  node->mt_transition_capture);
3639  break;
3640  case CMD_UPDATE:
3641  ExecASUpdateTriggers(node->ps.state, resultRelInfo,
3642  node->mt_transition_capture);
3643  break;
3644  case CMD_DELETE:
3645  ExecASDeleteTriggers(node->ps.state, resultRelInfo,
3646  node->mt_transition_capture);
3647  break;
3648  case CMD_MERGE:
3649  if (node->mt_merge_subcommands & MERGE_DELETE)
3650  ExecASDeleteTriggers(node->ps.state, resultRelInfo,
3651  node->mt_transition_capture);
3652  if (node->mt_merge_subcommands & MERGE_UPDATE)
3653  ExecASUpdateTriggers(node->ps.state, resultRelInfo,
3654  node->mt_transition_capture);
3655  if (node->mt_merge_subcommands & MERGE_INSERT)
3656  ExecASInsertTriggers(node->ps.state, resultRelInfo,
3657  node->mt_transition_capture);
3658  break;
3659  default:
3660  elog(ERROR, "unknown operation");
3661  break;
3662  }
3663 }
3664 
3665 /*
3666  * Set up the state needed for collecting transition tuples for AFTER
3667  * triggers.
3668  */
3669 static void
3671 {
3672  ModifyTable *plan = (ModifyTable *) mtstate->ps.plan;
3673  ResultRelInfo *targetRelInfo = mtstate->rootResultRelInfo;
3674 
3675  /* Check for transition tables on the directly targeted relation. */
3676  mtstate->mt_transition_capture =
3677  MakeTransitionCaptureState(targetRelInfo->ri_TrigDesc,
3678  RelationGetRelid(targetRelInfo->ri_RelationDesc),
3679  mtstate->operation);
3680  if (plan->operation == CMD_INSERT &&
3681  plan->onConflictAction == ONCONFLICT_UPDATE)
3682  mtstate->mt_oc_transition_capture =
3683  MakeTransitionCaptureState(targetRelInfo->ri_TrigDesc,
3684  RelationGetRelid(targetRelInfo->ri_RelationDesc),
3685  CMD_UPDATE);
3686 }
3687 
3688 /*
3689  * ExecPrepareTupleRouting --- prepare for routing one tuple
3690  *
3691  * Determine the partition in which the tuple in slot is to be inserted,
3692  * and return its ResultRelInfo in *partRelInfo. The return value is
3693  * a slot holding the tuple of the partition rowtype.
3694  *
3695  * This also sets the transition table information in mtstate based on the
3696  * selected partition.
3697  */
3698 static TupleTableSlot *
3700  EState *estate,
3701  PartitionTupleRouting *proute,
3702  ResultRelInfo *targetRelInfo,
3703  TupleTableSlot *slot,
3704  ResultRelInfo **partRelInfo)
3705 {
3706  ResultRelInfo *partrel;
3707  TupleConversionMap *map;
3708 
3709  /*
3710  * Lookup the target partition's ResultRelInfo. If ExecFindPartition does
3711  * not find a valid partition for the tuple in 'slot' then an error is
3712  * raised. An error may also be raised if the found partition is not a
3713  * valid target for INSERTs. This is required since a partitioned table
3714  * UPDATE to another partition becomes a DELETE+INSERT.
3715  */
3716  partrel = ExecFindPartition(mtstate, targetRelInfo, proute, slot, estate);
3717 
3718  /*
3719  * If we're capturing transition tuples, we might need to convert from the
3720  * partition rowtype to root partitioned table's rowtype. But if there
3721  * are no BEFORE triggers on the partition that could change the tuple, we
3722  * can just remember the original unconverted tuple to avoid a needless
3723  * round trip conversion.
3724  */
3725  if (mtstate->mt_transition_capture != NULL)
3726  {
3727  bool has_before_insert_row_trig;
3728 
3729  has_before_insert_row_trig = (partrel->ri_TrigDesc &&
3731 
3733  !has_before_insert_row_trig ? slot : NULL;
3734  }
3735 
3736  /*
3737  * Convert the tuple, if necessary.
3738  */
3739  map = ExecGetRootToChildMap(partrel, estate);
3740  if (map != NULL)
3741  {
3742  TupleTableSlot *new_slot = partrel->ri_PartitionTupleSlot;
3743 
3744  slot = execute_attr_map_slot(map->attrMap, slot, new_slot);
3745  }
3746 
3747  *partRelInfo = partrel;
3748  return slot;
3749 }
3750 
3751 /* ----------------------------------------------------------------
3752  * ExecModifyTable
3753  *
3754  * Perform table modifications as required, and return RETURNING results
3755  * if needed.
3756  * ----------------------------------------------------------------
3757  */
3758 static TupleTableSlot *
3760 {
3761  ModifyTableState *node = castNode(ModifyTableState, pstate);
3763  EState *estate = node->ps.state;
3764  CmdType operation = node->operation;
3765  ResultRelInfo *resultRelInfo;
3766  PlanState *subplanstate;
3767  TupleTableSlot *slot;
3768  TupleTableSlot *oldSlot;
3769  ItemPointerData tuple_ctid;
3770  HeapTupleData oldtupdata;
3771  HeapTuple oldtuple;
3772  ItemPointer tupleid;
3773 
3775 
3776  /*
3777  * This should NOT get called during EvalPlanQual; we should have passed a
3778  * subplan tree to EvalPlanQual, instead. Use a runtime test not just
3779  * Assert because this condition is easy to miss in testing. (Note:
3780  * although ModifyTable should not get executed within an EvalPlanQual
3781  * operation, we do have to allow it to be initialized and shut down in
3782  * case it is within a CTE subplan. Hence this test must be here, not in
3783  * ExecInitModifyTable.)
3784  */
3785  if (estate->es_epq_active != NULL)
3786  elog(ERROR, "ModifyTable should not be called during EvalPlanQual");
3787 
3788  /*
3789  * If we've already completed processing, don't try to do more. We need
3790  * this test because ExecPostprocessPlan might call us an extra time, and
3791  * our subplan's nodes aren't necessarily robust against being called
3792  * extra times.
3793  */
3794  if (node->mt_done)
3795  return NULL;
3796 
3797  /*
3798  * On first call, fire BEFORE STATEMENT triggers before proceeding.
3799  */
3800  if (node->fireBSTriggers)
3801  {
3802  fireBSTriggers(node);
3803  node->fireBSTriggers = false;
3804  }
3805 
3806  /* Preload local variables */
3807  resultRelInfo = node->resultRelInfo + node->mt_lastResultIndex;
3808  subplanstate = outerPlanState(node);
3809 
3810  /* Set global context */
3811  context.mtstate = node;
3812  context.epqstate = &node->mt_epqstate;
3813  context.estate = estate;
3814 
3815  /*
3816  * Fetch rows from subplan, and execute the required table modification
3817  * for each row.
3818  */
3819  for (;;)
3820  {
3821  /*
3822  * Reset the per-output-tuple exprcontext. This is needed because
3823  * triggers expect to use that context as workspace. It's a bit ugly
3824  * to do this below the top level of the plan, however. We might need
3825  * to rethink this later.
3826  */
3827  ResetPerTupleExprContext(estate);
3828 
3829  /*
3830  * Reset per-tuple memory context used for processing on conflict and
3831  * returning clauses, to free any expression evaluation storage
3832  * allocated in the previous cycle.
3833  */
3834  if (pstate->ps_ExprContext)
3836 
3837  /*
3838  * If there is a pending MERGE ... WHEN NOT MATCHED [BY TARGET] action
3839  * to execute, do so now --- see the comments in ExecMerge().
3840  */
3841  if (node->mt_merge_pending_not_matched != NULL)
3842  {
3843  context.planSlot = node->mt_merge_pending_not_matched;
3844 
3845  slot = ExecMergeNotMatched(&context, node->resultRelInfo,
3846  node->canSetTag);
3847 
3848  /* Clear the pending action */
3849  node->mt_merge_pending_not_matched = NULL;
3850 
3851  /*
3852  * If we got a RETURNING result, return it to the caller. We'll
3853  * continue the work on next call.
3854  */
3855  if (slot)
3856  return slot;
3857 
3858  continue; /* continue with the next tuple */
3859  }
3860 
3861  /* Fetch the next row from subplan */
3862  context.planSlot = ExecProcNode(subplanstate);
3863 
3864  /* No more tuples to process? */
3865  if (TupIsNull(context.planSlot))
3866  break;
3867 
3868  /*
3869  * When there are multiple result relations, each tuple contains a
3870  * junk column that gives the OID of the rel from which it came.
3871  * Extract it and select the correct result relation.
3872  */
3874  {
3875  Datum datum;
3876  bool isNull;
3877  Oid resultoid;
3878 
3879  datum = ExecGetJunkAttribute(context.planSlot, node->mt_resultOidAttno,
3880  &isNull);
3881  if (isNull)
3882  {
3883  /*
3884  * For commands other than MERGE, any tuples having InvalidOid
3885  * for tableoid are errors. For MERGE, we may need to handle
3886  * them as WHEN NOT MATCHED clauses if any, so do that.
3887  *
3888  * Note that we use the node's toplevel resultRelInfo, not any
3889  * specific partition's.
3890  */
3891  if (operation == CMD_MERGE)
3892  {
3893  EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
3894 
3895  slot = ExecMerge(&context, node->resultRelInfo,
3896  NULL, NULL, node->canSetTag);
3897 
3898  /*
3899  * If we got a RETURNING result, return it to the caller.
3900  * We'll continue the work on next call.
3901  */
3902  if (slot)
3903  return slot;
3904 
3905  continue; /* continue with the next tuple */
3906  }
3907 
3908  elog(ERROR, "tableoid is NULL");
3909  }
3910  resultoid = DatumGetObjectId(datum);
3911 
3912  /* If it's not the same as last time, we need to locate the rel */
3913  if (resultoid != node->mt_lastResultOid)
3914  resultRelInfo = ExecLookupResultRelByOid(node, resultoid,
3915  false, true);
3916  }
3917 
3918  /*
3919  * If resultRelInfo->ri_usesFdwDirectModify is true, all we need to do
3920  * here is compute the RETURNING expressions.
3921  */
3922  if (resultRelInfo->ri_usesFdwDirectModify)
3923  {
3924  Assert(resultRelInfo->ri_projectReturning);
3925 
3926  /*
3927  * A scan slot containing the data that was actually inserted,
3928  * updated or deleted has already been made available to
3929  * ExecProcessReturning by IterateDirectModify, so no need to
3930  * provide it here.
3931  */
3932  slot = ExecProcessReturning(resultRelInfo, NULL, context.planSlot);
3933 
3934  return slot;
3935  }
3936 
3937  EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
3938  slot = context.planSlot;
3939 
3940  tupleid = NULL;
3941  oldtuple = NULL;
3942 
3943  /*
3944  * For UPDATE/DELETE/MERGE, fetch the row identity info for the tuple
3945  * to be updated/deleted/merged. For a heap relation, that's a TID;
3946  * otherwise we may have a wholerow junk attr that carries the old
3947  * tuple in toto. Keep this in step with the part of
3948  * ExecInitModifyTable that sets up ri_RowIdAttNo.
3949  */
3950  if (operation == CMD_UPDATE || operation == CMD_DELETE ||
3951  operation == CMD_MERGE)
3952  {
3953  char relkind;
3954  Datum datum;
3955  bool isNull;
3956 
3957  relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
3958  if (relkind == RELKIND_RELATION ||
3959  relkind == RELKIND_MATVIEW ||
3960  relkind == RELKIND_PARTITIONED_TABLE)
3961  {
3962  /* ri_RowIdAttNo refers to a ctid attribute */
3963  Assert(AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo));
3964  datum = ExecGetJunkAttribute(slot,
3965  resultRelInfo->ri_RowIdAttNo,
3966  &isNull);
3967 
3968  /*
3969  * For commands other than MERGE, any tuples having a null row
3970  * identifier are errors. For MERGE, we may need to handle
3971  * them as WHEN NOT MATCHED clauses if any, so do that.
3972  *
3973  * Note that we use the node's toplevel resultRelInfo, not any
3974  * specific partition's.
3975  */
3976  if (isNull)
3977  {
3978  if (operation == CMD_MERGE)
3979  {
3980  EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
3981 
3982  slot = ExecMerge(&context, node->resultRelInfo,
3983  NULL, NULL, node->canSetTag);
3984 
3985  /*
3986  * If we got a RETURNING result, return it to the
3987  * caller. We'll continue the work on next call.
3988  */
3989  if (slot)
3990  return slot;
3991 
3992  continue; /* continue with the next tuple */
3993  }
3994 
3995  elog(ERROR, "ctid is NULL");
3996  }
3997 
3998  tupleid = (ItemPointer) DatumGetPointer(datum);
3999  tuple_ctid = *tupleid; /* be sure we don't free ctid!! */
4000  tupleid = &tuple_ctid;
4001  }
4002 
4003  /*
4004  * Use the wholerow attribute, when available, to reconstruct the
4005  * old relation tuple. The old tuple serves one or both of two
4006  * purposes: 1) it serves as the OLD tuple for row triggers, 2) it
4007  * provides values for any unchanged columns for the NEW tuple of
4008  * an UPDATE, because the subplan does not produce all the columns
4009  * of the target table.
4010  *
4011  * Note that the wholerow attribute does not carry system columns,
4012  * so foreign table triggers miss seeing those, except that we
4013  * know enough here to set t_tableOid. Quite separately from
4014  * this, the FDW may fetch its own junk attrs to identify the row.
4015  *
4016  * Other relevant relkinds, currently limited to views, always
4017  * have a wholerow attribute.
4018  */
4019  else if (AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
4020  {
4021  datum = ExecGetJunkAttribute(slot,
4022  resultRelInfo->ri_RowIdAttNo,
4023  &isNull);
4024 
4025  /*
4026  * For commands other than MERGE, any tuples having a null row
4027  * identifier are errors. For MERGE, we may need to handle
4028  * them as WHEN NOT MATCHED clauses if any, so do that.
4029  *
4030  * Note that we use the node's toplevel resultRelInfo, not any
4031  * specific partition's.
4032  */
4033  if (isNull)
4034  {
4035  if (operation == CMD_MERGE)
4036  {
4037  EvalPlanQualSetSlot(&node->mt_epqstate, context.planSlot);
4038 
4039  slot = ExecMerge(&context, node->resultRelInfo,
4040  NULL, NULL, node->canSetTag);
4041 
4042  /*
4043  * If we got a RETURNING result, return it to the
4044  * caller. We'll continue the work on next call.
4045  */
4046  if (slot)
4047  return slot;
4048 
4049  continue; /* continue with the next tuple */
4050  }
4051 
4052  elog(ERROR, "wholerow is NULL");
4053  }
4054 
4055  oldtupdata.t_data = DatumGetHeapTupleHeader(datum);
4056  oldtupdata.t_len =
4058  ItemPointerSetInvalid(&(oldtupdata.t_self));
4059  /* Historically, view triggers see invalid t_tableOid. */
4060  oldtupdata.t_tableOid =
4061  (relkind == RELKIND_VIEW) ? InvalidOid :
4062  RelationGetRelid(resultRelInfo->ri_RelationDesc);
4063 
4064  oldtuple = &oldtupdata;
4065  }
4066  else
4067  {
4068  /* Only foreign tables are allowed to omit a row-ID attr */
4069  Assert(relkind == RELKIND_FOREIGN_TABLE);
4070  }
4071  }
4072 
4073  switch (operation)
4074  {
4075  case CMD_INSERT:
4076  /* Initialize projection info if first time for this table */
4077  if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
4078  ExecInitInsertProjection(node, resultRelInfo);
4079  slot = ExecGetInsertNewTuple(resultRelInfo, context.planSlot);
4080  slot = ExecInsert(&context, resultRelInfo, slot,
4081  node->canSetTag, NULL, NULL);
4082  break;
4083 
4084  case CMD_UPDATE:
4085  /* Initialize projection info if first time for this table */
4086  if (unlikely(!resultRelInfo->ri_projectNewInfoValid))
4087  ExecInitUpdateProjection(node, resultRelInfo);
4088 
4089  /*
4090  * Make the new tuple by combining plan's output tuple with
4091  * the old tuple being updated.
4092  */
4093  oldSlot = resultRelInfo->ri_oldTupleSlot;
4094  if (oldtuple != NULL)
4095  {
4096  /* Use the wholerow junk attr as the old tuple. */
4097  ExecForceStoreHeapTuple(oldtuple, oldSlot, false);
4098  }
4099  else
4100  {
4101  /* Fetch the most recent version of old tuple. */
4102  Relation relation = resultRelInfo->ri_RelationDesc;
4103 
4104  if (!table_tuple_fetch_row_version(relation, tupleid,
4105  SnapshotAny,
4106  oldSlot))
4107  elog(ERROR, "failed to fetch tuple being updated");
4108  }
4109  slot = ExecGetUpdateNewTuple(resultRelInfo, context.planSlot,
4110  oldSlot);
4111 
4112  /* Now apply the update. */
4113  slot = ExecUpdate(&context, resultRelInfo, tupleid, oldtuple,
4114  slot, node->canSetTag);
4115  break;
4116 
4117  case CMD_DELETE:
4118  slot = ExecDelete(&context, resultRelInfo, tupleid, oldtuple,
4119  true, false, node->canSetTag, NULL, NULL, NULL);
4120  break;
4121 
4122  case CMD_MERGE:
4123  slot = ExecMerge(&context, resultRelInfo, tupleid, oldtuple,
4124  node->canSetTag);
4125  break;
4126 
4127  default:
4128  elog(ERROR, "unknown operation");
4129  break;
4130  }
4131 
4132  /*
4133  * If we got a RETURNING result, return it to caller. We'll continue
4134  * the work on next call.
4135  */
4136  if (slot)
4137  return slot;
4138  }
4139 
4140  /*
4141  * Insert remaining tuples for batch insert.
4142  */
4143  if (estate->es_insert_pending_result_relations != NIL)
4144  ExecPendingInserts(estate);
4145 
4146  /*
4147  * We're done, but fire AFTER STATEMENT triggers before exiting.
4148  */
4149  fireASTriggers(node);
4150 
4151  node->mt_done = true;
4152 
4153  return NULL;
4154 }
4155 
4156 /*
4157  * ExecLookupResultRelByOid
4158  * If the table with given OID is among the result relations to be
4159  * updated by the given ModifyTable node, return its ResultRelInfo.
4160  *
4161  * If not found, return NULL if missing_ok, else raise error.
4162  *
4163  * If update_cache is true, then upon successful lookup, update the node's
4164  * one-element cache. ONLY ExecModifyTable may pass true for this.
4165  */
4166 ResultRelInfo *
4168  bool missing_ok, bool update_cache)
4169 {
4170  if (node->mt_resultOidHash)
4171  {
4172  /* Use the pre-built hash table to locate the rel */
4173  MTTargetRelLookup *mtlookup;
4174 
4175  mtlookup = (MTTargetRelLookup *)
4176  hash_search(node->mt_resultOidHash, &resultoid, HASH_FIND, NULL);
4177  if (mtlookup)
4178  {
4179  if (update_cache)
4180  {
4181  node->mt_lastResultOid = resultoid;
4182  node->mt_lastResultIndex = mtlookup->relationIndex;
4183  }
4184  return node->resultRelInfo + mtlookup->relationIndex;
4185  }
4186  }
4187  else
4188  {
4189  /* With few target rels, just search the ResultRelInfo array */
4190  for (int ndx = 0; ndx < node->mt_nrels; ndx++)
4191  {
4192  ResultRelInfo *rInfo = node->resultRelInfo + ndx;
4193 
4194  if (RelationGetRelid(rInfo->ri_RelationDesc) == resultoid)
4195  {
4196  if (update_cache)
4197  {
4198  node->mt_lastResultOid = resultoid;
4199  node->mt_lastResultIndex = ndx;
4200  }
4201  return rInfo;
4202  }
4203  }
4204  }
4205 
4206  if (!missing_ok)
4207  elog(ERROR, "incorrect result relation OID %u", resultoid);
4208  return NULL;
4209 }
4210 
4211 /* ----------------------------------------------------------------
4212  * ExecInitModifyTable
4213  * ----------------------------------------------------------------
4214  */
4216 ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
4217 {
4218  ModifyTableState *mtstate;
4219  Plan *subplan = outerPlan(node);
4220  CmdType operation = node->operation;
4221  int nrels = list_length(node->resultRelations);
4222  ResultRelInfo *resultRelInfo;
4223  List *arowmarks;
4224  ListCell *l;
4225  int i;
4226  Relation rel;
4227 
4228  /* check for unsupported flags */
4229  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
4230 
4231  /*
4232  * create state structure
4233  */
4234  mtstate = makeNode(ModifyTableState);
4235  mtstate->ps.plan = (Plan *) node;
4236  mtstate->ps.state = estate;
4237  mtstate->ps.ExecProcNode = ExecModifyTable;
4238 
4239  mtstate->operation = operation;
4240  mtstate->canSetTag = node->canSetTag;
4241  mtstate->mt_done = false;
4242 
4243  mtstate->mt_nrels = nrels;
4244  mtstate->resultRelInfo = (ResultRelInfo *)
4245  palloc(nrels * sizeof(ResultRelInfo));
4246 
4247  mtstate->mt_merge_pending_not_matched = NULL;
4248  mtstate->mt_merge_inserted = 0;
4249  mtstate->mt_merge_updated = 0;
4250  mtstate->mt_merge_deleted = 0;
4251 
4252  /*----------
4253  * Resolve the target relation. This is the same as:
4254  *
4255  * - the relation for which we will fire FOR STATEMENT triggers,
4256  * - the relation into whose tuple format all captured transition tuples
4257  * must be converted, and
4258  * - the root partitioned table used for tuple routing.
4259  *
4260  * If it's a partitioned or inherited table, the root partition or
4261  * appendrel RTE doesn't appear elsewhere in the plan and its RT index is
4262  * given explicitly in node->rootRelation. Otherwise, the target relation
4263  * is the sole relation in the node->resultRelations list.
4264  *----------
4265  */
4266  if (node->rootRelation > 0)
4267  {
4269  ExecInitResultRelation(estate, mtstate->rootResultRelInfo,
4270  node->rootRelation);
4271  }
4272  else
4273  {
4274  Assert(list_length(node->resultRelations) == 1);
4275  mtstate->rootResultRelInfo = mtstate->resultRelInfo;
4276  ExecInitResultRelation(estate, mtstate->resultRelInfo,
4277  linitial_int(node->resultRelations));
4278  }
4279 
4280  /* set up epqstate with dummy subplan data for the moment */
4281  EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL,
4282  node->epqParam, node->resultRelations);
4283  mtstate->fireBSTriggers = true;
4284 
4285  /*
4286  * Build state for collecting transition tuples. This requires having a
4287  * valid trigger query context, so skip it in explain-only mode.
4288  */
4289  if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
4290  ExecSetupTransitionCaptureState(mtstate, estate);
4291 
4292  /*
4293  * Open all the result relations and initialize the ResultRelInfo structs.
4294  * (But root relation was initialized above, if it's part of the array.)
4295  * We must do this before initializing the subplan, because direct-modify
4296  * FDWs expect their ResultRelInfos to be available.
4297  */
4298  resultRelInfo = mtstate->resultRelInfo;
4299  i = 0;
4300  foreach(l, node->resultRelations)
4301  {
4302  Index resultRelation = lfirst_int(l);
4303  List *mergeActions = NIL;
4304 
4305  if (node->mergeActionLists)
4306  mergeActions = list_nth(node->mergeActionLists, i);
4307 
4308  if (resultRelInfo != mtstate->rootResultRelInfo)
4309  {
4310  ExecInitResultRelation(estate, resultRelInfo, resultRelation);
4311 
4312  /*
4313  * For child result relations, store the root result relation
4314  * pointer. We do so for the convenience of places that want to
4315  * look at the query's original target relation but don't have the
4316  * mtstate handy.
4317  */
4318  resultRelInfo->ri_RootResultRelInfo = mtstate->rootResultRelInfo;
4319  }
4320 
4321  /* Initialize the usesFdwDirectModify flag */
4322  resultRelInfo->ri_usesFdwDirectModify =
4324 
4325  /*
4326  * Verify result relation is a valid target for the current operation
4327  */
4328  CheckValidResultRel(resultRelInfo, operation, mergeActions);
4329 
4330  resultRelInfo++;
4331  i++;
4332  }
4333 
4334  /*
4335  * Now we may initialize the subplan.
4336  */
4337  outerPlanState(mtstate) = ExecInitNode(subplan, estate, eflags);
4338 
4339  /*
4340  * Do additional per-result-relation initialization.
4341  */
4342  for (i = 0; i < nrels; i++)
4343  {
4344  resultRelInfo = &mtstate->resultRelInfo[i];
4345 
4346  /* Let FDWs init themselves for foreign-table result rels */
4347  if (!resultRelInfo->ri_usesFdwDirectModify &&
4348  resultRelInfo->ri_FdwRoutine != NULL &&
4349  resultRelInfo->ri_FdwRoutine->BeginForeignModify != NULL)
4350  {
4351  List *fdw_private = (List *) list_nth(node->fdwPrivLists, i);
4352 
4353  resultRelInfo->ri_FdwRoutine->BeginForeignModify(mtstate,
4354  resultRelInfo,
4355  fdw_private,
4356  i,
4357  eflags);
4358  }
4359 
4360  /*
4361  * For UPDATE/DELETE/MERGE, find the appropriate junk attr now, either
4362  * a 'ctid' or 'wholerow' attribute depending on relkind. For foreign
4363  * tables, the FDW might have created additional junk attr(s), but
4364  * those are no concern of ours.
4365  */
4366  if (operation == CMD_UPDATE || operation == CMD_DELETE ||
4367  operation == CMD_MERGE)
4368  {
4369  char relkind;
4370 
4371  relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
4372  if (relkind == RELKIND_RELATION ||
4373  relkind == RELKIND_MATVIEW ||
4374  relkind == RELKIND_PARTITIONED_TABLE)
4375  {
4376  resultRelInfo->ri_RowIdAttNo =
4377  ExecFindJunkAttributeInTlist(subplan->targetlist, "ctid");
4378  if (!AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
4379  elog(ERROR, "could not find junk ctid column");
4380  }
4381  else if (relkind == RELKIND_FOREIGN_TABLE)
4382  {
4383  /*
4384  * We don't support MERGE with foreign tables for now. (It's
4385  * problematic because the implementation uses CTID.)
4386  */
4387  Assert(operation != CMD_MERGE);
4388 
4389  /*
4390  * When there is a row-level trigger, there should be a
4391  * wholerow attribute. We also require it to be present in
4392  * UPDATE and MERGE, so we can get the values of unchanged
4393  * columns.
4394  */
4395  resultRelInfo->ri_RowIdAttNo =
4397  "wholerow");
4398  if ((mtstate->operation == CMD_UPDATE || mtstate->operation == CMD_MERGE) &&
4399  !AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
4400  elog(ERROR, "could not find junk wholerow column");
4401  }
4402  else
4403  {
4404  /* Other valid target relkinds must provide wholerow */
4405  resultRelInfo->ri_RowIdAttNo =
4407  "wholerow");
4408  if (!AttributeNumberIsValid(resultRelInfo->ri_RowIdAttNo))
4409  elog(ERROR, "could not find junk wholerow column");
4410  }
4411  }
4412  }
4413 
4414  /*
4415  * If this is an inherited update/delete/merge, there will be a junk
4416  * attribute named "tableoid" present in the subplan's targetlist. It
4417  * will be used to identify the result relation for a given tuple to be
4418  * updated/deleted/merged.
4419  */
4420  mtstate->mt_resultOidAttno =
4421  ExecFindJunkAttributeInTlist(subplan->targetlist, "tableoid");
4422  Assert(AttributeNumberIsValid(mtstate->mt_resultOidAttno) || nrels == 1);
4423  mtstate->mt_lastResultOid = InvalidOid; /* force lookup at first tuple */
4424  mtstate->mt_lastResultIndex = 0; /* must be zero if no such attr */
4425 
4426  /* Get the root target relation */
4427  rel = mtstate->rootResultRelInfo->ri_RelationDesc;
4428 
4429  /*
4430  * Build state for tuple routing if it's a partitioned INSERT. An UPDATE
4431  * or MERGE might need this too, but only if it actually moves tuples
4432  * between partitions; in that case setup is done by
4433  * ExecCrossPartitionUpdate.
4434  */
4435  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
4436  operation == CMD_INSERT)
4437  mtstate->mt_partition_tuple_routing =
4438  ExecSetupPartitionTupleRouting(estate, rel);
4439 
4440  /*
4441  * Initialize any WITH CHECK OPTION constraints if needed.
4442  */
4443  resultRelInfo = mtstate->resultRelInfo;
4444  foreach(l, node->withCheckOptionLists)
4445  {
4446  List *wcoList = (List *) lfirst(l);
4447  List *wcoExprs = NIL;
4448  ListCell *ll;
4449 
4450  foreach(ll, wcoList)
4451  {
4452  WithCheckOption *wco = (WithCheckOption *) lfirst(ll);
4453  ExprState *wcoExpr = ExecInitQual((List *) wco->qual,
4454  &mtstate->ps);
4455 
4456  wcoExprs = lappend(wcoExprs, wcoExpr);
4457  }
4458 
4459  resultRelInfo->ri_WithCheckOptions = wcoList;
4460  resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
4461  resultRelInfo++;
4462  }
4463 
4464  /*
4465  * Initialize RETURNING projections if needed.
4466  */
4467  if (node->returningLists)
4468  {
4469  TupleTableSlot *slot;
4470  ExprContext *econtext;
4471 
4472  /*
4473  * Initialize result tuple slot and assign its rowtype using the first
4474  * RETURNING list. We assume the rest will look the same.
4475  */
4476  mtstate->ps.plan->targetlist = (List *) linitial(node->returningLists);
4477 
4478  /* Set up a slot for the output of the RETURNING projection(s) */
4480  slot = mtstate->ps.ps_ResultTupleSlot;
4481 
4482  /* Need an econtext too */
4483  if (mtstate->ps.ps_ExprContext == NULL)
4484  ExecAssignExprContext(estate, &mtstate->ps);
4485  econtext = mtstate->ps.ps_ExprContext;
4486 
4487  /*
4488  * Build a projection for each result rel.
4489  */
4490  resultRelInfo = mtstate->resultRelInfo;
4491  foreach(l, node->returningLists)
4492  {
4493  List *rlist = (List *) lfirst(l);
4494 
4495  resultRelInfo->ri_returningList = rlist;
4496  resultRelInfo->ri_projectReturning =
4497  ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
4498  resultRelInfo->ri_RelationDesc->rd_att);
4499  resultRelInfo++;
4500  }
4501  }
4502  else
4503  {
4504  /*
4505  * We still must construct a dummy result tuple type, because InitPlan
4506  * expects one (maybe should change that?).
4507  */
4508  mtstate->ps.plan->targetlist = NIL;
4509  ExecInitResultTypeTL(&mtstate->ps);
4510 
4511  mtstate->ps.ps_ExprContext = NULL;
4512  }
4513 
4514  /* Set the list of arbiter indexes if needed for ON CONFLICT */
4515  resultRelInfo = mtstate->resultRelInfo;
4516  if (node->onConflictAction != ONCONFLICT_NONE)
4517  {
4518  /* insert may only have one relation, inheritance is not expanded */
4519  Assert(nrels == 1);
4520  resultRelInfo->ri_onConflictArbiterIndexes = node->arbiterIndexes;
4521  }
4522 
4523  /*
4524  * If needed, Initialize target list, projection and qual for ON CONFLICT
4525  * DO UPDATE.
4526  */
4527  if (node->onConflictAction == ONCONFLICT_UPDATE)
4528  {
4530  ExprContext *econtext;
4531  TupleDesc relationDesc;
4532 
4533  /* already exists if created by RETURNING processing above */
4534  if (mtstate->ps.ps_ExprContext == NULL)
4535  ExecAssignExprContext(estate, &mtstate->ps);
4536 
4537  econtext = mtstate->ps.ps_ExprContext;
4538  relationDesc = resultRelInfo->ri_RelationDesc->rd_att;
4539 
4540  /* create state for DO UPDATE SET operation */
4541  resultRelInfo->ri_onConflict = onconfl;
4542 
4543  /* initialize slot for the existing tuple */
4544  onconfl->oc_Existing =
4545  table_slot_create(resultRelInfo->ri_RelationDesc,
4546  &mtstate->ps.state->es_tupleTable);
4547 
4548  /*
4549  * Create the tuple slot for the UPDATE SET projection. We want a slot
4550  * of the table's type here, because the slot will be used to insert
4551  * into the table, and for RETURNING processing - which may access
4552  * system attributes.
4553  */
4554  onconfl->oc_ProjSlot =
4555  table_slot_create(resultRelInfo->ri_RelationDesc,
4556  &mtstate->ps.state->es_tupleTable);
4557 
4558  /* build UPDATE SET projection state */
4559  onconfl->oc_ProjInfo =
4561  true,
4562  node->onConflictCols,
4563  relationDesc,
4564  econtext,
4565  onconfl->oc_ProjSlot,
4566  &mtstate->ps);
4567 
4568  /* initialize state to evaluate the WHERE clause, if any */
4569  if (node->onConflictWhere)
4570  {
4571  ExprState *qualexpr;
4572 
4573  qualexpr = ExecInitQual((List *) node->onConflictWhere,
4574  &mtstate->ps);
4575  onconfl->oc_WhereClause = qualexpr;
4576  }
4577  }
4578 
4579  /*
4580  * If we have any secondary relations in an UPDATE or DELETE, they need to
4581  * be treated like non-locked relations in SELECT FOR UPDATE, i.e., the
4582  * EvalPlanQual mechanism needs to be told about them. This also goes for
4583  * the source relations in a MERGE. Locate the relevant ExecRowMarks.
4584  */
4585  arowmarks = NIL;
4586  foreach(l, node->rowMarks)
4587  {
4589  ExecRowMark *erm;
4590  ExecAuxRowMark *aerm;
4591 
4592  /* ignore "parent" rowmarks; they are irrelevant at runtime */
4593  if (rc->isParent)
4594  continue;
4595 
4596  /* Find ExecRowMark and build ExecAuxRowMark */
4597  erm = ExecFindRowMark(estate, rc->rti, false);
4598  aerm = ExecBuildAuxRowMark(erm, subplan->targetlist);
4599  arowmarks = lappend(arowmarks, aerm);
4600  }
4601 
4602  /* For a MERGE command, initialize its state */
4603  if (mtstate->operation == CMD_MERGE)
4604  ExecInitMerge(mtstate, estate);
4605 
4606  EvalPlanQualSetPlan(&mtstate->mt_epqstate, subplan, arowmarks);
4607 
4608  /*
4609  * If there are a lot of result relations, use a hash table to speed the
4610  * lookups. If there are not a lot, a simple linear search is faster.
4611  *
4612  * It's not clear where the threshold is, but try 64 for starters. In a
4613  * debugging build, use a small threshold so that we get some test
4614  * coverage of both code paths.
4615  */
4616 #ifdef USE_ASSERT_CHECKING
4617 #define MT_NRELS_HASH 4
4618 #else
4619 #define MT_NRELS_HASH 64
4620 #endif
4621  if (nrels >= MT_NRELS_HASH)
4622  {
4623  HASHCTL hash_ctl;
4624 
4625  hash_ctl.keysize = sizeof(Oid);
4626  hash_ctl.entrysize = sizeof(MTTargetRelLookup);
4627  hash_ctl.hcxt = CurrentMemoryContext;
4628  mtstate->mt_resultOidHash =
4629  hash_create("ModifyTable target hash",
4630  nrels, &hash_ctl,
4632  for (i = 0; i < nrels; i++)
4633  {
4634  Oid hashkey;
4635  MTTargetRelLookup *mtlookup;
4636  bool found;
4637 
4638  resultRelInfo = &mtstate->resultRelInfo[i];
4639  hashkey = RelationGetRelid(resultRelInfo->ri_RelationDesc);
4640  mtlookup = (MTTargetRelLookup *)
4641  hash_search(mtstate->mt_resultOidHash, &hashkey,
4642  HASH_ENTER, &found);
4643  Assert(!found);
4644  mtlookup->relationIndex = i;
4645  }
4646  }
4647  else
4648  mtstate->mt_resultOidHash = NULL;
4649 
4650  /*
4651  * Determine if the FDW supports batch insert and determine the batch size
4652  * (a FDW may support batching, but it may be disabled for the
4653  * server/table).
4654  *
4655  * We only do this for INSERT, so that for UPDATE/DELETE the batch size
4656  * remains set to 0.
4657  */
4658  if (operation == CMD_INSERT)
4659  {
4660  /* insert may only have one relation, inheritance is not expanded */
4661  Assert(nrels == 1);
4662  resultRelInfo = mtstate->resultRelInfo;
4663  if (!resultRelInfo->ri_usesFdwDirectModify &&
4664  resultRelInfo->ri_FdwRoutine != NULL &&
4665  resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize &&
4666  resultRelInfo->ri_FdwRoutine->ExecForeignBatchInsert)
4667  {
4668  resultRelInfo->ri_BatchSize =
4669  resultRelInfo->ri_FdwRoutine->GetForeignModifyBatchSize(resultRelInfo);
4670  Assert(resultRelInfo->ri_BatchSize >= 1);
4671  }
4672  else
4673  resultRelInfo->ri_BatchSize = 1;
4674  }
4675 
4676  /*
4677  * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it
4678  * to estate->es_auxmodifytables so that it will be run to completion by
4679  * ExecPostprocessPlan. (It'd actually work fine to add the primary
4680  * ModifyTable node too, but there's no need.) Note the use of lcons not
4681  * lappend: we need later-initialized ModifyTable nodes to be shut down
4682  * before earlier ones. This ensures that we don't throw away RETURNING
4683  * rows that need to be seen by a later CTE subplan.
4684  */
4685  if (!mtstate->canSetTag)
4686  estate->es_auxmodifytables = lcons(mtstate,
4687  estate->es_auxmodifytables);
4688 
4689  return mtstate;
4690 }
4691 
4692 /* ----------------------------------------------------------------
4693  * ExecEndModifyTable
4694  *
4695  * Shuts down the plan.
4696  *
4697  * Returns nothing of interest.
4698  * ----------------------------------------------------------------
4699  */
4700 void
4702 {
4703  int i;
4704 
4705  /*
4706  * Allow any FDWs to shut down
4707  */
4708  for (i = 0; i < node->mt_nrels; i++)
4709  {
4710  int j;
4711  ResultRelInfo *resultRelInfo = node->resultRelInfo + i;
4712 
4713  if (!resultRelInfo->ri_usesFdwDirectModify &&
4714  resultRelInfo->ri_FdwRoutine != NULL &&
4715  resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
4716  resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
4717  resultRelInfo);
4718 
4719  /*
4720  * Cleanup the initialized batch slots. This only matters for FDWs
4721  * with batching, but the other cases will have ri_NumSlotsInitialized
4722  * == 0.
4723  */
4724  for (j = 0; j < resultRelInfo->ri_NumSlotsInitialized; j++)
4725  {
4726  ExecDropSingleTupleTableSlot(resultRelInfo->ri_Slots[j]);
4727  ExecDropSingleTupleTableSlot(resultRelInfo->ri_PlanSlots[j]);
4728  }
4729  }
4730 
4731  /*
4732  * Close all the partitioned tables, leaf partitions, and their indices
4733  * and release the slot used for tuple routing, if set.
4734  */
4735  if (node->mt_partition_tuple_routing)
4736  {
4738 
4739  if (node->mt_root_tuple_slot)
4741  }
4742 
4743  /*
4744  * Terminate EPQ execution if active
4745  */
4746  EvalPlanQualEnd(&node->mt_epqstate);
4747 
4748  /*
4749  * shut down subplan
4750  */
4751  ExecEndNode(outerPlanState(node));
4752 }
4753 
4754 void
4756 {
4757  /*
4758  * Currently, we don't need to support rescan on ModifyTable nodes. The
4759  * semantics of that would be a bit debatable anyway.
4760  */
4761  elog(ERROR, "ExecReScanModifyTable is not implemented");
4762 }
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
static Datum values[MAXATTR]
Definition: bootstrap.c:150
unsigned int uint32
Definition: c.h:506
#define Assert(condition)
Definition: c.h:858
#define unlikely(x)
Definition: c.h:311
unsigned int Index
Definition: c.h:614
uint32 TransactionId
Definition: c.h:652
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:743
ProjectionInfo * ExecBuildUpdateProjection(List *targetList, bool evalTargetList, List *targetColnos, TupleDesc relDesc, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent)
Definition: execExpr.c:525
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:224
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:365
List * ExecInsertIndexTuples(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool update, bool noDupErr, bool *specConflict, List *arbiterIndexes, bool onlySummarizing)
Definition: execIndexing.c:299
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:156
bool ExecCheckIndexConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, ItemPointer conflictTid, ItemPointer tupleid, List *arbiterIndexes)
Definition: execIndexing.c:532
AttrNumber ExecFindJunkAttributeInTlist(List *targetlist, const char *attrName)
Definition: execJunk.c:222
LockTupleMode ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo)
Definition: execMain.c:2346
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation, List *mergeActions)
Definition: execMain.c:1021
void EvalPlanQualBegin(EPQState *epqstate)
Definition: execMain.c:2746
TupleTableSlot * EvalPlanQual(EPQState *epqstate, Relation relation, Index rti, TupleTableSlot *inputslot)
Definition: execMain.c:2465
bool ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool emitError)
Definition: execMain.c:1787
void EvalPlanQualInit(EPQState *epqstate, EState *parentestate, Plan *subplan, List *auxrowmarks, int epqParam, List *resultRelations)
Definition: execMain.c:2534
void ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:2046
ExecAuxRowMark * ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
Definition: execMain.c:2395
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:2977
void EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
Definition: execMain.c:2576
ExecRowMark * ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
Definition: execMain.c:2372
List * ExecGetAncestorResultRels(EState *estate, ResultRelInfo *resultRelInfo)
Definition: execMain.c:1367
TupleTableSlot * EvalPlanQualSlot(EPQState *epqstate, Relation relation, Index rti)
Definition: execMain.c:2593
void ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1840
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1911
ResultRelInfo * ExecFindPartition(ModifyTableState *mtstate, ResultRelInfo *rootResultRelInfo, PartitionTupleRouting *proute, TupleTableSlot *slot, EState *estate)
PartitionTupleRouting * ExecSetupPartitionTupleRouting(EState *estate, Relation rel)
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:562
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1639
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1341
TupleTableSlot * ExecStoreAllNullTuple(TupleTableSlot *slot)
Definition: execTuples.c:1663
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1842
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1886
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1325
void ExecForceStoreHeapTuple(HeapTuple tuple, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1556
Bitmapset * ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
Definition: execUtils.c:1288
TupleTableSlot * ExecGetReturningSlot(EState *estate, ResultRelInfo *relInfo)
Definition: execUtils.c:1182
void ExecInitResultRelation(EState *estate, ResultRelInfo *resultRelInfo, Index rti)
Definition: execUtils.c:814
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:483
TupleConversionMap * ExecGetRootToChildMap(ResultRelInfo *resultRelInfo, EState *estate)
Definition: execUtils.c:1232
TupleConversionMap * ExecGetChildToRootMap(ResultRelInfo *resultRelInfo)
Definition: execUtils.c:1206
#define MERGE_UPDATE
Definition: execnodes.h:1348
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:1223
#define outerPlanState(node)
Definition: execnodes.h:1215
#define InstrCountTuples2(node, delta)
Definition: execnodes.h:1218
#define MERGE_INSERT
Definition: execnodes.h:1347
#define MERGE_DELETE
Definition: execnodes.h:1349
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define ResetPerTupleExprContext(estate)
Definition: executor.h:570
#define GetPerTupleExprContext(estate)
Definition: executor.h:561
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:387
#define ResetExprContext(econtext)
Definition: executor.h:555
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:566
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:424
#define EvalPlanQualSetSlot(epqstate, slot)
Definition: executor.h:248
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:344
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:65
static Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno, bool *isNull)
Definition: executor.h:190
#define EXEC_FLAG_MARK
Definition: executor.h:69
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:273
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:450
long val
Definition: informix.c:689
int j
Definition: isn.c:74
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
static bool ItemPointerIndicatesMovedPartitions(const ItemPointerData *pointer)
Definition: itemptr.h:197
ItemPointerData * ItemPointer
Definition: itemptr.h:49
List * lappend(List *list, void *datum)
Definition: list.c:339
bool list_member_ptr(const List *list, const void *datum)
Definition: list.c:682
void list_free(List *list)
Definition: list.c:1546
List * lcons(void *datum, List *list)
Definition: list.c:495
uint32 SpeculativeInsertionLockAcquire(TransactionId xid)
Definition: lmgr.c:772
void SpeculativeInsertionLockRelease(TransactionId xid)
Definition: lmgr.c:798
@ LockWaitBlock
Definition: lockoptions.h:39
LockTupleMode
Definition: lockoptions.h:50
@ LockTupleExclusive
Definition: lockoptions.h:58
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void * palloc(Size size)
Definition: mcxt.c:1317
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:451
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static void ExecInitInsertProjection(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo)
static void ExecPendingInserts(EState *estate)
static void ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
void ExecInitMergeTupleSlots(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo)
struct ModifyTableContext ModifyTableContext
static void ExecUpdatePrepareSlot(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
static TupleTableSlot * ExecMergeMatched(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, bool canSetTag, bool *matched)
static TupleTableSlot * ExecInsert(ModifyTableContext *context, ResultRelInfo *resultRelInfo, TupleTableSlot *slot, bool canSetTag, TupleTableSlot **inserted_tuple, ResultRelInfo **insert_destrel)
void ExecInitStoredGenerated(ResultRelInfo *resultRelInfo, EState *estate, CmdType cmdtype)
static bool ExecDeletePrologue(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot **epqreturnslot, TM_Result *result)
static void ExecCheckPlanOutput(Relation resultRel, List *targetList)
static TupleTableSlot * ExecModifyTable(PlanState *pstate)
static void ExecCrossPartitionUpdateForeignKey(ModifyTableContext *context, ResultRelInfo *sourcePartInfo, ResultRelInfo *destPartInfo, ItemPointer tupleid, TupleTableSlot *oldslot, TupleTableSlot *newslot)
TupleTableSlot * ExecGetUpdateNewTuple(ResultRelInfo *relinfo, TupleTableSlot *planSlot, TupleTableSlot *oldSlot)
static void ExecInitUpdateProjection(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo)
static void ExecCheckTIDVisible(EState *estate, ResultRelInfo *relinfo, ItemPointer tid, TupleTableSlot *tempSlot)
static TM_Result ExecDeleteAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, bool changingPart)
static void ExecCheckTupleVisible(EState *estate, Relation rel, TupleTableSlot *slot)
static TupleTableSlot * ExecGetInsertNewTuple(ResultRelInfo *relinfo, TupleTableSlot *planSlot)
void ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo, EState *estate, TupleTableSlot *slot, CmdType cmdtype)
static TupleTableSlot * ExecPrepareTupleRouting(ModifyTableState *mtstate, EState *estate, PartitionTupleRouting *proute, ResultRelInfo *targetRelInfo, TupleTableSlot *slot, ResultRelInfo **partRelInfo)
struct MTTargetRelLookup MTTargetRelLookup
static TupleTableSlot * ExecUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, bool canSetTag)
ResultRelInfo * ExecLookupResultRelByOid(ModifyTableState *node, Oid resultoid, bool missing_ok, bool update_cache)
struct UpdateContext UpdateContext
#define MT_NRELS_HASH
static TM_Result ExecUpdateAct(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, bool canSetTag, UpdateContext *updateCxt)
static void ExecUpdateEpilogue(ModifyTableContext *context, UpdateContext *updateCxt, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot)
static void fireBSTriggers(ModifyTableState *node)
static TupleTableSlot * ExecDelete(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, bool processReturning, bool changingPart, bool canSetTag, TM_Result *tmresult, bool *tupleDeleted, TupleTableSlot **epqreturnslot)
static TupleTableSlot * ExecProcessReturning(ResultRelInfo *resultRelInfo, TupleTableSlot *tupleSlot, TupleTableSlot *planSlot)
void ExecReScanModifyTable(ModifyTableState *node)
void ExecEndModifyTable(ModifyTableState *node)
ModifyTableState * ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
static void fireASTriggers(ModifyTableState *node)
static bool ExecOnConflictUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer conflictTid, TupleTableSlot *excludedSlot, bool canSetTag, TupleTableSlot **returning)
static void ExecBatchInsert(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo, TupleTableSlot **slots, TupleTableSlot **planSlots, int numSlots, EState *estate, bool canSetTag)
static bool ExecUpdatePrologue(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, TM_Result *result)
static void ExecDeleteEpilogue(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, bool changingPart)
static TupleTableSlot * ExecMergeNotMatched(ModifyTableContext *context, ResultRelInfo *resultRelInfo, bool canSetTag)
static TupleTableSlot * ExecMerge(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, bool canSetTag)
static bool ExecCrossPartitionUpdate(ModifyTableContext *context, ResultRelInfo *resultRelInfo, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, bool canSetTag, UpdateContext *updateCxt, TM_Result *tmresult, TupleTableSlot **retry_slot, TupleTableSlot **inserted_tuple, ResultRelInfo **insert_destrel)
static void ExecInitMerge(ModifyTableState *mtstate, EState *estate)
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
OnConflictAction
Definition: nodes.h:417
@ ONCONFLICT_NONE
Definition: nodes.h:418
@ ONCONFLICT_UPDATE
Definition: nodes.h:420
@ ONCONFLICT_NOTHING
Definition: nodes.h:419
CmdType
Definition: nodes.h:263
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_INSERT
Definition: nodes.h:267
@ CMD_DELETE
Definition: nodes.h:268
@ CMD_UPDATE
Definition: nodes.h:266
@ CMD_NOTHING
Definition: nodes.h:272
#define makeNode(_type_)
Definition: nodes.h:155
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
WCOKind
Definition: parsenodes.h:1358
@ WCO_RLS_MERGE_UPDATE_CHECK
Definition: parsenodes.h:1363
@ WCO_RLS_CONFLICT_CHECK
Definition: parsenodes.h:1362
@ WCO_RLS_INSERT_CHECK
Definition: parsenodes.h:1360
@ WCO_VIEW_CHECK
Definition: parsenodes.h:1359
@ WCO_RLS_UPDATE_CHECK
Definition: parsenodes.h:1361
@ WCO_RLS_MERGE_DELETE_CHECK
Definition: parsenodes.h:1364
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lfirst_int(lc)
Definition: pg_list.h:173
#define linitial_int(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:178
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define plan(x)
Definition: pg_regress.c:162
#define ERRCODE_T_R_SERIALIZATION_FAILURE
Definition: pgbench.c:76
#define outerPlan(node)
Definition: plannodes.h:183
uintptr_t Datum
Definition: postgres.h:64
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:242
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static TransactionId DatumGetTransactionId(Datum X)
Definition: postgres.h:262
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static void test(void)
@ MERGE_WHEN_NOT_MATCHED_BY_TARGET
Definition: primnodes.h:1994
@ MERGE_WHEN_NOT_MATCHED_BY_SOURCE
Definition: primnodes.h:1993
@ MERGE_WHEN_MATCHED
Definition: primnodes.h:1992
tree context
Definition: radixtree.h:1835
MemoryContextSwitchTo(old_ctx)
#define RelationGetRelid(relation)
Definition: rel.h:505
#define RelationGetDescr(relation)
Definition: rel.h:531
#define RelationGetRelationName(relation)
Definition: rel.h:539
Node * build_column_default(Relation rel, int attrno)
int RI_FKey_trigger_type(Oid tgfoid)
Definition: ri_triggers.c:2993
#define SnapshotAny
Definition: snapmgr.h:33
uint64 es_processed
Definition: execnodes.h:675
List * es_insert_pending_result_relations
Definition: execnodes.h:727
MemoryContext es_query_cxt
Definition: execnodes.h:671
List * es_tupleTable
Definition: execnodes.h:673
struct EPQState * es_epq_active
Definition: execnodes.h:703
CommandId es_output_cid
Definition: execnodes.h:643
List * es_insert_pending_modifytables
Definition: execnodes.h:728
Snapshot es_snapshot
Definition: execnodes.h:628
List * es_auxmodifytables
Definition: execnodes.h:688
Snapshot es_crosscheck_snapshot
Definition: execnodes.h:629
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:259
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:257
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:261
BeginForeignModify_function BeginForeignModify
Definition: fdwapi.h:231
EndForeignModify_function EndForeignModify
Definition: fdwapi.h:237
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:232
ExecForeignUpdate_function ExecForeignUpdate
Definition: fdwapi.h:235
ExecForeignBatchInsert_function ExecForeignBatchInsert
Definition: fdwapi.h:233
GetForeignModifyBatchSize_function GetForeignModifyBatchSize
Definition: fdwapi.h:234
ExecForeignDelete_function ExecForeignDelete
Definition: fdwapi.h:236
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
Definition: pg_list.h:54
MergeAction * mas_action
Definition: execnodes.h:427
ProjectionInfo * mas_proj
Definition: execnodes.h:428
ExprState * mas_whenqual
Definition: execnodes.h:430
CmdType commandType
Definition: primnodes.h:2003
MergeMatchKind matchKind
Definition: primnodes.h:2002
TM_FailureData tmfd
TupleTableSlot * planSlot
TupleTableSlot * cpUpdateReturningSlot
ModifyTableState * mtstate
CmdType operation
Definition: execnodes.h:1358
TupleTableSlot * mt_merge_pending_not_matched
Definition: execnodes.h:1412
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1362
double mt_merge_deleted
Definition: execnodes.h:1417
struct PartitionTupleRouting * mt_partition_tuple_routing
Definition: execnodes.h:1393
double mt_merge_inserted
Definition: execnodes.h:1415
TupleTableSlot * mt_root_tuple_slot
Definition: execnodes.h:1390
EPQState mt_epqstate
Definition: execnodes.h:1372
int mt_merge_subcommands
Definition: execnodes.h:1402
double mt_merge_updated
Definition: execnodes.h:1416
PlanState ps
Definition: execnodes.h:1357
HTAB * mt_resultOidHash
Definition: execnodes.h:1384
ResultRelInfo * rootResultRelInfo
Definition: execnodes.h:1370
struct TransitionCaptureState * mt_transition_capture
Definition: execnodes.h:1396
struct TransitionCaptureState * mt_oc_transition_capture
Definition: execnodes.h:1399
MergeActionState * mt_merge_action
Definition: execnodes.h:1405
List * updateColnosLists
Definition: plannodes.h:239
List * arbiterIndexes
Definition: plannodes.h:247
List * onConflictCols
Definition: plannodes.h:249
List * mergeJoinConditions
Definition: plannodes.h:255
CmdType operation
Definition: plannodes.h:233
int epqParam
Definition: plannodes.h:245
List * resultRelations
Definition: plannodes.h:238
Bitmapset * fdwDirectModifyPlans
Definition: plannodes.h:243
List * onConflictSet
Definition: plannodes.h:248
List * mergeActionLists
Definition: plannodes.h:253
bool canSetTag
Definition: plannodes.h:234
List * fdwPrivLists
Definition: plannodes.h:242
List * returningLists
Definition: plannodes.h:241
List * withCheckOptionLists
Definition: plannodes.h:240
Index rootRelation
Definition: plannodes.h:236
Node * onConflictWhere
Definition: plannodes.h:250
List * rowMarks
Definition: plannodes.h:244
OnConflictAction onConflictAction
Definition: plannodes.h:246
Definition: nodes.h:129
TupleTableSlot * oc_ProjSlot
Definition: execnodes.h:412
TupleTableSlot * oc_Existing
Definition: execnodes.h:411
ExprState * oc_WhereClause
Definition: execnodes.h:414
ProjectionInfo * oc_ProjInfo
Definition: execnodes.h:413
bool isParent
Definition: plannodes.h:1390
Plan * plan
Definition: execnodes.h:1119
EState * state
Definition: execnodes.h:1121
ExprContext * ps_ExprContext
Definition: execnodes.h:1158
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1157
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1125
List * targetlist
Definition: plannodes.h:153
ExprContext * pi_exprContext
Definition: execnodes.h:366
TriggerDesc * trigdesc
Definition: rel.h:117
TupleDesc rd_att
Definition: rel.h:112
Form_pg_class rd_rel
Definition: rel.h:111
TupleTableSlot * ri_PartitionTupleSlot
Definition: execnodes.h:587
bool ri_projectNewInfoValid
Definition: execnodes.h:485
OnConflictSetState * ri_onConflict
Definition: execnodes.h:549
int ri_NumIndices
Definition: execnodes.h:461
List * ri_onConflictArbiterIndexes
Definition: execnodes.h:546
struct ResultRelInfo * ri_RootResultRelInfo
Definition: execnodes.h:586
TupleTableSlot ** ri_Slots
Definition: execnodes.h:517
ExprState * ri_MergeJoinCondition
Definition: execnodes.h:555
Relation ri_RelationDesc
Definition: execnodes.h:458
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:464
int ri_NumSlotsInitialized
Definition: execnodes.h:515
List * ri_WithCheckOptions
Definition: execnodes.h:521
TupleTableSlot * ri_oldTupleSlot
Definition: execnodes.h:483
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:488
Bitmapset * ri_extraUpdatedCols
Definition: execnodes.h:476
Index ri_RangeTableIndex
Definition: execnodes.h:455
ExprState ** ri_GeneratedExprsI
Definition: execnodes.h:532
int ri_NumGeneratedNeededU
Definition: execnodes.h:537
List * ri_MergeActions[NUM_MERGE_MATCH_KINDS]
Definition: execnodes.h:552
TupleTableSlot * ri_newTupleSlot
Definition: execnodes.h:481
List * ri_WithCheckOptionExprs
Definition: execnodes.h:524
ProjectionInfo * ri_projectNew
Definition: execnodes.h:479
ProjectionInfo * ri_projectReturning
Definition: execnodes.h:543
ExprState ** ri_GeneratedExprsU
Definition: execnodes.h:533
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:505
List * ri_returningList
Definition: execnodes.h:540
TupleTableSlot ** ri_PlanSlots
Definition: execnodes.h:518
bool ri_usesFdwDirectModify
Definition: execnodes.h:511
AttrNumber ri_RowIdAttNo
Definition: execnodes.h:473
int ri_NumGeneratedNeededI
Definition: execnodes.h:536
int ri_BatchSize
Definition: execnodes.h:516
ItemPointerData ctid
Definition: tableam.h:150
Expr * expr
Definition: primnodes.h:2186
TupleTableSlot * tcs_original_insert_tuple
Definition: trigger.h:76
int numtriggers
Definition: reltrigger.h:50
bool trig_delete_before_row
Definition: reltrigger.h:66
bool trig_update_instead_row
Definition: reltrigger.h:63
Trigger * triggers
Definition: reltrigger.h:49
bool trig_delete_instead_row
Definition: reltrigger.h:68
bool trig_update_after_row
Definition: reltrigger.h:62
bool trig_insert_instead_row
Definition: reltrigger.h:58
bool trig_update_before_row
Definition: reltrigger.h:61
bool trig_insert_before_row
Definition: reltrigger.h:56
Oid tgfoid
Definition: reltrigger.h:28
bool tgisclone
Definition: reltrigger.h:32
bool has_generated_stored
Definition: tupdesc.h:45
AttrMap * attrMap
Definition: tupconvert.h:28
TupleConstr * constr
Definition: tupdesc.h:85
Oid tts_tableOid
Definition: tuptable.h:130
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:123
const TupleTableSlotOps *const tts_ops
Definition: tuptable.h:121
bool * tts_isnull
Definition: tuptable.h:127
Datum * tts_values
Definition: tuptable.h:125
TU_UpdateIndexes updateIndexes
LockTupleMode lockmode
#define MinTransactionIdAttributeNumber
Definition: sysattr.h:22
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:91
TU_UpdateIndexes
Definition: tableam.h:118
@ TU_Summarizing
Definition: tableam.h:126
@ TU_None
Definition: tableam.h:120
TM_Result
Definition: tableam.h:80
@ TM_Ok
Definition: tableam.h:85
@ TM_BeingModified
Definition: tableam.h:107
@ TM_Deleted
Definition: tableam.h:100
@ TM_WouldBlock
Definition: tableam.h:110
@ TM_Updated
Definition: tableam.h:97
@ TM_SelfModified
Definition: tableam.h:91
@ TM_Invisible
Definition: tableam.h:88
static TM_Result table_tuple_lock(Relation rel, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, uint8 flags, TM_FailureData *tmfd)
Definition: tableam.h:1580
static void table_tuple_complete_speculative(Relation rel, TupleTableSlot *slot, uint32 specToken, bool succeeded)
Definition: tableam.h:1435
static TM_Result table_tuple_update(Relation rel, ItemPointer otid, TupleTableSlot *slot, CommandId cid, Snapshot snapshot, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
Definition: tableam.h:1535
static TM_Result table_tuple_delete(Relation rel, ItemPointer tid, CommandId cid, Snapshot snapshot, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, bool changingPart)
Definition: tableam.h:1491
#define TUPLE_LOCK_FLAG_FIND_LAST_VERSION
Definition: tableam.h:268
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
Definition: tableam.h:1402
static void table_tuple_insert_speculative(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate, uint32 specToken)
Definition: tableam.h:1421
static bool table_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot, Snapshot snapshot)
Definition: tableam.h:1335
static bool table_tuple_fetch_row_version(Relation rel, ItemPointer tid, Snapshot snapshot, TupleTableSlot *slot)
Definition: tableam.h:1288
bool ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, TM_Result *tmresult, TM_FailureData *tmfd)
Definition: trigger.c:2934
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:2774
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2394
bool ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2458
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2811
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2612
bool ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2551
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ResultRelInfo *src_partinfo, ResultRelInfo *dst_partinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *newslot, List *recheckIndexes, TransitionCaptureState *transition_capture, bool is_crosspart_update)
Definition: trigger.c:3097
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2916
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2663
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot **epqslot, TM_Result *tmresult, TM_FailureData *tmfd)
Definition: trigger.c:2683
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2534
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4884
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2445
bool ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *newslot)
Definition: trigger.c:3156
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2858
#define RI_TRIGGER_PK
Definition: trigger.h:282
TupleTableSlot * execute_attr_map_slot(AttrMap *attrMap, TupleTableSlot *in_slot, TupleTableSlot *out_slot)
Definition: tupconvert.c:192
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454
#define TTS_EMPTY(slot)
Definition: tuptable.h:96
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: tuptable.h:509
static Datum slot_getsysattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:416
#define TupIsNull(slot)
Definition: tuptable.h:306
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:368
static void ExecMaterializeSlot(TupleTableSlot *slot)
Definition: tuptable.h:472
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:296
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:940
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:453
#define IsolationUsesXactSnapshot()
Definition: xact.h:51