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-2018, 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  * Each ModifyTable node contains a list of one or more subplans,
23  * much like an Append node. There is one subplan per result relation.
24  * The key reason for this is that in an inherited UPDATE command, each
25  * result relation could have a different schema (more or different
26  * columns) requiring a different plan tree to produce it. In an
27  * inherited DELETE, all the subplans should produce the same output
28  * rowtype, but we might still find that different plans are appropriate
29  * for different child relations.
30  *
31  * If the query specifies RETURNING, then the ModifyTable returns a
32  * RETURNING tuple after completing each row insert, update, or delete.
33  * It must be called again to continue the operation. Without RETURNING,
34  * we just loop within the node until all the work is done, then
35  * return NULL. This avoids useless call/return overhead.
36  */
37 
38 #include "postgres.h"
39 
40 #include "access/htup_details.h"
41 #include "access/xact.h"
42 #include "commands/trigger.h"
43 #include "executor/execPartition.h"
44 #include "executor/executor.h"
46 #include "foreign/fdwapi.h"
47 #include "miscadmin.h"
48 #include "nodes/nodeFuncs.h"
49 #include "storage/bufmgr.h"
50 #include "storage/lmgr.h"
51 #include "utils/builtins.h"
52 #include "utils/memutils.h"
53 #include "utils/rel.h"
54 #include "utils/tqual.h"
55 
56 
57 static bool ExecOnConflictUpdate(ModifyTableState *mtstate,
58  ResultRelInfo *resultRelInfo,
59  ItemPointer conflictTid,
60  TupleTableSlot *planSlot,
61  TupleTableSlot *excludedSlot,
62  EState *estate,
63  bool canSetTag,
64  TupleTableSlot **returning);
66  EState *estate,
67  PartitionTupleRouting *proute,
68  ResultRelInfo *targetRelInfo,
69  TupleTableSlot *slot);
74  int whichplan);
75 
76 /*
77  * Verify that the tuples to be produced by INSERT or UPDATE match the
78  * target relation's rowtype
79  *
80  * We do this to guard against stale plans. If plan invalidation is
81  * functioning properly then we should never get a failure here, but better
82  * safe than sorry. Note that this is called after we have obtained lock
83  * on the target rel, so the rowtype can't change underneath us.
84  *
85  * The plan output is represented by its targetlist, because that makes
86  * handling the dropped-column case easier.
87  */
88 static void
89 ExecCheckPlanOutput(Relation resultRel, List *targetList)
90 {
91  TupleDesc resultDesc = RelationGetDescr(resultRel);
92  int attno = 0;
93  ListCell *lc;
94 
95  foreach(lc, targetList)
96  {
97  TargetEntry *tle = (TargetEntry *) lfirst(lc);
98  Form_pg_attribute attr;
99 
100  if (tle->resjunk)
101  continue; /* ignore junk tlist items */
102 
103  if (attno >= resultDesc->natts)
104  ereport(ERROR,
105  (errcode(ERRCODE_DATATYPE_MISMATCH),
106  errmsg("table row type and query-specified row type do not match"),
107  errdetail("Query has too many columns.")));
108  attr = TupleDescAttr(resultDesc, attno);
109  attno++;
110 
111  if (!attr->attisdropped)
112  {
113  /* Normal case: demand type match */
114  if (exprType((Node *) tle->expr) != attr->atttypid)
115  ereport(ERROR,
116  (errcode(ERRCODE_DATATYPE_MISMATCH),
117  errmsg("table row type and query-specified row type do not match"),
118  errdetail("Table has type %s at ordinal position %d, but query expects %s.",
119  format_type_be(attr->atttypid),
120  attno,
121  format_type_be(exprType((Node *) tle->expr)))));
122  }
123  else
124  {
125  /*
126  * For a dropped column, we can't check atttypid (it's likely 0).
127  * In any case the planner has most likely inserted an INT4 null.
128  * What we insist on is just *some* NULL constant.
129  */
130  if (!IsA(tle->expr, Const) ||
131  !((Const *) tle->expr)->constisnull)
132  ereport(ERROR,
133  (errcode(ERRCODE_DATATYPE_MISMATCH),
134  errmsg("table row type and query-specified row type do not match"),
135  errdetail("Query provides a value for a dropped column at ordinal position %d.",
136  attno)));
137  }
138  }
139  if (attno != resultDesc->natts)
140  ereport(ERROR,
141  (errcode(ERRCODE_DATATYPE_MISMATCH),
142  errmsg("table row type and query-specified row type do not match"),
143  errdetail("Query has too few columns.")));
144 }
145 
146 /*
147  * ExecProcessReturning --- evaluate a RETURNING list
148  *
149  * resultRelInfo: current result rel
150  * tupleSlot: slot holding tuple actually inserted/updated/deleted
151  * planSlot: slot holding tuple returned by top subplan node
152  *
153  * Note: If tupleSlot is NULL, the FDW should have already provided econtext's
154  * scan tuple.
155  *
156  * Returns a slot holding the result tuple
157  */
158 static TupleTableSlot *
160  TupleTableSlot *tupleSlot,
161  TupleTableSlot *planSlot)
162 {
163  ProjectionInfo *projectReturning = resultRelInfo->ri_projectReturning;
164  ExprContext *econtext = projectReturning->pi_exprContext;
165 
166  /*
167  * Reset per-tuple memory context to free any expression evaluation
168  * storage allocated in the previous cycle.
169  */
170  ResetExprContext(econtext);
171 
172  /* Make tuple and any needed join variables available to ExecProject */
173  if (tupleSlot)
174  econtext->ecxt_scantuple = tupleSlot;
175  else
176  {
177  HeapTuple tuple;
178 
179  /*
180  * RETURNING expressions might reference the tableoid column, so
181  * initialize t_tableOid before evaluating them.
182  */
183  Assert(!TupIsNull(econtext->ecxt_scantuple));
184  tuple = ExecMaterializeSlot(econtext->ecxt_scantuple);
185  tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
186  }
187  econtext->ecxt_outertuple = planSlot;
188 
189  /* Compute the RETURNING expressions */
190  return ExecProject(projectReturning);
191 }
192 
193 /*
194  * ExecCheckHeapTupleVisible -- verify heap tuple is visible
195  *
196  * It would not be consistent with guarantees of the higher isolation levels to
197  * proceed with avoiding insertion (taking speculative insertion's alternative
198  * path) on the basis of another tuple that is not visible to MVCC snapshot.
199  * Check for the need to raise a serialization failure, and do so as necessary.
200  */
201 static void
203  HeapTuple tuple,
204  Buffer buffer)
205 {
207  return;
208 
209  /*
210  * We need buffer pin and lock to call HeapTupleSatisfiesVisibility.
211  * Caller should be holding pin, but not lock.
212  */
213  LockBuffer(buffer, BUFFER_LOCK_SHARE);
214  if (!HeapTupleSatisfiesVisibility(tuple, estate->es_snapshot, buffer))
215  {
216  /*
217  * We should not raise a serialization failure if the conflict is
218  * against a tuple inserted by our own transaction, even if it's not
219  * visible to our snapshot. (This would happen, for example, if
220  * conflicting keys are proposed for insertion in a single command.)
221  */
223  ereport(ERROR,
224  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
225  errmsg("could not serialize access due to concurrent update")));
226  }
228 }
229 
230 /*
231  * ExecCheckTIDVisible -- convenience variant of ExecCheckHeapTupleVisible()
232  */
233 static void
235  ResultRelInfo *relinfo,
236  ItemPointer tid)
237 {
238  Relation rel = relinfo->ri_RelationDesc;
239  Buffer buffer;
240  HeapTupleData tuple;
241 
242  /* Redundantly check isolation level */
244  return;
245 
246  tuple.t_self = *tid;
247  if (!heap_fetch(rel, SnapshotAny, &tuple, &buffer, false, NULL))
248  elog(ERROR, "failed to fetch conflicting tuple for ON CONFLICT");
249  ExecCheckHeapTupleVisible(estate, &tuple, buffer);
250  ReleaseBuffer(buffer);
251 }
252 
253 /* ----------------------------------------------------------------
254  * ExecInsert
255  *
256  * For INSERT, we have to insert the tuple into the target relation
257  * and insert appropriate tuples into the index relations.
258  *
259  * Returns RETURNING result if any, otherwise NULL.
260  * ----------------------------------------------------------------
261  */
262 static TupleTableSlot *
264  TupleTableSlot *slot,
265  TupleTableSlot *planSlot,
266  EState *estate,
267  bool canSetTag)
268 {
269  HeapTuple tuple;
270  ResultRelInfo *resultRelInfo;
271  Relation resultRelationDesc;
272  Oid newId;
273  List *recheckIndexes = NIL;
274  TupleTableSlot *result = NULL;
275  TransitionCaptureState *ar_insert_trig_tcs;
276  ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
277  OnConflictAction onconflict = node->onConflictAction;
278 
279  /*
280  * get the heap tuple out of the tuple table slot, making sure we have a
281  * writable copy
282  */
283  tuple = ExecMaterializeSlot(slot);
284 
285  /*
286  * get information on the (current) result relation
287  */
288  resultRelInfo = estate->es_result_relation_info;
289  resultRelationDesc = resultRelInfo->ri_RelationDesc;
290 
291  /*
292  * If the result relation has OIDs, force the tuple's OID to zero so that
293  * heap_insert will assign a fresh OID. Usually the OID already will be
294  * zero at this point, but there are corner cases where the plan tree can
295  * return a tuple extracted literally from some table with the same
296  * rowtype.
297  *
298  * XXX if we ever wanted to allow users to assign their own OIDs to new
299  * rows, this'd be the place to do it. For the moment, we make a point of
300  * doing this before calling triggers, so that a user-supplied trigger
301  * could hack the OID if desired.
302  */
303  if (resultRelationDesc->rd_rel->relhasoids)
304  HeapTupleSetOid(tuple, InvalidOid);
305 
306  /*
307  * BEFORE ROW INSERT Triggers.
308  *
309  * Note: We fire BEFORE ROW TRIGGERS for every attempted insertion in an
310  * INSERT ... ON CONFLICT statement. We cannot check for constraint
311  * violations before firing these triggers, because they can change the
312  * values to insert. Also, they can run arbitrary user-defined code with
313  * side-effects that we can't cancel by just not inserting the tuple.
314  */
315  if (resultRelInfo->ri_TrigDesc &&
316  resultRelInfo->ri_TrigDesc->trig_insert_before_row)
317  {
318  slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
319 
320  if (slot == NULL) /* "do nothing" */
321  return NULL;
322 
323  /* trigger might have changed tuple */
324  tuple = ExecMaterializeSlot(slot);
325  }
326 
327  /* INSTEAD OF ROW INSERT Triggers */
328  if (resultRelInfo->ri_TrigDesc &&
329  resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
330  {
331  slot = ExecIRInsertTriggers(estate, resultRelInfo, slot);
332 
333  if (slot == NULL) /* "do nothing" */
334  return NULL;
335 
336  /* trigger might have changed tuple */
337  tuple = ExecMaterializeSlot(slot);
338 
339  newId = InvalidOid;
340  }
341  else if (resultRelInfo->ri_FdwRoutine)
342  {
343  /*
344  * insert into foreign table: let the FDW do it
345  */
346  slot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
347  resultRelInfo,
348  slot,
349  planSlot);
350 
351  if (slot == NULL) /* "do nothing" */
352  return NULL;
353 
354  /* FDW might have changed tuple */
355  tuple = ExecMaterializeSlot(slot);
356 
357  /*
358  * AFTER ROW Triggers or RETURNING expressions might reference the
359  * tableoid column, so initialize t_tableOid before evaluating them.
360  */
361  tuple->t_tableOid = RelationGetRelid(resultRelationDesc);
362 
363  newId = InvalidOid;
364  }
365  else
366  {
367  WCOKind wco_kind;
368  bool check_partition_constr;
369 
370  /*
371  * We always check the partition constraint, including when the tuple
372  * got here via tuple-routing. However we don't need to in the latter
373  * case if no BR trigger is defined on the partition. Note that a BR
374  * trigger might modify the tuple such that the partition constraint
375  * is no longer satisfied, so we need to check in that case.
376  */
377  check_partition_constr = (resultRelInfo->ri_PartitionCheck != NIL);
378 
379  /*
380  * Constraints might reference the tableoid column, so initialize
381  * t_tableOid before evaluating them.
382  */
383  tuple->t_tableOid = RelationGetRelid(resultRelationDesc);
384 
385  /*
386  * Check any RLS WITH CHECK policies.
387  *
388  * Normally we should check INSERT policies. But if the insert is the
389  * result of a partition key update that moved the tuple to a new
390  * partition, we should instead check UPDATE policies, because we are
391  * executing policies defined on the target table, and not those
392  * defined on the child partitions.
393  */
394  wco_kind = (mtstate->operation == CMD_UPDATE) ?
396 
397  /*
398  * ExecWithCheckOptions() will skip any WCOs which are not of the kind
399  * we are looking for at this point.
400  */
401  if (resultRelInfo->ri_WithCheckOptions != NIL)
402  ExecWithCheckOptions(wco_kind, resultRelInfo, slot, estate);
403 
404  /*
405  * No need though if the tuple has been routed, and a BR trigger
406  * doesn't exist.
407  */
408  if (resultRelInfo->ri_PartitionRoot != NULL &&
409  !(resultRelInfo->ri_TrigDesc &&
410  resultRelInfo->ri_TrigDesc->trig_insert_before_row))
411  check_partition_constr = false;
412 
413  /* Check the constraints of the tuple */
414  if (resultRelationDesc->rd_att->constr || check_partition_constr)
415  ExecConstraints(resultRelInfo, slot, estate, true);
416 
417  if (onconflict != ONCONFLICT_NONE && resultRelInfo->ri_NumIndices > 0)
418  {
419  /* Perform a speculative insertion. */
420  uint32 specToken;
421  ItemPointerData conflictTid;
422  bool specConflict;
423  List *arbiterIndexes;
424 
425  arbiterIndexes = resultRelInfo->ri_onConflictArbiterIndexes;
426 
427  /*
428  * Do a non-conclusive check for conflicts first.
429  *
430  * We're not holding any locks yet, so this doesn't guarantee that
431  * the later insert won't conflict. But it avoids leaving behind
432  * a lot of canceled speculative insertions, if you run a lot of
433  * INSERT ON CONFLICT statements that do conflict.
434  *
435  * We loop back here if we find a conflict below, either during
436  * the pre-check, or when we re-check after inserting the tuple
437  * speculatively.
438  */
439  vlock:
440  specConflict = false;
441  if (!ExecCheckIndexConstraints(slot, estate, &conflictTid,
442  arbiterIndexes))
443  {
444  /* committed conflict tuple found */
445  if (onconflict == ONCONFLICT_UPDATE)
446  {
447  /*
448  * In case of ON CONFLICT DO UPDATE, execute the UPDATE
449  * part. Be prepared to retry if the UPDATE fails because
450  * of another concurrent UPDATE/DELETE to the conflict
451  * tuple.
452  */
453  TupleTableSlot *returning = NULL;
454 
455  if (ExecOnConflictUpdate(mtstate, resultRelInfo,
456  &conflictTid, planSlot, slot,
457  estate, canSetTag, &returning))
458  {
459  InstrCountTuples2(&mtstate->ps, 1);
460  return returning;
461  }
462  else
463  goto vlock;
464  }
465  else
466  {
467  /*
468  * In case of ON CONFLICT DO NOTHING, do nothing. However,
469  * verify that the tuple is visible to the executor's MVCC
470  * snapshot at higher isolation levels.
471  */
472  Assert(onconflict == ONCONFLICT_NOTHING);
473  ExecCheckTIDVisible(estate, resultRelInfo, &conflictTid);
474  InstrCountTuples2(&mtstate->ps, 1);
475  return NULL;
476  }
477  }
478 
479  /*
480  * Before we start insertion proper, acquire our "speculative
481  * insertion lock". Others can use that to wait for us to decide
482  * if we're going to go ahead with the insertion, instead of
483  * waiting for the whole transaction to complete.
484  */
486  HeapTupleHeaderSetSpeculativeToken(tuple->t_data, specToken);
487 
488  /* insert the tuple, with the speculative token */
489  newId = heap_insert(resultRelationDesc, tuple,
490  estate->es_output_cid,
492  NULL);
493 
494  /* insert index entries for tuple */
495  recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
496  estate, true, &specConflict,
497  arbiterIndexes);
498 
499  /* adjust the tuple's state accordingly */
500  if (!specConflict)
501  heap_finish_speculative(resultRelationDesc, tuple);
502  else
503  heap_abort_speculative(resultRelationDesc, tuple);
504 
505  /*
506  * Wake up anyone waiting for our decision. They will re-check
507  * the tuple, see that it's no longer speculative, and wait on our
508  * XID as if this was a regularly inserted tuple all along. Or if
509  * we killed the tuple, they will see it's dead, and proceed as if
510  * the tuple never existed.
511  */
513 
514  /*
515  * If there was a conflict, start from the beginning. We'll do
516  * the pre-check again, which will now find the conflicting tuple
517  * (unless it aborts before we get there).
518  */
519  if (specConflict)
520  {
521  list_free(recheckIndexes);
522  goto vlock;
523  }
524 
525  /* Since there was no insertion conflict, we're done */
526  }
527  else
528  {
529  /*
530  * insert the tuple normally.
531  *
532  * Note: heap_insert returns the tid (location) of the new tuple
533  * in the t_self field.
534  */
535  newId = heap_insert(resultRelationDesc, tuple,
536  estate->es_output_cid,
537  0, NULL);
538 
539  /* insert index entries for tuple */
540  if (resultRelInfo->ri_NumIndices > 0)
541  recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
542  estate, false, NULL,
543  NIL);
544  }
545  }
546 
547  if (canSetTag)
548  {
549  (estate->es_processed)++;
550  estate->es_lastoid = newId;
551  setLastTid(&(tuple->t_self));
552  }
553 
554  /*
555  * If this insert is the result of a partition key update that moved the
556  * tuple to a new partition, put this row into the transition NEW TABLE,
557  * if there is one. We need to do this separately for DELETE and INSERT
558  * because they happen on different tables.
559  */
560  ar_insert_trig_tcs = mtstate->mt_transition_capture;
561  if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture
563  {
564  ExecARUpdateTriggers(estate, resultRelInfo, NULL,
565  NULL,
566  tuple,
567  NULL,
568  mtstate->mt_transition_capture);
569 
570  /*
571  * We've already captured the NEW TABLE row, so make sure any AR
572  * INSERT trigger fired below doesn't capture it again.
573  */
574  ar_insert_trig_tcs = NULL;
575  }
576 
577  /* AFTER ROW INSERT Triggers */
578  ExecARInsertTriggers(estate, resultRelInfo, tuple, recheckIndexes,
579  ar_insert_trig_tcs);
580 
581  list_free(recheckIndexes);
582 
583  /*
584  * Check any WITH CHECK OPTION constraints from parent views. We are
585  * required to do this after testing all constraints and uniqueness
586  * violations per the SQL spec, so we do it after actually inserting the
587  * record into the heap and all indexes.
588  *
589  * ExecWithCheckOptions will elog(ERROR) if a violation is found, so the
590  * tuple will never be seen, if it violates the WITH CHECK OPTION.
591  *
592  * ExecWithCheckOptions() will skip any WCOs which are not of the kind we
593  * are looking for at this point.
594  */
595  if (resultRelInfo->ri_WithCheckOptions != NIL)
596  ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);
597 
598  /* Process RETURNING if present */
599  if (resultRelInfo->ri_projectReturning)
600  result = ExecProcessReturning(resultRelInfo, slot, planSlot);
601 
602  return result;
603 }
604 
605 /* ----------------------------------------------------------------
606  * ExecDelete
607  *
608  * DELETE is like UPDATE, except that we delete the tuple and no
609  * index modifications are needed.
610  *
611  * When deleting from a table, tupleid identifies the tuple to
612  * delete and oldtuple is NULL. When deleting from a view,
613  * oldtuple is passed to the INSTEAD OF triggers and identifies
614  * what to delete, and tupleid is invalid. When deleting from a
615  * foreign table, tupleid is invalid; the FDW has to figure out
616  * which row to delete using data from the planSlot. oldtuple is
617  * passed to foreign table triggers; it is NULL when the foreign
618  * table has no relevant triggers.
619  *
620  * Returns RETURNING result if any, otherwise NULL.
621  * ----------------------------------------------------------------
622  */
623 static TupleTableSlot *
625  ItemPointer tupleid,
626  HeapTuple oldtuple,
627  TupleTableSlot *planSlot,
628  EPQState *epqstate,
629  EState *estate,
630  bool *tupleDeleted,
631  bool processReturning,
632  bool canSetTag,
633  bool changingPart)
634 {
635  ResultRelInfo *resultRelInfo;
636  Relation resultRelationDesc;
637  HTSU_Result result;
639  TupleTableSlot *slot = NULL;
640  TransitionCaptureState *ar_delete_trig_tcs;
641 
642  if (tupleDeleted)
643  *tupleDeleted = false;
644 
645  /*
646  * get information on the (current) result relation
647  */
648  resultRelInfo = estate->es_result_relation_info;
649  resultRelationDesc = resultRelInfo->ri_RelationDesc;
650 
651  /* BEFORE ROW DELETE Triggers */
652  if (resultRelInfo->ri_TrigDesc &&
653  resultRelInfo->ri_TrigDesc->trig_delete_before_row)
654  {
655  bool dodelete;
656 
657  dodelete = ExecBRDeleteTriggers(estate, epqstate, resultRelInfo,
658  tupleid, oldtuple);
659 
660  if (!dodelete) /* "do nothing" */
661  return NULL;
662  }
663 
664  /* INSTEAD OF ROW DELETE Triggers */
665  if (resultRelInfo->ri_TrigDesc &&
666  resultRelInfo->ri_TrigDesc->trig_delete_instead_row)
667  {
668  bool dodelete;
669 
670  Assert(oldtuple != NULL);
671  dodelete = ExecIRDeleteTriggers(estate, resultRelInfo, oldtuple);
672 
673  if (!dodelete) /* "do nothing" */
674  return NULL;
675  }
676  else if (resultRelInfo->ri_FdwRoutine)
677  {
678  HeapTuple tuple;
679 
680  /*
681  * delete from foreign table: let the FDW do it
682  *
683  * We offer the trigger tuple slot as a place to store RETURNING data,
684  * although the FDW can return some other slot if it wants. Set up
685  * the slot's tupdesc so the FDW doesn't need to do that for itself.
686  */
687  slot = estate->es_trig_tuple_slot;
688  if (slot->tts_tupleDescriptor != RelationGetDescr(resultRelationDesc))
689  ExecSetSlotDescriptor(slot, RelationGetDescr(resultRelationDesc));
690 
691  slot = resultRelInfo->ri_FdwRoutine->ExecForeignDelete(estate,
692  resultRelInfo,
693  slot,
694  planSlot);
695 
696  if (slot == NULL) /* "do nothing" */
697  return NULL;
698 
699  /*
700  * RETURNING expressions might reference the tableoid column, so
701  * initialize t_tableOid before evaluating them.
702  */
703  if (slot->tts_isempty)
704  ExecStoreAllNullTuple(slot);
705  tuple = ExecMaterializeSlot(slot);
706  tuple->t_tableOid = RelationGetRelid(resultRelationDesc);
707  }
708  else
709  {
710  /*
711  * delete the tuple
712  *
713  * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check
714  * that the row to be deleted is visible to that snapshot, and throw a
715  * can't-serialize error if not. This is a special-case behavior
716  * needed for referential integrity updates in transaction-snapshot
717  * mode transactions.
718  */
719 ldelete:;
720  result = heap_delete(resultRelationDesc, tupleid,
721  estate->es_output_cid,
722  estate->es_crosscheck_snapshot,
723  true /* wait for commit */ ,
724  &hufd,
725  changingPart);
726  switch (result)
727  {
729 
730  /*
731  * The target tuple was already updated or deleted by the
732  * current command, or by a later command in the current
733  * transaction. The former case is possible in a join DELETE
734  * where multiple tuples join to the same target tuple. This
735  * is somewhat questionable, but Postgres has always allowed
736  * it: we just ignore additional deletion attempts.
737  *
738  * The latter case arises if the tuple is modified by a
739  * command in a BEFORE trigger, or perhaps by a command in a
740  * volatile function used in the query. In such situations we
741  * should not ignore the deletion, but it is equally unsafe to
742  * proceed. We don't want to discard the original DELETE
743  * while keeping the triggered actions based on its deletion;
744  * and it would be no better to allow the original DELETE
745  * while discarding updates that it triggered. The row update
746  * carries some information that might be important according
747  * to business rules; so throwing an error is the only safe
748  * course.
749  *
750  * If a trigger actually intends this type of interaction, it
751  * can re-execute the DELETE and then return NULL to cancel
752  * the outer delete.
753  */
754  if (hufd.cmax != estate->es_output_cid)
755  ereport(ERROR,
756  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
757  errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
758  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
759 
760  /* Else, already deleted by self; nothing to do */
761  return NULL;
762 
764  break;
765 
766  case HeapTupleUpdated:
768  ereport(ERROR,
769  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
770  errmsg("could not serialize access due to concurrent update")));
772  ereport(ERROR,
773  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
774  errmsg("tuple to be deleted was already moved to another partition due to concurrent update")));
775 
776  if (!ItemPointerEquals(tupleid, &hufd.ctid))
777  {
778  TupleTableSlot *epqslot;
779 
780  epqslot = EvalPlanQual(estate,
781  epqstate,
782  resultRelationDesc,
783  resultRelInfo->ri_RangeTableIndex,
785  &hufd.ctid,
786  hufd.xmax);
787  if (!TupIsNull(epqslot))
788  {
789  *tupleid = hufd.ctid;
790  goto ldelete;
791  }
792  }
793  /* tuple already deleted; nothing to do */
794  return NULL;
795 
796  default:
797  elog(ERROR, "unrecognized heap_delete status: %u", result);
798  return NULL;
799  }
800 
801  /*
802  * Note: Normally one would think that we have to delete index tuples
803  * associated with the heap tuple now...
804  *
805  * ... but in POSTGRES, we have no need to do this because VACUUM will
806  * take care of it later. We can't delete index tuples immediately
807  * anyway, since the tuple is still visible to other transactions.
808  */
809  }
810 
811  if (canSetTag)
812  (estate->es_processed)++;
813 
814  /* Tell caller that the delete actually happened. */
815  if (tupleDeleted)
816  *tupleDeleted = true;
817 
818  /*
819  * If this delete is the result of a partition key update that moved the
820  * tuple to a new partition, put this row into the transition OLD TABLE,
821  * if there is one. We need to do this separately for DELETE and INSERT
822  * because they happen on different tables.
823  */
824  ar_delete_trig_tcs = mtstate->mt_transition_capture;
825  if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture
827  {
828  ExecARUpdateTriggers(estate, resultRelInfo,
829  tupleid,
830  oldtuple,
831  NULL,
832  NULL,
833  mtstate->mt_transition_capture);
834 
835  /*
836  * We've already captured the NEW TABLE row, so make sure any AR
837  * DELETE trigger fired below doesn't capture it again.
838  */
839  ar_delete_trig_tcs = NULL;
840  }
841 
842  /* AFTER ROW DELETE Triggers */
843  ExecARDeleteTriggers(estate, resultRelInfo, tupleid, oldtuple,
844  ar_delete_trig_tcs);
845 
846  /* Process RETURNING if present and if requested */
847  if (processReturning && resultRelInfo->ri_projectReturning)
848  {
849  /*
850  * We have to put the target tuple into a slot, which means first we
851  * gotta fetch it. We can use the trigger tuple slot.
852  */
853  TupleTableSlot *rslot;
854  HeapTupleData deltuple;
855  Buffer delbuffer;
856 
857  if (resultRelInfo->ri_FdwRoutine)
858  {
859  /* FDW must have provided a slot containing the deleted row */
860  Assert(!TupIsNull(slot));
861  delbuffer = InvalidBuffer;
862  }
863  else
864  {
865  slot = estate->es_trig_tuple_slot;
866  if (oldtuple != NULL)
867  {
868  deltuple = *oldtuple;
869  delbuffer = InvalidBuffer;
870  }
871  else
872  {
873  deltuple.t_self = *tupleid;
874  if (!heap_fetch(resultRelationDesc, SnapshotAny,
875  &deltuple, &delbuffer, false, NULL))
876  elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
877  }
878 
879  if (slot->tts_tupleDescriptor != RelationGetDescr(resultRelationDesc))
880  ExecSetSlotDescriptor(slot, RelationGetDescr(resultRelationDesc));
881  ExecStoreTuple(&deltuple, slot, InvalidBuffer, false);
882  }
883 
884  rslot = ExecProcessReturning(resultRelInfo, slot, planSlot);
885 
886  /*
887  * Before releasing the target tuple again, make sure rslot has a
888  * local copy of any pass-by-reference values.
889  */
890  ExecMaterializeSlot(rslot);
891 
892  ExecClearTuple(slot);
893  if (BufferIsValid(delbuffer))
894  ReleaseBuffer(delbuffer);
895 
896  return rslot;
897  }
898 
899  return NULL;
900 }
901 
902 /* ----------------------------------------------------------------
903  * ExecUpdate
904  *
905  * note: we can't run UPDATE queries with transactions
906  * off because UPDATEs are actually INSERTs and our
907  * scan will mistakenly loop forever, updating the tuple
908  * it just inserted.. This should be fixed but until it
909  * is, we don't want to get stuck in an infinite loop
910  * which corrupts your database..
911  *
912  * When updating a table, tupleid identifies the tuple to
913  * update and oldtuple is NULL. When updating a view, oldtuple
914  * is passed to the INSTEAD OF triggers and identifies what to
915  * update, and tupleid is invalid. When updating a foreign table,
916  * tupleid is invalid; the FDW has to figure out which row to
917  * update using data from the planSlot. oldtuple is passed to
918  * foreign table triggers; it is NULL when the foreign table has
919  * no relevant triggers.
920  *
921  * Returns RETURNING result if any, otherwise NULL.
922  * ----------------------------------------------------------------
923  */
924 static TupleTableSlot *
926  ItemPointer tupleid,
927  HeapTuple oldtuple,
928  TupleTableSlot *slot,
929  TupleTableSlot *planSlot,
930  EPQState *epqstate,
931  EState *estate,
932  bool canSetTag)
933 {
934  HeapTuple tuple;
935  ResultRelInfo *resultRelInfo;
936  Relation resultRelationDesc;
937  HTSU_Result result;
939  List *recheckIndexes = NIL;
940  TupleConversionMap *saved_tcs_map = NULL;
941 
942  /*
943  * abort the operation if not running transactions
944  */
946  elog(ERROR, "cannot UPDATE during bootstrap");
947 
948  /*
949  * get the heap tuple out of the tuple table slot, making sure we have a
950  * writable copy
951  */
952  tuple = ExecMaterializeSlot(slot);
953 
954  /*
955  * get information on the (current) result relation
956  */
957  resultRelInfo = estate->es_result_relation_info;
958  resultRelationDesc = resultRelInfo->ri_RelationDesc;
959 
960  /* BEFORE ROW UPDATE Triggers */
961  if (resultRelInfo->ri_TrigDesc &&
962  resultRelInfo->ri_TrigDesc->trig_update_before_row)
963  {
964  slot = ExecBRUpdateTriggers(estate, epqstate, resultRelInfo,
965  tupleid, oldtuple, slot);
966 
967  if (slot == NULL) /* "do nothing" */
968  return NULL;
969 
970  /* trigger might have changed tuple */
971  tuple = ExecMaterializeSlot(slot);
972  }
973 
974  /* INSTEAD OF ROW UPDATE Triggers */
975  if (resultRelInfo->ri_TrigDesc &&
976  resultRelInfo->ri_TrigDesc->trig_update_instead_row)
977  {
978  slot = ExecIRUpdateTriggers(estate, resultRelInfo,
979  oldtuple, slot);
980 
981  if (slot == NULL) /* "do nothing" */
982  return NULL;
983 
984  /* trigger might have changed tuple */
985  tuple = ExecMaterializeSlot(slot);
986  }
987  else if (resultRelInfo->ri_FdwRoutine)
988  {
989  /*
990  * update in foreign table: let the FDW do it
991  */
992  slot = resultRelInfo->ri_FdwRoutine->ExecForeignUpdate(estate,
993  resultRelInfo,
994  slot,
995  planSlot);
996 
997  if (slot == NULL) /* "do nothing" */
998  return NULL;
999 
1000  /* FDW might have changed tuple */
1001  tuple = ExecMaterializeSlot(slot);
1002 
1003  /*
1004  * AFTER ROW Triggers or RETURNING expressions might reference the
1005  * tableoid column, so initialize t_tableOid before evaluating them.
1006  */
1007  tuple->t_tableOid = RelationGetRelid(resultRelationDesc);
1008  }
1009  else
1010  {
1011  LockTupleMode lockmode;
1012  bool partition_constraint_failed;
1013 
1014  /*
1015  * Constraints might reference the tableoid column, so initialize
1016  * t_tableOid before evaluating them.
1017  */
1018  tuple->t_tableOid = RelationGetRelid(resultRelationDesc);
1019 
1020  /*
1021  * Check any RLS UPDATE WITH CHECK policies
1022  *
1023  * If we generate a new candidate tuple after EvalPlanQual testing, we
1024  * must loop back here and recheck any RLS policies and constraints.
1025  * (We don't need to redo triggers, however. If there are any BEFORE
1026  * triggers then trigger.c will have done heap_lock_tuple to lock the
1027  * correct tuple, so there's no need to do them again.)
1028  */
1029 lreplace:;
1030 
1031  /*
1032  * If partition constraint fails, this row might get moved to another
1033  * partition, in which case we should check the RLS CHECK policy just
1034  * before inserting into the new partition, rather than doing it here.
1035  * This is because a trigger on that partition might again change the
1036  * row. So skip the WCO checks if the partition constraint fails.
1037  */
1038  partition_constraint_failed =
1039  resultRelInfo->ri_PartitionCheck &&
1040  !ExecPartitionCheck(resultRelInfo, slot, estate);
1041 
1042  if (!partition_constraint_failed &&
1043  resultRelInfo->ri_WithCheckOptions != NIL)
1044  {
1045  /*
1046  * ExecWithCheckOptions() will skip any WCOs which are not of the
1047  * kind we are looking for at this point.
1048  */
1050  resultRelInfo, slot, estate);
1051  }
1052 
1053  /*
1054  * If a partition check failed, try to move the row into the right
1055  * partition.
1056  */
1057  if (partition_constraint_failed)
1058  {
1059  bool tuple_deleted;
1060  TupleTableSlot *ret_slot;
1062  int map_index;
1063  TupleConversionMap *tupconv_map;
1064 
1065  /*
1066  * Disallow an INSERT ON CONFLICT DO UPDATE that causes the
1067  * original row to migrate to a different partition. Maybe this
1068  * can be implemented some day, but it seems a fringe feature with
1069  * little redeeming value.
1070  */
1071  if (((ModifyTable *) mtstate->ps.plan)->onConflictAction == ONCONFLICT_UPDATE)
1072  ereport(ERROR,
1073  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1074  errmsg("invalid ON UPDATE specification"),
1075  errdetail("The result tuple would appear in a different partition than the original tuple.")));
1076 
1077  /*
1078  * When an UPDATE is run on a leaf partition, we will not have
1079  * partition tuple routing set up. In that case, fail with
1080  * partition constraint violation error.
1081  */
1082  if (proute == NULL)
1083  ExecPartitionCheckEmitError(resultRelInfo, slot, estate);
1084 
1085  /*
1086  * Row movement, part 1. Delete the tuple, but skip RETURNING
1087  * processing. We want to return rows from INSERT.
1088  */
1089  ExecDelete(mtstate, tupleid, oldtuple, planSlot, epqstate,
1090  estate, &tuple_deleted, false,
1091  false /* canSetTag */, true /* changingPart */);
1092 
1093  /*
1094  * For some reason if DELETE didn't happen (e.g. trigger prevented
1095  * it, or it was already deleted by self, or it was concurrently
1096  * deleted by another transaction), then we should skip the insert
1097  * as well; otherwise, an UPDATE could cause an increase in the
1098  * total number of rows across all partitions, which is clearly
1099  * wrong.
1100  *
1101  * For a normal UPDATE, the case where the tuple has been the
1102  * subject of a concurrent UPDATE or DELETE would be handled by
1103  * the EvalPlanQual machinery, but for an UPDATE that we've
1104  * translated into a DELETE from this partition and an INSERT into
1105  * some other partition, that's not available, because CTID chains
1106  * can't span relation boundaries. We mimic the semantics to a
1107  * limited extent by skipping the INSERT if the DELETE fails to
1108  * find a tuple. This ensures that two concurrent attempts to
1109  * UPDATE the same tuple at the same time can't turn one tuple
1110  * into two, and that an UPDATE of a just-deleted tuple can't
1111  * resurrect it.
1112  */
1113  if (!tuple_deleted)
1114  return NULL;
1115 
1116  /*
1117  * Updates set the transition capture map only when a new subplan
1118  * is chosen. But for inserts, it is set for each row. So after
1119  * INSERT, we need to revert back to the map created for UPDATE;
1120  * otherwise the next UPDATE will incorrectly use the one created
1121  * for INSERT. So first save the one created for UPDATE.
1122  */
1123  if (mtstate->mt_transition_capture)
1124  saved_tcs_map = mtstate->mt_transition_capture->tcs_map;
1125 
1126  /*
1127  * resultRelInfo is one of the per-subplan resultRelInfos. So we
1128  * should convert the tuple into root's tuple descriptor, since
1129  * ExecInsert() starts the search from root. The tuple conversion
1130  * map list is in the order of mtstate->resultRelInfo[], so to
1131  * retrieve the one for this resultRel, we need to know the
1132  * position of the resultRel in mtstate->resultRelInfo[].
1133  */
1134  map_index = resultRelInfo - mtstate->resultRelInfo;
1135  Assert(map_index >= 0 && map_index < mtstate->mt_nplans);
1136  tupconv_map = tupconv_map_for_subplan(mtstate, map_index);
1137  tuple = ConvertPartitionTupleSlot(tupconv_map,
1138  tuple,
1139  proute->root_tuple_slot,
1140  &slot);
1141 
1142  /*
1143  * Prepare for tuple routing, making it look like we're inserting
1144  * into the root.
1145  */
1146  Assert(mtstate->rootResultRelInfo != NULL);
1147  slot = ExecPrepareTupleRouting(mtstate, estate, proute,
1148  mtstate->rootResultRelInfo, slot);
1149 
1150  ret_slot = ExecInsert(mtstate, slot, planSlot,
1151  estate, canSetTag);
1152 
1153  /* Revert ExecPrepareTupleRouting's node change. */
1154  estate->es_result_relation_info = resultRelInfo;
1155  if (mtstate->mt_transition_capture)
1156  {
1158  mtstate->mt_transition_capture->tcs_map = saved_tcs_map;
1159  }
1160 
1161  return ret_slot;
1162  }
1163 
1164  /*
1165  * Check the constraints of the tuple. Note that we pass the same
1166  * slot for the orig_slot argument, because unlike ExecInsert(), no
1167  * tuple-routing is performed here, hence the slot remains unchanged.
1168  * We've already checked the partition constraint above; however, we
1169  * must still ensure the tuple passes all other constraints, so we
1170  * will call ExecConstraints() and have it validate all remaining
1171  * checks.
1172  */
1173  if (resultRelationDesc->rd_att->constr)
1174  ExecConstraints(resultRelInfo, slot, estate, false);
1175 
1176  /*
1177  * replace the heap tuple
1178  *
1179  * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check
1180  * that the row to be updated is visible to that snapshot, and throw a
1181  * can't-serialize error if not. This is a special-case behavior
1182  * needed for referential integrity updates in transaction-snapshot
1183  * mode transactions.
1184  */
1185  result = heap_update(resultRelationDesc, tupleid, tuple,
1186  estate->es_output_cid,
1187  estate->es_crosscheck_snapshot,
1188  true /* wait for commit */ ,
1189  &hufd, &lockmode);
1190  switch (result)
1191  {
1192  case HeapTupleSelfUpdated:
1193 
1194  /*
1195  * The target tuple was already updated or deleted by the
1196  * current command, or by a later command in the current
1197  * transaction. The former case is possible in a join UPDATE
1198  * where multiple tuples join to the same target tuple. This
1199  * is pretty questionable, but Postgres has always allowed it:
1200  * we just execute the first update action and ignore
1201  * additional update attempts.
1202  *
1203  * The latter case arises if the tuple is modified by a
1204  * command in a BEFORE trigger, or perhaps by a command in a
1205  * volatile function used in the query. In such situations we
1206  * should not ignore the update, but it is equally unsafe to
1207  * proceed. We don't want to discard the original UPDATE
1208  * while keeping the triggered actions based on it; and we
1209  * have no principled way to merge this update with the
1210  * previous ones. So throwing an error is the only safe
1211  * course.
1212  *
1213  * If a trigger actually intends this type of interaction, it
1214  * can re-execute the UPDATE (assuming it can figure out how)
1215  * and then return NULL to cancel the outer update.
1216  */
1217  if (hufd.cmax != estate->es_output_cid)
1218  ereport(ERROR,
1219  (errcode(ERRCODE_TRIGGERED_DATA_CHANGE_VIOLATION),
1220  errmsg("tuple to be updated was already modified by an operation triggered by the current command"),
1221  errhint("Consider using an AFTER trigger instead of a BEFORE trigger to propagate changes to other rows.")));
1222 
1223  /* Else, already updated by self; nothing to do */
1224  return NULL;
1225 
1226  case HeapTupleMayBeUpdated:
1227  break;
1228 
1229  case HeapTupleUpdated:
1231  ereport(ERROR,
1232  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1233  errmsg("could not serialize access due to concurrent update")));
1235  ereport(ERROR,
1236  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1237  errmsg("tuple to be updated was already moved to another partition due to concurrent update")));
1238 
1239  if (!ItemPointerEquals(tupleid, &hufd.ctid))
1240  {
1241  TupleTableSlot *epqslot;
1242 
1243  epqslot = EvalPlanQual(estate,
1244  epqstate,
1245  resultRelationDesc,
1246  resultRelInfo->ri_RangeTableIndex,
1247  lockmode,
1248  &hufd.ctid,
1249  hufd.xmax);
1250  if (!TupIsNull(epqslot))
1251  {
1252  *tupleid = hufd.ctid;
1253  slot = ExecFilterJunk(resultRelInfo->ri_junkFilter, epqslot);
1254  tuple = ExecMaterializeSlot(slot);
1255  goto lreplace;
1256  }
1257  }
1258  /* tuple already deleted; nothing to do */
1259  return NULL;
1260 
1261  default:
1262  elog(ERROR, "unrecognized heap_update status: %u", result);
1263  return NULL;
1264  }
1265 
1266  /*
1267  * Note: instead of having to update the old index tuples associated
1268  * with the heap tuple, all we do is form and insert new index tuples.
1269  * This is because UPDATEs are actually DELETEs and INSERTs, and index
1270  * tuple deletion is done later by VACUUM (see notes in ExecDelete).
1271  * All we do here is insert new index tuples. -cim 9/27/89
1272  */
1273 
1274  /*
1275  * insert index entries for tuple
1276  *
1277  * Note: heap_update returns the tid (location) of the new tuple in
1278  * the t_self field.
1279  *
1280  * If it's a HOT update, we mustn't insert new index entries.
1281  */
1282  if (resultRelInfo->ri_NumIndices > 0 && !HeapTupleIsHeapOnly(tuple))
1283  recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
1284  estate, false, NULL, NIL);
1285  }
1286 
1287  if (canSetTag)
1288  (estate->es_processed)++;
1289 
1290  /* AFTER ROW UPDATE Triggers */
1291  ExecARUpdateTriggers(estate, resultRelInfo, tupleid, oldtuple, tuple,
1292  recheckIndexes,
1293  mtstate->operation == CMD_INSERT ?
1294  mtstate->mt_oc_transition_capture :
1295  mtstate->mt_transition_capture);
1296 
1297  list_free(recheckIndexes);
1298 
1299  /*
1300  * Check any WITH CHECK OPTION constraints from parent views. We are
1301  * required to do this after testing all constraints and uniqueness
1302  * violations per the SQL spec, so we do it after actually updating the
1303  * record in the heap and all indexes.
1304  *
1305  * ExecWithCheckOptions() will skip any WCOs which are not of the kind we
1306  * are looking for at this point.
1307  */
1308  if (resultRelInfo->ri_WithCheckOptions != NIL)
1309  ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);
1310 
1311  /* Process RETURNING if present */
1312  if (resultRelInfo->ri_projectReturning)
1313  return ExecProcessReturning(resultRelInfo, slot, planSlot);
1314 
1315  return NULL;
1316 }
1317 
1318 /*
1319  * ExecOnConflictUpdate --- execute UPDATE of INSERT ON CONFLICT DO UPDATE
1320  *
1321  * Try to lock tuple for update as part of speculative insertion. If
1322  * a qual originating from ON CONFLICT DO UPDATE is satisfied, update
1323  * (but still lock row, even though it may not satisfy estate's
1324  * snapshot).
1325  *
1326  * Returns true if if we're done (with or without an update), or false if
1327  * the caller must retry the INSERT from scratch.
1328  */
1329 static bool
1331  ResultRelInfo *resultRelInfo,
1332  ItemPointer conflictTid,
1333  TupleTableSlot *planSlot,
1334  TupleTableSlot *excludedSlot,
1335  EState *estate,
1336  bool canSetTag,
1337  TupleTableSlot **returning)
1338 {
1339  ExprContext *econtext = mtstate->ps.ps_ExprContext;
1340  Relation relation = resultRelInfo->ri_RelationDesc;
1341  ExprState *onConflictSetWhere = resultRelInfo->ri_onConflict->oc_WhereClause;
1342  HeapTupleData tuple;
1343  HeapUpdateFailureData hufd;
1344  LockTupleMode lockmode;
1345  HTSU_Result test;
1346  Buffer buffer;
1347 
1348  /* Determine lock mode to use */
1349  lockmode = ExecUpdateLockMode(estate, resultRelInfo);
1350 
1351  /*
1352  * Lock tuple for update. Don't follow updates when tuple cannot be
1353  * locked without doing so. A row locking conflict here means our
1354  * previous conclusion that the tuple is conclusively committed is not
1355  * true anymore.
1356  */
1357  tuple.t_self = *conflictTid;
1358  test = heap_lock_tuple(relation, &tuple, estate->es_output_cid,
1359  lockmode, LockWaitBlock, false, &buffer,
1360  &hufd);
1361  switch (test)
1362  {
1363  case HeapTupleMayBeUpdated:
1364  /* success! */
1365  break;
1366 
1367  case HeapTupleInvisible:
1368 
1369  /*
1370  * This can occur when a just inserted tuple is updated again in
1371  * the same command. E.g. because multiple rows with the same
1372  * conflicting key values are inserted.
1373  *
1374  * This is somewhat similar to the ExecUpdate()
1375  * HeapTupleSelfUpdated case. We do not want to proceed because
1376  * it would lead to the same row being updated a second time in
1377  * some unspecified order, and in contrast to plain UPDATEs
1378  * there's no historical behavior to break.
1379  *
1380  * It is the user's responsibility to prevent this situation from
1381  * occurring. These problems are why SQL-2003 similarly specifies
1382  * that for SQL MERGE, an exception must be raised in the event of
1383  * an attempt to update the same row twice.
1384  */
1386  ereport(ERROR,
1387  (errcode(ERRCODE_CARDINALITY_VIOLATION),
1388  errmsg("ON CONFLICT DO UPDATE command cannot affect row a second time"),
1389  errhint("Ensure that no rows proposed for insertion within the same command have duplicate constrained values.")));
1390 
1391  /* This shouldn't happen */
1392  elog(ERROR, "attempted to lock invisible tuple");
1393 
1394  case HeapTupleSelfUpdated:
1395 
1396  /*
1397  * This state should never be reached. As a dirty snapshot is used
1398  * to find conflicting tuples, speculative insertion wouldn't have
1399  * seen this row to conflict with.
1400  */
1401  elog(ERROR, "unexpected self-updated tuple");
1402 
1403  case HeapTupleUpdated:
1405  ereport(ERROR,
1406  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1407  errmsg("could not serialize access due to concurrent update")));
1408 
1409  /*
1410  * As long as we don't support an UPDATE of INSERT ON CONFLICT for
1411  * a partitioned table we shouldn't reach to a case where tuple to
1412  * be lock is moved to another partition due to concurrent update
1413  * of the partition key.
1414  */
1416 
1417  /*
1418  * Tell caller to try again from the very start.
1419  *
1420  * It does not make sense to use the usual EvalPlanQual() style
1421  * loop here, as the new version of the row might not conflict
1422  * anymore, or the conflicting tuple has actually been deleted.
1423  */
1424  ReleaseBuffer(buffer);
1425  return false;
1426 
1427  default:
1428  elog(ERROR, "unrecognized heap_lock_tuple status: %u", test);
1429  }
1430 
1431  /*
1432  * Success, the tuple is locked.
1433  *
1434  * Reset per-tuple memory context to free any expression evaluation
1435  * storage allocated in the previous cycle.
1436  */
1437  ResetExprContext(econtext);
1438 
1439  /*
1440  * Verify that the tuple is visible to our MVCC snapshot if the current
1441  * isolation level mandates that.
1442  *
1443  * It's not sufficient to rely on the check within ExecUpdate() as e.g.
1444  * CONFLICT ... WHERE clause may prevent us from reaching that.
1445  *
1446  * This means we only ever continue when a new command in the current
1447  * transaction could see the row, even though in READ COMMITTED mode the
1448  * tuple will not be visible according to the current statement's
1449  * snapshot. This is in line with the way UPDATE deals with newer tuple
1450  * versions.
1451  */
1452  ExecCheckHeapTupleVisible(estate, &tuple, buffer);
1453 
1454  /* Store target's existing tuple in the state's dedicated slot */
1455  ExecStoreTuple(&tuple, mtstate->mt_existing, buffer, false);
1456 
1457  /*
1458  * Make tuple and any needed join variables available to ExecQual and
1459  * ExecProject. The EXCLUDED tuple is installed in ecxt_innertuple, while
1460  * the target's existing tuple is installed in the scantuple. EXCLUDED
1461  * has been made to reference INNER_VAR in setrefs.c, but there is no
1462  * other redirection.
1463  */
1464  econtext->ecxt_scantuple = mtstate->mt_existing;
1465  econtext->ecxt_innertuple = excludedSlot;
1466  econtext->ecxt_outertuple = NULL;
1467 
1468  if (!ExecQual(onConflictSetWhere, econtext))
1469  {
1470  ReleaseBuffer(buffer);
1471  InstrCountFiltered1(&mtstate->ps, 1);
1472  return true; /* done with the tuple */
1473  }
1474 
1475  if (resultRelInfo->ri_WithCheckOptions != NIL)
1476  {
1477  /*
1478  * Check target's existing tuple against UPDATE-applicable USING
1479  * security barrier quals (if any), enforced here as RLS checks/WCOs.
1480  *
1481  * The rewriter creates UPDATE RLS checks/WCOs for UPDATE security
1482  * quals, and stores them as WCOs of "kind" WCO_RLS_CONFLICT_CHECK,
1483  * but that's almost the extent of its special handling for ON
1484  * CONFLICT DO UPDATE.
1485  *
1486  * The rewriter will also have associated UPDATE applicable straight
1487  * RLS checks/WCOs for the benefit of the ExecUpdate() call that
1488  * follows. INSERTs and UPDATEs naturally have mutually exclusive WCO
1489  * kinds, so there is no danger of spurious over-enforcement in the
1490  * INSERT or UPDATE path.
1491  */
1493  mtstate->mt_existing,
1494  mtstate->ps.state);
1495  }
1496 
1497  /* Project the new tuple version */
1498  ExecProject(resultRelInfo->ri_onConflict->oc_ProjInfo);
1499 
1500  /*
1501  * Note that it is possible that the target tuple has been modified in
1502  * this session, after the above heap_lock_tuple. We choose to not error
1503  * out in that case, in line with ExecUpdate's treatment of similar cases.
1504  * This can happen if an UPDATE is triggered from within ExecQual(),
1505  * ExecWithCheckOptions() or ExecProject() above, e.g. by selecting from a
1506  * wCTE in the ON CONFLICT's SET.
1507  */
1508 
1509  /* Execute UPDATE with projection */
1510  *returning = ExecUpdate(mtstate, &tuple.t_self, NULL,
1511  mtstate->mt_conflproj, planSlot,
1512  &mtstate->mt_epqstate, mtstate->ps.state,
1513  canSetTag);
1514 
1515  ReleaseBuffer(buffer);
1516  return true;
1517 }
1518 
1519 
1520 /*
1521  * Process BEFORE EACH STATEMENT triggers
1522  */
1523 static void
1525 {
1526  ModifyTable *plan = (ModifyTable *) node->ps.plan;
1527  ResultRelInfo *resultRelInfo = node->resultRelInfo;
1528 
1529  /*
1530  * If the node modifies a partitioned table, we must fire its triggers.
1531  * Note that in that case, node->resultRelInfo points to the first leaf
1532  * partition, not the root table.
1533  */
1534  if (node->rootResultRelInfo != NULL)
1535  resultRelInfo = node->rootResultRelInfo;
1536 
1537  switch (node->operation)
1538  {
1539  case CMD_INSERT:
1540  ExecBSInsertTriggers(node->ps.state, resultRelInfo);
1541  if (plan->onConflictAction == ONCONFLICT_UPDATE)
1543  resultRelInfo);
1544  break;
1545  case CMD_UPDATE:
1546  ExecBSUpdateTriggers(node->ps.state, resultRelInfo);
1547  break;
1548  case CMD_DELETE:
1549  ExecBSDeleteTriggers(node->ps.state, resultRelInfo);
1550  break;
1551  default:
1552  elog(ERROR, "unknown operation");
1553  break;
1554  }
1555 }
1556 
1557 /*
1558  * Return the target rel ResultRelInfo.
1559  *
1560  * This relation is the same as :
1561  * - the relation for which we will fire AFTER STATEMENT triggers.
1562  * - the relation into whose tuple format all captured transition tuples must
1563  * be converted.
1564  * - the root partitioned table.
1565  */
1566 static ResultRelInfo *
1568 {
1569  /*
1570  * Note that if the node modifies a partitioned table, node->resultRelInfo
1571  * points to the first leaf partition, not the root table.
1572  */
1573  if (node->rootResultRelInfo != NULL)
1574  return node->rootResultRelInfo;
1575  else
1576  return node->resultRelInfo;
1577 }
1578 
1579 /*
1580  * Process AFTER EACH STATEMENT triggers
1581  */
1582 static void
1584 {
1585  ModifyTable *plan = (ModifyTable *) node->ps.plan;
1586  ResultRelInfo *resultRelInfo = getTargetResultRelInfo(node);
1587 
1588  switch (node->operation)
1589  {
1590  case CMD_INSERT:
1591  if (plan->onConflictAction == ONCONFLICT_UPDATE)
1593  resultRelInfo,
1594  node->mt_oc_transition_capture);
1595  ExecASInsertTriggers(node->ps.state, resultRelInfo,
1596  node->mt_transition_capture);
1597  break;
1598  case CMD_UPDATE:
1599  ExecASUpdateTriggers(node->ps.state, resultRelInfo,
1600  node->mt_transition_capture);
1601  break;
1602  case CMD_DELETE:
1603  ExecASDeleteTriggers(node->ps.state, resultRelInfo,
1604  node->mt_transition_capture);
1605  break;
1606  default:
1607  elog(ERROR, "unknown operation");
1608  break;
1609  }
1610 }
1611 
1612 /*
1613  * Set up the state needed for collecting transition tuples for AFTER
1614  * triggers.
1615  */
1616 static void
1618 {
1619  ModifyTable *plan = (ModifyTable *) mtstate->ps.plan;
1620  ResultRelInfo *targetRelInfo = getTargetResultRelInfo(mtstate);
1621 
1622  /* Check for transition tables on the directly targeted relation. */
1623  mtstate->mt_transition_capture =
1624  MakeTransitionCaptureState(targetRelInfo->ri_TrigDesc,
1625  RelationGetRelid(targetRelInfo->ri_RelationDesc),
1626  mtstate->operation);
1627  if (plan->operation == CMD_INSERT &&
1629  mtstate->mt_oc_transition_capture =
1630  MakeTransitionCaptureState(targetRelInfo->ri_TrigDesc,
1631  RelationGetRelid(targetRelInfo->ri_RelationDesc),
1632  CMD_UPDATE);
1633 
1634  /*
1635  * If we found that we need to collect transition tuples then we may also
1636  * need tuple conversion maps for any children that have TupleDescs that
1637  * aren't compatible with the tuplestores. (We can share these maps
1638  * between the regular and ON CONFLICT cases.)
1639  */
1640  if (mtstate->mt_transition_capture != NULL ||
1641  mtstate->mt_oc_transition_capture != NULL)
1642  {
1644 
1645  /*
1646  * Install the conversion map for the first plan for UPDATE and DELETE
1647  * operations. It will be advanced each time we switch to the next
1648  * plan. (INSERT operations set it every time, so we need not update
1649  * mtstate->mt_oc_transition_capture here.)
1650  */
1651  if (mtstate->mt_transition_capture && mtstate->operation != CMD_INSERT)
1652  mtstate->mt_transition_capture->tcs_map =
1653  tupconv_map_for_subplan(mtstate, 0);
1654  }
1655 }
1656 
1657 /*
1658  * ExecPrepareTupleRouting --- prepare for routing one tuple
1659  *
1660  * Determine the partition in which the tuple in slot is to be inserted,
1661  * and modify mtstate and estate to prepare for it.
1662  *
1663  * Caller must revert the estate changes after executing the insertion!
1664  * In mtstate, transition capture changes may also need to be reverted.
1665  *
1666  * Returns a slot holding the tuple of the partition rowtype.
1667  */
1668 static TupleTableSlot *
1670  EState *estate,
1671  PartitionTupleRouting *proute,
1672  ResultRelInfo *targetRelInfo,
1673  TupleTableSlot *slot)
1674 {
1675  ModifyTable *node;
1676  int partidx;
1677  ResultRelInfo *partrel;
1678  HeapTuple tuple;
1679 
1680  /*
1681  * Determine the target partition. If ExecFindPartition does not find
1682  * a partition after all, it doesn't return here; otherwise, the returned
1683  * value is to be used as an index into the arrays for the ResultRelInfo
1684  * and TupleConversionMap for the partition.
1685  */
1686  partidx = ExecFindPartition(targetRelInfo,
1687  proute->partition_dispatch_info,
1688  slot,
1689  estate);
1690  Assert(partidx >= 0 && partidx < proute->num_partitions);
1691 
1692  /*
1693  * Get the ResultRelInfo corresponding to the selected partition; if not
1694  * yet there, initialize it.
1695  */
1696  partrel = proute->partitions[partidx];
1697  if (partrel == NULL)
1698  partrel = ExecInitPartitionInfo(mtstate, targetRelInfo,
1699  proute, estate,
1700  partidx);
1701 
1702  /*
1703  * Set up information needed for routing tuples to the partition if we
1704  * didn't yet (ExecInitRoutingInfo would abort the operation if the
1705  * partition isn't routable).
1706  *
1707  * Note: an UPDATE of a partition key invokes an INSERT that moves the
1708  * tuple to a new partition. This setup would be needed for a subplan
1709  * partition of such an UPDATE that is chosen as the partition to route
1710  * the tuple to. The reason we do this setup here rather than in
1711  * ExecSetupPartitionTupleRouting is to avoid aborting such an UPDATE
1712  * unnecessarily due to non-routable subplan partitions that may not be
1713  * chosen for update tuple movement after all.
1714  */
1715  if (!partrel->ri_PartitionReadyForRouting)
1716  ExecInitRoutingInfo(mtstate, estate, proute, partrel, partidx);
1717 
1718  /*
1719  * Make it look like we are inserting into the partition.
1720  */
1721  estate->es_result_relation_info = partrel;
1722 
1723  /* Get the heap tuple out of the given slot. */
1724  tuple = ExecMaterializeSlot(slot);
1725 
1726  /*
1727  * If we're capturing transition tuples, we might need to convert from the
1728  * partition rowtype to parent rowtype.
1729  */
1730  if (mtstate->mt_transition_capture != NULL)
1731  {
1732  if (partrel->ri_TrigDesc &&
1734  {
1735  /*
1736  * If there are any BEFORE triggers on the partition, we'll have
1737  * to be ready to convert their result back to tuplestore format.
1738  */
1740  mtstate->mt_transition_capture->tcs_map =
1741  TupConvMapForLeaf(proute, targetRelInfo, partidx);
1742  }
1743  else
1744  {
1745  /*
1746  * Otherwise, just remember the original unconverted tuple, to
1747  * avoid a needless round trip conversion.
1748  */
1750  mtstate->mt_transition_capture->tcs_map = NULL;
1751  }
1752  }
1753  if (mtstate->mt_oc_transition_capture != NULL)
1754  {
1755  mtstate->mt_oc_transition_capture->tcs_map =
1756  TupConvMapForLeaf(proute, targetRelInfo, partidx);
1757  }
1758 
1759  /*
1760  * Convert the tuple, if necessary.
1761  */
1763  tuple,
1764  proute->partition_tuple_slot,
1765  &slot);
1766 
1767  /* Initialize information needed to handle ON CONFLICT DO UPDATE. */
1768  Assert(mtstate != NULL);
1769  node = (ModifyTable *) mtstate->ps.plan;
1770  if (node->onConflictAction == ONCONFLICT_UPDATE)
1771  {
1772  Assert(mtstate->mt_existing != NULL);
1774  RelationGetDescr(partrel->ri_RelationDesc));
1775  Assert(mtstate->mt_conflproj != NULL);
1777  partrel->ri_onConflict->oc_ProjTupdesc);
1778  }
1779 
1780  return slot;
1781 }
1782 
1783 /*
1784  * Initialize the child-to-root tuple conversion map array for UPDATE subplans.
1785  *
1786  * This map array is required to convert the tuple from the subplan result rel
1787  * to the target table descriptor. This requirement arises for two independent
1788  * scenarios:
1789  * 1. For update-tuple-routing.
1790  * 2. For capturing tuples in transition tables.
1791  */
1792 static void
1794 {
1795  ResultRelInfo *targetRelInfo = getTargetResultRelInfo(mtstate);
1796  ResultRelInfo *resultRelInfos = mtstate->resultRelInfo;
1797  TupleDesc outdesc;
1798  int numResultRelInfos = mtstate->mt_nplans;
1799  int i;
1800 
1801  /*
1802  * First check if there is already a per-subplan array allocated. Even if
1803  * there is already a per-leaf map array, we won't require a per-subplan
1804  * one, since we will use the subplan offset array to convert the subplan
1805  * index to per-leaf index.
1806  */
1807  if (mtstate->mt_per_subplan_tupconv_maps ||
1808  (mtstate->mt_partition_tuple_routing &&
1810  return;
1811 
1812  /*
1813  * Build array of conversion maps from each child's TupleDesc to the one
1814  * used in the target relation. The map pointers may be NULL when no
1815  * conversion is necessary, which is hopefully a common case.
1816  */
1817 
1818  /* Get tuple descriptor of the target rel. */
1819  outdesc = RelationGetDescr(targetRelInfo->ri_RelationDesc);
1820 
1822  palloc(sizeof(TupleConversionMap *) * numResultRelInfos);
1823 
1824  for (i = 0; i < numResultRelInfos; ++i)
1825  {
1826  mtstate->mt_per_subplan_tupconv_maps[i] =
1827  convert_tuples_by_name(RelationGetDescr(resultRelInfos[i].ri_RelationDesc),
1828  outdesc,
1829  gettext_noop("could not convert row type"));
1830  }
1831 }
1832 
1833 /*
1834  * Initialize the child-to-root tuple conversion map array required for
1835  * capturing transition tuples.
1836  *
1837  * The map array can be indexed either by subplan index or by leaf-partition
1838  * index. For transition tables, we need a subplan-indexed access to the map,
1839  * and where tuple-routing is present, we also require a leaf-indexed access.
1840  */
1841 static void
1843 {
1845 
1846  /*
1847  * If partition tuple routing is set up, we will require partition-indexed
1848  * access. In that case, create the map array indexed by partition; we
1849  * will still be able to access the maps using a subplan index by
1850  * converting the subplan index to a partition index using
1851  * subplan_partition_offsets. If tuple routing is not set up, it means we
1852  * don't require partition-indexed access. In that case, create just a
1853  * subplan-indexed map.
1854  */
1855  if (proute)
1856  {
1857  /*
1858  * If a partition-indexed map array is to be created, the subplan map
1859  * array has to be NULL. If the subplan map array is already created,
1860  * we won't be able to access the map using a partition index.
1861  */
1862  Assert(mtstate->mt_per_subplan_tupconv_maps == NULL);
1863 
1865  }
1866  else
1868 }
1869 
1870 /*
1871  * For a given subplan index, get the tuple conversion map.
1872  */
1873 static TupleConversionMap *
1875 {
1876  /*
1877  * If a partition-index tuple conversion map array is allocated, we need
1878  * to first get the index into the partition array. Exactly *one* of the
1879  * two arrays is allocated. This is because if there is a partition array
1880  * required, we don't require subplan-indexed array since we can translate
1881  * subplan index into partition index. And, we create a subplan-indexed
1882  * array *only* if partition-indexed array is not required.
1883  */
1884  if (mtstate->mt_per_subplan_tupconv_maps == NULL)
1885  {
1886  int leaf_index;
1888 
1889  /*
1890  * If subplan-indexed array is NULL, things should have been arranged
1891  * to convert the subplan index to partition index.
1892  */
1893  Assert(proute && proute->subplan_partition_offsets != NULL &&
1894  whichplan < proute->num_subplan_partition_offsets);
1895 
1896  leaf_index = proute->subplan_partition_offsets[whichplan];
1897 
1898  return TupConvMapForLeaf(proute, getTargetResultRelInfo(mtstate),
1899  leaf_index);
1900  }
1901  else
1902  {
1903  Assert(whichplan >= 0 && whichplan < mtstate->mt_nplans);
1904  return mtstate->mt_per_subplan_tupconv_maps[whichplan];
1905  }
1906 }
1907 
1908 /* ----------------------------------------------------------------
1909  * ExecModifyTable
1910  *
1911  * Perform table modifications as required, and return RETURNING results
1912  * if needed.
1913  * ----------------------------------------------------------------
1914  */
1915 static TupleTableSlot *
1917 {
1918  ModifyTableState *node = castNode(ModifyTableState, pstate);
1920  EState *estate = node->ps.state;
1921  CmdType operation = node->operation;
1922  ResultRelInfo *saved_resultRelInfo;
1923  ResultRelInfo *resultRelInfo;
1924  PlanState *subplanstate;
1925  JunkFilter *junkfilter;
1926  TupleTableSlot *slot;
1927  TupleTableSlot *planSlot;
1928  ItemPointer tupleid;
1929  ItemPointerData tuple_ctid;
1930  HeapTupleData oldtupdata;
1931  HeapTuple oldtuple;
1932 
1934 
1935  /*
1936  * This should NOT get called during EvalPlanQual; we should have passed a
1937  * subplan tree to EvalPlanQual, instead. Use a runtime test not just
1938  * Assert because this condition is easy to miss in testing. (Note:
1939  * although ModifyTable should not get executed within an EvalPlanQual
1940  * operation, we do have to allow it to be initialized and shut down in
1941  * case it is within a CTE subplan. Hence this test must be here, not in
1942  * ExecInitModifyTable.)
1943  */
1944  if (estate->es_epqTuple != NULL)
1945  elog(ERROR, "ModifyTable should not be called during EvalPlanQual");
1946 
1947  /*
1948  * If we've already completed processing, don't try to do more. We need
1949  * this test because ExecPostprocessPlan might call us an extra time, and
1950  * our subplan's nodes aren't necessarily robust against being called
1951  * extra times.
1952  */
1953  if (node->mt_done)
1954  return NULL;
1955 
1956  /*
1957  * On first call, fire BEFORE STATEMENT triggers before proceeding.
1958  */
1959  if (node->fireBSTriggers)
1960  {
1961  fireBSTriggers(node);
1962  node->fireBSTriggers = false;
1963  }
1964 
1965  /* Preload local variables */
1966  resultRelInfo = node->resultRelInfo + node->mt_whichplan;
1967  subplanstate = node->mt_plans[node->mt_whichplan];
1968  junkfilter = resultRelInfo->ri_junkFilter;
1969 
1970  /*
1971  * es_result_relation_info must point to the currently active result
1972  * relation while we are within this ModifyTable node. Even though
1973  * ModifyTable nodes can't be nested statically, they can be nested
1974  * dynamically (since our subplan could include a reference to a modifying
1975  * CTE). So we have to save and restore the caller's value.
1976  */
1977  saved_resultRelInfo = estate->es_result_relation_info;
1978 
1979  estate->es_result_relation_info = resultRelInfo;
1980 
1981  /*
1982  * Fetch rows from subplan(s), and execute the required table modification
1983  * for each row.
1984  */
1985  for (;;)
1986  {
1987  /*
1988  * Reset the per-output-tuple exprcontext. This is needed because
1989  * triggers expect to use that context as workspace. It's a bit ugly
1990  * to do this below the top level of the plan, however. We might need
1991  * to rethink this later.
1992  */
1993  ResetPerTupleExprContext(estate);
1994 
1995  planSlot = ExecProcNode(subplanstate);
1996 
1997  if (TupIsNull(planSlot))
1998  {
1999  /* advance to next subplan if any */
2000  node->mt_whichplan++;
2001  if (node->mt_whichplan < node->mt_nplans)
2002  {
2003  resultRelInfo++;
2004  subplanstate = node->mt_plans[node->mt_whichplan];
2005  junkfilter = resultRelInfo->ri_junkFilter;
2006  estate->es_result_relation_info = resultRelInfo;
2007  EvalPlanQualSetPlan(&node->mt_epqstate, subplanstate->plan,
2008  node->mt_arowmarks[node->mt_whichplan]);
2009  /* Prepare to convert transition tuples from this child. */
2010  if (node->mt_transition_capture != NULL)
2011  {
2014  }
2015  if (node->mt_oc_transition_capture != NULL)
2016  {
2019  }
2020  continue;
2021  }
2022  else
2023  break;
2024  }
2025 
2026  /*
2027  * If resultRelInfo->ri_usesFdwDirectModify is true, all we need to do
2028  * here is compute the RETURNING expressions.
2029  */
2030  if (resultRelInfo->ri_usesFdwDirectModify)
2031  {
2032  Assert(resultRelInfo->ri_projectReturning);
2033 
2034  /*
2035  * A scan slot containing the data that was actually inserted,
2036  * updated or deleted has already been made available to
2037  * ExecProcessReturning by IterateDirectModify, so no need to
2038  * provide it here.
2039  */
2040  slot = ExecProcessReturning(resultRelInfo, NULL, planSlot);
2041 
2042  estate->es_result_relation_info = saved_resultRelInfo;
2043  return slot;
2044  }
2045 
2046  EvalPlanQualSetSlot(&node->mt_epqstate, planSlot);
2047  slot = planSlot;
2048 
2049  tupleid = NULL;
2050  oldtuple = NULL;
2051  if (junkfilter != NULL)
2052  {
2053  /*
2054  * extract the 'ctid' or 'wholerow' junk attribute.
2055  */
2056  if (operation == CMD_UPDATE || operation == CMD_DELETE)
2057  {
2058  char relkind;
2059  Datum datum;
2060  bool isNull;
2061 
2062  relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
2063  if (relkind == RELKIND_RELATION || relkind == RELKIND_MATVIEW)
2064  {
2065  datum = ExecGetJunkAttribute(slot,
2066  junkfilter->jf_junkAttNo,
2067  &isNull);
2068  /* shouldn't ever get a null result... */
2069  if (isNull)
2070  elog(ERROR, "ctid is NULL");
2071 
2072  tupleid = (ItemPointer) DatumGetPointer(datum);
2073  tuple_ctid = *tupleid; /* be sure we don't free ctid!! */
2074  tupleid = &tuple_ctid;
2075  }
2076 
2077  /*
2078  * Use the wholerow attribute, when available, to reconstruct
2079  * the old relation tuple.
2080  *
2081  * Foreign table updates have a wholerow attribute when the
2082  * relation has a row-level trigger. Note that the wholerow
2083  * attribute does not carry system columns. Foreign table
2084  * triggers miss seeing those, except that we know enough here
2085  * to set t_tableOid. Quite separately from this, the FDW may
2086  * fetch its own junk attrs to identify the row.
2087  *
2088  * Other relevant relkinds, currently limited to views, always
2089  * have a wholerow attribute.
2090  */
2091  else if (AttributeNumberIsValid(junkfilter->jf_junkAttNo))
2092  {
2093  datum = ExecGetJunkAttribute(slot,
2094  junkfilter->jf_junkAttNo,
2095  &isNull);
2096  /* shouldn't ever get a null result... */
2097  if (isNull)
2098  elog(ERROR, "wholerow is NULL");
2099 
2100  oldtupdata.t_data = DatumGetHeapTupleHeader(datum);
2101  oldtupdata.t_len =
2103  ItemPointerSetInvalid(&(oldtupdata.t_self));
2104  /* Historically, view triggers see invalid t_tableOid. */
2105  oldtupdata.t_tableOid =
2106  (relkind == RELKIND_VIEW) ? InvalidOid :
2107  RelationGetRelid(resultRelInfo->ri_RelationDesc);
2108 
2109  oldtuple = &oldtupdata;
2110  }
2111  else
2112  Assert(relkind == RELKIND_FOREIGN_TABLE);
2113  }
2114 
2115  /*
2116  * apply the junkfilter if needed.
2117  */
2118  if (operation != CMD_DELETE)
2119  slot = ExecFilterJunk(junkfilter, slot);
2120  }
2121 
2122  switch (operation)
2123  {
2124  case CMD_INSERT:
2125  /* Prepare for tuple routing if needed. */
2126  if (proute)
2127  slot = ExecPrepareTupleRouting(node, estate, proute,
2128  resultRelInfo, slot);
2129  slot = ExecInsert(node, slot, planSlot,
2130  estate, node->canSetTag);
2131  /* Revert ExecPrepareTupleRouting's state change. */
2132  if (proute)
2133  estate->es_result_relation_info = resultRelInfo;
2134  break;
2135  case CMD_UPDATE:
2136  slot = ExecUpdate(node, tupleid, oldtuple, slot, planSlot,
2137  &node->mt_epqstate, estate, node->canSetTag);
2138  break;
2139  case CMD_DELETE:
2140  slot = ExecDelete(node, tupleid, oldtuple, planSlot,
2141  &node->mt_epqstate, estate,
2142  NULL, true, node->canSetTag,
2143  false /* changingPart */);
2144  break;
2145  default:
2146  elog(ERROR, "unknown operation");
2147  break;
2148  }
2149 
2150  /*
2151  * If we got a RETURNING result, return it to caller. We'll continue
2152  * the work on next call.
2153  */
2154  if (slot)
2155  {
2156  estate->es_result_relation_info = saved_resultRelInfo;
2157  return slot;
2158  }
2159  }
2160 
2161  /* Restore es_result_relation_info before exiting */
2162  estate->es_result_relation_info = saved_resultRelInfo;
2163 
2164  /*
2165  * We're done, but fire AFTER STATEMENT triggers before exiting.
2166  */
2167  fireASTriggers(node);
2168 
2169  node->mt_done = true;
2170 
2171  return NULL;
2172 }
2173 
2174 /* ----------------------------------------------------------------
2175  * ExecInitModifyTable
2176  * ----------------------------------------------------------------
2177  */
2179 ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
2180 {
2181  ModifyTableState *mtstate;
2182  CmdType operation = node->operation;
2183  int nplans = list_length(node->plans);
2184  ResultRelInfo *saved_resultRelInfo;
2185  ResultRelInfo *resultRelInfo;
2186  Plan *subplan;
2187  ListCell *l;
2188  int i;
2189  Relation rel;
2190  bool update_tuple_routing_needed = node->partColsUpdated;
2191 
2192  /* check for unsupported flags */
2193  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
2194 
2195  /*
2196  * create state structure
2197  */
2198  mtstate = makeNode(ModifyTableState);
2199  mtstate->ps.plan = (Plan *) node;
2200  mtstate->ps.state = estate;
2201  mtstate->ps.ExecProcNode = ExecModifyTable;
2202 
2203  mtstate->operation = operation;
2204  mtstate->canSetTag = node->canSetTag;
2205  mtstate->mt_done = false;
2206 
2207  mtstate->mt_plans = (PlanState **) palloc0(sizeof(PlanState *) * nplans);
2208  mtstate->resultRelInfo = estate->es_result_relations + node->resultRelIndex;
2209 
2210  /* If modifying a partitioned table, initialize the root table info */
2211  if (node->rootResultRelIndex >= 0)
2212  mtstate->rootResultRelInfo = estate->es_root_result_relations +
2213  node->rootResultRelIndex;
2214 
2215  mtstate->mt_arowmarks = (List **) palloc0(sizeof(List *) * nplans);
2216  mtstate->mt_nplans = nplans;
2217 
2218  /* set up epqstate with dummy subplan data for the moment */
2219  EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL, node->epqParam);
2220  mtstate->fireBSTriggers = true;
2221 
2222  /*
2223  * call ExecInitNode on each of the plans to be executed and save the
2224  * results into the array "mt_plans". This is also a convenient place to
2225  * verify that the proposed target relations are valid and open their
2226  * indexes for insertion of new index entries. Note we *must* set
2227  * estate->es_result_relation_info correctly while we initialize each
2228  * sub-plan; ExecContextForcesOids depends on that!
2229  */
2230  saved_resultRelInfo = estate->es_result_relation_info;
2231 
2232  resultRelInfo = mtstate->resultRelInfo;
2233  i = 0;
2234  foreach(l, node->plans)
2235  {
2236  subplan = (Plan *) lfirst(l);
2237 
2238  /* Initialize the usesFdwDirectModify flag */
2239  resultRelInfo->ri_usesFdwDirectModify = bms_is_member(i,
2240  node->fdwDirectModifyPlans);
2241 
2242  /*
2243  * Verify result relation is a valid target for the current operation
2244  */
2245  CheckValidResultRel(resultRelInfo, operation);
2246 
2247  /*
2248  * If there are indices on the result relation, open them and save
2249  * descriptors in the result relation info, so that we can add new
2250  * index entries for the tuples we add/update. We need not do this
2251  * for a DELETE, however, since deletion doesn't affect indexes. Also,
2252  * inside an EvalPlanQual operation, the indexes might be open
2253  * already, since we share the resultrel state with the original
2254  * query.
2255  */
2256  if (resultRelInfo->ri_RelationDesc->rd_rel->relhasindex &&
2257  operation != CMD_DELETE &&
2258  resultRelInfo->ri_IndexRelationDescs == NULL)
2259  ExecOpenIndices(resultRelInfo,
2261 
2262  /*
2263  * If this is an UPDATE and a BEFORE UPDATE trigger is present, the
2264  * trigger itself might modify the partition-key values. So arrange
2265  * for tuple routing.
2266  */
2267  if (resultRelInfo->ri_TrigDesc &&
2268  resultRelInfo->ri_TrigDesc->trig_update_before_row &&
2269  operation == CMD_UPDATE)
2270  update_tuple_routing_needed = true;
2271 
2272  /* Now init the plan for this result rel */
2273  estate->es_result_relation_info = resultRelInfo;
2274  mtstate->mt_plans[i] = ExecInitNode(subplan, estate, eflags);
2275 
2276  /* Also let FDWs init themselves for foreign-table result rels */
2277  if (!resultRelInfo->ri_usesFdwDirectModify &&
2278  resultRelInfo->ri_FdwRoutine != NULL &&
2279  resultRelInfo->ri_FdwRoutine->BeginForeignModify != NULL)
2280  {
2281  List *fdw_private = (List *) list_nth(node->fdwPrivLists, i);
2282 
2283  resultRelInfo->ri_FdwRoutine->BeginForeignModify(mtstate,
2284  resultRelInfo,
2285  fdw_private,
2286  i,
2287  eflags);
2288  }
2289 
2290  resultRelInfo++;
2291  i++;
2292  }
2293 
2294  estate->es_result_relation_info = saved_resultRelInfo;
2295 
2296  /* Get the target relation */
2297  rel = (getTargetResultRelInfo(mtstate))->ri_RelationDesc;
2298 
2299  /*
2300  * If it's not a partitioned table after all, UPDATE tuple routing should
2301  * not be attempted.
2302  */
2303  if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2304  update_tuple_routing_needed = false;
2305 
2306  /*
2307  * Build state for tuple routing if it's an INSERT or if it's an UPDATE of
2308  * partition key.
2309  */
2310  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
2311  (operation == CMD_INSERT || update_tuple_routing_needed))
2312  mtstate->mt_partition_tuple_routing =
2313  ExecSetupPartitionTupleRouting(mtstate, rel);
2314 
2315  /*
2316  * Build state for collecting transition tuples. This requires having a
2317  * valid trigger query context, so skip it in explain-only mode.
2318  */
2319  if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
2320  ExecSetupTransitionCaptureState(mtstate, estate);
2321 
2322  /*
2323  * Construct mapping from each of the per-subplan partition attnos to the
2324  * root attno. This is required when during update row movement the tuple
2325  * descriptor of a source partition does not match the root partitioned
2326  * table descriptor. In such a case we need to convert tuples to the root
2327  * tuple descriptor, because the search for destination partition starts
2328  * from the root. Skip this setup if it's not a partition key update.
2329  */
2330  if (update_tuple_routing_needed)
2332 
2333  /*
2334  * Initialize any WITH CHECK OPTION constraints if needed.
2335  */
2336  resultRelInfo = mtstate->resultRelInfo;
2337  i = 0;
2338  foreach(l, node->withCheckOptionLists)
2339  {
2340  List *wcoList = (List *) lfirst(l);
2341  List *wcoExprs = NIL;
2342  ListCell *ll;
2343 
2344  foreach(ll, wcoList)
2345  {
2346  WithCheckOption *wco = (WithCheckOption *) lfirst(ll);
2347  ExprState *wcoExpr = ExecInitQual((List *) wco->qual,
2348  mtstate->mt_plans[i]);
2349 
2350  wcoExprs = lappend(wcoExprs, wcoExpr);
2351  }
2352 
2353  resultRelInfo->ri_WithCheckOptions = wcoList;
2354  resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
2355  resultRelInfo++;
2356  i++;
2357  }
2358 
2359  /*
2360  * Initialize RETURNING projections if needed.
2361  */
2362  if (node->returningLists)
2363  {
2364  TupleTableSlot *slot;
2365  ExprContext *econtext;
2366 
2367  /*
2368  * Initialize result tuple slot and assign its rowtype using the first
2369  * RETURNING list. We assume the rest will look the same.
2370  */
2371  mtstate->ps.plan->targetlist = (List *) linitial(node->returningLists);
2372 
2373  /* Set up a slot for the output of the RETURNING projection(s) */
2374  ExecInitResultTupleSlotTL(estate, &mtstate->ps);
2375  slot = mtstate->ps.ps_ResultTupleSlot;
2376 
2377  /* Need an econtext too */
2378  if (mtstate->ps.ps_ExprContext == NULL)
2379  ExecAssignExprContext(estate, &mtstate->ps);
2380  econtext = mtstate->ps.ps_ExprContext;
2381 
2382  /*
2383  * Build a projection for each result rel.
2384  */
2385  resultRelInfo = mtstate->resultRelInfo;
2386  foreach(l, node->returningLists)
2387  {
2388  List *rlist = (List *) lfirst(l);
2389 
2390  resultRelInfo->ri_returningList = rlist;
2391  resultRelInfo->ri_projectReturning =
2392  ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
2393  resultRelInfo->ri_RelationDesc->rd_att);
2394  resultRelInfo++;
2395  }
2396  }
2397  else
2398  {
2399  /*
2400  * We still must construct a dummy result tuple type, because InitPlan
2401  * expects one (maybe should change that?).
2402  */
2403  mtstate->ps.plan->targetlist = NIL;
2404  ExecInitResultTupleSlotTL(estate, &mtstate->ps);
2405 
2406  mtstate->ps.ps_ExprContext = NULL;
2407  }
2408 
2409  /* Set the list of arbiter indexes if needed for ON CONFLICT */
2410  resultRelInfo = mtstate->resultRelInfo;
2411  if (node->onConflictAction != ONCONFLICT_NONE)
2412  resultRelInfo->ri_onConflictArbiterIndexes = node->arbiterIndexes;
2413 
2414  /*
2415  * If needed, Initialize target list, projection and qual for ON CONFLICT
2416  * DO UPDATE.
2417  */
2418  if (node->onConflictAction == ONCONFLICT_UPDATE)
2419  {
2420  ExprContext *econtext;
2421  TupleDesc relationDesc;
2422  TupleDesc tupDesc;
2423 
2424  /* insert may only have one plan, inheritance is not expanded */
2425  Assert(nplans == 1);
2426 
2427  /* already exists if created by RETURNING processing above */
2428  if (mtstate->ps.ps_ExprContext == NULL)
2429  ExecAssignExprContext(estate, &mtstate->ps);
2430 
2431  econtext = mtstate->ps.ps_ExprContext;
2432  relationDesc = resultRelInfo->ri_RelationDesc->rd_att;
2433 
2434  /*
2435  * Initialize slot for the existing tuple. If we'll be performing
2436  * tuple routing, the tuple descriptor to use for this will be
2437  * determined based on which relation the update is actually applied
2438  * to, so we don't set its tuple descriptor here.
2439  */
2440  mtstate->mt_existing =
2441  ExecInitExtraTupleSlot(mtstate->ps.state,
2442  mtstate->mt_partition_tuple_routing ?
2443  NULL : relationDesc);
2444 
2445  /* carried forward solely for the benefit of explain */
2446  mtstate->mt_excludedtlist = node->exclRelTlist;
2447 
2448  /* create state for DO UPDATE SET operation */
2449  resultRelInfo->ri_onConflict = makeNode(OnConflictSetState);
2450 
2451  /*
2452  * Create the tuple slot for the UPDATE SET projection.
2453  *
2454  * Just like mt_existing above, we leave it without a tuple descriptor
2455  * in the case of partitioning tuple routing, so that it can be
2456  * changed by ExecPrepareTupleRouting. In that case, we still save
2457  * the tupdesc in the parent's state: it can be reused by partitions
2458  * with an identical descriptor to the parent.
2459  */
2460  tupDesc = ExecTypeFromTL((List *) node->onConflictSet,
2461  relationDesc->tdhasoid);
2462  mtstate->mt_conflproj =
2463  ExecInitExtraTupleSlot(mtstate->ps.state,
2464  mtstate->mt_partition_tuple_routing ?
2465  NULL : tupDesc);
2466  resultRelInfo->ri_onConflict->oc_ProjTupdesc = tupDesc;
2467 
2468  /* build UPDATE SET projection state */
2469  resultRelInfo->ri_onConflict->oc_ProjInfo =
2470  ExecBuildProjectionInfo(node->onConflictSet, econtext,
2471  mtstate->mt_conflproj, &mtstate->ps,
2472  relationDesc);
2473 
2474  /* initialize state to evaluate the WHERE clause, if any */
2475  if (node->onConflictWhere)
2476  {
2477  ExprState *qualexpr;
2478 
2479  qualexpr = ExecInitQual((List *) node->onConflictWhere,
2480  &mtstate->ps);
2481  resultRelInfo->ri_onConflict->oc_WhereClause = qualexpr;
2482  }
2483  }
2484 
2485  /*
2486  * If we have any secondary relations in an UPDATE or DELETE, they need to
2487  * be treated like non-locked relations in SELECT FOR UPDATE, ie, the
2488  * EvalPlanQual mechanism needs to be told about them. Locate the
2489  * relevant ExecRowMarks.
2490  */
2491  foreach(l, node->rowMarks)
2492  {
2494  ExecRowMark *erm;
2495 
2496  /* ignore "parent" rowmarks; they are irrelevant at runtime */
2497  if (rc->isParent)
2498  continue;
2499 
2500  /* find ExecRowMark (same for all subplans) */
2501  erm = ExecFindRowMark(estate, rc->rti, false);
2502 
2503  /* build ExecAuxRowMark for each subplan */
2504  for (i = 0; i < nplans; i++)
2505  {
2506  ExecAuxRowMark *aerm;
2507 
2508  subplan = mtstate->mt_plans[i]->plan;
2509  aerm = ExecBuildAuxRowMark(erm, subplan->targetlist);
2510  mtstate->mt_arowmarks[i] = lappend(mtstate->mt_arowmarks[i], aerm);
2511  }
2512  }
2513 
2514  /* select first subplan */
2515  mtstate->mt_whichplan = 0;
2516  subplan = (Plan *) linitial(node->plans);
2517  EvalPlanQualSetPlan(&mtstate->mt_epqstate, subplan,
2518  mtstate->mt_arowmarks[0]);
2519 
2520  /*
2521  * Initialize the junk filter(s) if needed. INSERT queries need a filter
2522  * if there are any junk attrs in the tlist. UPDATE and DELETE always
2523  * need a filter, since there's always at least one junk attribute present
2524  * --- no need to look first. Typically, this will be a 'ctid' or
2525  * 'wholerow' attribute, but in the case of a foreign data wrapper it
2526  * might be a set of junk attributes sufficient to identify the remote
2527  * row.
2528  *
2529  * If there are multiple result relations, each one needs its own junk
2530  * filter. Note multiple rels are only possible for UPDATE/DELETE, so we
2531  * can't be fooled by some needing a filter and some not.
2532  *
2533  * This section of code is also a convenient place to verify that the
2534  * output of an INSERT or UPDATE matches the target table(s).
2535  */
2536  {
2537  bool junk_filter_needed = false;
2538 
2539  switch (operation)
2540  {
2541  case CMD_INSERT:
2542  foreach(l, subplan->targetlist)
2543  {
2544  TargetEntry *tle = (TargetEntry *) lfirst(l);
2545 
2546  if (tle->resjunk)
2547  {
2548  junk_filter_needed = true;
2549  break;
2550  }
2551  }
2552  break;
2553  case CMD_UPDATE:
2554  case CMD_DELETE:
2555  junk_filter_needed = true;
2556  break;
2557  default:
2558  elog(ERROR, "unknown operation");
2559  break;
2560  }
2561 
2562  if (junk_filter_needed)
2563  {
2564  resultRelInfo = mtstate->resultRelInfo;
2565  for (i = 0; i < nplans; i++)
2566  {
2567  JunkFilter *j;
2568 
2569  subplan = mtstate->mt_plans[i]->plan;
2570  if (operation == CMD_INSERT || operation == CMD_UPDATE)
2571  ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
2572  subplan->targetlist);
2573 
2574  j = ExecInitJunkFilter(subplan->targetlist,
2575  resultRelInfo->ri_RelationDesc->rd_att->tdhasoid,
2576  ExecInitExtraTupleSlot(estate, NULL));
2577 
2578  if (operation == CMD_UPDATE || operation == CMD_DELETE)
2579  {
2580  /* For UPDATE/DELETE, find the appropriate junk attr now */
2581  char relkind;
2582 
2583  relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
2584  if (relkind == RELKIND_RELATION ||
2585  relkind == RELKIND_MATVIEW ||
2586  relkind == RELKIND_PARTITIONED_TABLE)
2587  {
2588  j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
2590  elog(ERROR, "could not find junk ctid column");
2591  }
2592  else if (relkind == RELKIND_FOREIGN_TABLE)
2593  {
2594  /*
2595  * When there is a row-level trigger, there should be
2596  * a wholerow attribute.
2597  */
2598  j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
2599  }
2600  else
2601  {
2602  j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
2604  elog(ERROR, "could not find junk wholerow column");
2605  }
2606  }
2607 
2608  resultRelInfo->ri_junkFilter = j;
2609  resultRelInfo++;
2610  }
2611  }
2612  else
2613  {
2614  if (operation == CMD_INSERT)
2616  subplan->targetlist);
2617  }
2618  }
2619 
2620  /*
2621  * Set up a tuple table slot for use for trigger output tuples. In a plan
2622  * containing multiple ModifyTable nodes, all can share one such slot, so
2623  * we keep it in the estate.
2624  */
2625  if (estate->es_trig_tuple_slot == NULL)
2626  estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate, NULL);
2627 
2628  /*
2629  * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it
2630  * to estate->es_auxmodifytables so that it will be run to completion by
2631  * ExecPostprocessPlan. (It'd actually work fine to add the primary
2632  * ModifyTable node too, but there's no need.) Note the use of lcons not
2633  * lappend: we need later-initialized ModifyTable nodes to be shut down
2634  * before earlier ones. This ensures that we don't throw away RETURNING
2635  * rows that need to be seen by a later CTE subplan.
2636  */
2637  if (!mtstate->canSetTag)
2638  estate->es_auxmodifytables = lcons(mtstate,
2639  estate->es_auxmodifytables);
2640 
2641  return mtstate;
2642 }
2643 
2644 /* ----------------------------------------------------------------
2645  * ExecEndModifyTable
2646  *
2647  * Shuts down the plan.
2648  *
2649  * Returns nothing of interest.
2650  * ----------------------------------------------------------------
2651  */
2652 void
2654 {
2655  int i;
2656 
2657  /*
2658  * Allow any FDWs to shut down
2659  */
2660  for (i = 0; i < node->mt_nplans; i++)
2661  {
2662  ResultRelInfo *resultRelInfo = node->resultRelInfo + i;
2663 
2664  if (!resultRelInfo->ri_usesFdwDirectModify &&
2665  resultRelInfo->ri_FdwRoutine != NULL &&
2666  resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
2667  resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
2668  resultRelInfo);
2669  }
2670 
2671  /* Close all the partitioned tables, leaf partitions, and their indices */
2672  if (node->mt_partition_tuple_routing)
2674 
2675  /*
2676  * Free the exprcontext
2677  */
2678  ExecFreeExprContext(&node->ps);
2679 
2680  /*
2681  * clean out the tuple table
2682  */
2684 
2685  /*
2686  * Terminate EPQ execution if active
2687  */
2688  EvalPlanQualEnd(&node->mt_epqstate);
2689 
2690  /*
2691  * shut down subplans
2692  */
2693  for (i = 0; i < node->mt_nplans; i++)
2694  ExecEndNode(node->mt_plans[i]);
2695 }
2696 
2697 void
2699 {
2700  /*
2701  * Currently, we don't need to support rescan on ModifyTable nodes. The
2702  * semantics of that would be a bit debatable anyway.
2703  */
2704  elog(ERROR, "ExecReScanModifyTable is not implemented");
2705 }
AttrNumber jf_junkAttNo
Definition: execnodes.h:365
ExecForeignDelete_function ExecForeignDelete
Definition: fdwapi.h:212
int ri_NumIndices
Definition: execnodes.h:400
#define NIL
Definition: pg_list.h:69
int ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd, TupleTableSlot *slot, EState *estate)
TupleTableSlot * ExecStoreTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer, bool shouldFree)
Definition: execTuples.c:356
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
JunkFilter * ri_junkFilter
Definition: execnodes.h:439
HeapTuple * es_epqTuple
Definition: execnodes.h:558
List * arbiterIndexes
Definition: plannodes.h:236
struct TransitionCaptureState * mt_oc_transition_capture
Definition: execnodes.h:1059
Relation ri_RelationDesc
Definition: execnodes.h:397
bool tts_isempty
Definition: tuptable.h:116
bool ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple)
Definition: trigger.c:2831
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Bitmapset * fdwDirectModifyPlans
Definition: plannodes.h:232
void SpeculativeInsertionLockRelease(TransactionId xid)
Definition: lmgr.c:695
static bool ExecOnConflictUpdate(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo, ItemPointer conflictTid, TupleTableSlot *planSlot, TupleTableSlot *excludedSlot, EState *estate, bool canSetTag, TupleTableSlot **returning)
int errhint(const char *fmt,...)
Definition: elog.c:987
void ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:2095
static TupleTableSlot * ExecInsert(ModifyTableState *mtstate, TupleTableSlot *slot, TupleTableSlot *planSlot, EState *estate, bool canSetTag)
void ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2662
void heap_abort_speculative(Relation relation, HeapTuple tuple)
Definition: heapam.c:6246
static ResultRelInfo * getTargetResultRelInfo(ModifyTableState *node)
bool tdhasoid
Definition: tupdesc.h:85
CommandId es_output_cid
Definition: execnodes.h:487
static void test(void)
void ExecReScanModifyTable(ModifyTableState *node)
TupleConversionMap * TupConvMapForLeaf(PartitionTupleRouting *proute, ResultRelInfo *rootRelInfo, int leaf_index)
List * ExecInsertIndexTuples(TupleTableSlot *slot, ItemPointer tupleid, EState *estate, bool noDupErr, bool *specConflict, List *arbiterIndexes)
Definition: execIndexing.c:271
#define ResetPerTupleExprContext(estate)
Definition: executor.h:498
struct PartitionTupleRouting * mt_partition_tuple_routing
Definition: execnodes.h:1053
TupleTableSlot * ExecStoreAllNullTuple(TupleTableSlot *slot)
Definition: execTuples.c:548
#define RelationGetDescr(relation)
Definition: rel.h:433
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:765
Oid es_lastoid
Definition: execnodes.h:530
List * withCheckOptionLists
Definition: plannodes.h:229
HTSU_Result heap_lock_tuple(Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, bool follow_updates, Buffer *buffer, HeapUpdateFailureData *hufd)
Definition: heapam.c:4685
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:539
int resultRelIndex
Definition: plannodes.h:226
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1042
static TupleTableSlot * ExecUpdate(ModifyTableState *mtstate, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *slot, TupleTableSlot *planSlot, EPQState *epqstate, EState *estate, bool canSetTag)
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf, bool keep_buf, Relation stats_relation)
Definition: heapam.c:1903
AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter, const char *attrName)
Definition: execJunk.c:209
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
ExecForeignInsert_function ExecForeignInsert
Definition: fdwapi.h:210
HTSU_Result heap_delete(Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, HeapUpdateFailureData *hufd, bool changingPart)
Definition: heapam.c:3060
Relation ri_PartitionRoot
Definition: execnodes.h:460
ExprContext * ps_ExprContext
Definition: execnodes.h:946
TupleTableSlot * EvalPlanQual(EState *estate, EPQState *epqstate, Relation relation, Index rti, int lockmode, ItemPointer tid, TransactionId priorXmax)
Definition: execMain.c:2500
static void fireBSTriggers(ModifyTableState *node)
TupleTableSlot * ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TupleTableSlot *slot)
Definition: trigger.c:2951
TupleTableSlot * ExecIRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2596
TupleTableSlot * mt_conflproj
Definition: execnodes.h:1050
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
static TupleConversionMap * tupconv_map_for_subplan(ModifyTableState *node, int whichplan)
bool ri_PartitionReadyForRouting
Definition: execnodes.h:463
#define IsolationUsesXactSnapshot()
Definition: xact.h:50
#define HeapTupleHeaderSetSpeculativeToken(tup, token)
Definition: htup_details.h:445
#define InvalidBuffer
Definition: buf.h:25
#define gettext_noop(x)
Definition: c.h:1036
bool partColsUpdated
Definition: plannodes.h:224
TupleConversionMap ** child_parent_tupconv_maps
Definition: nodes.h:517
Snapshot es_crosscheck_snapshot
Definition: execnodes.h:479
int errcode(int sqlerrcode)
Definition: elog.c:575
uint32 SpeculativeInsertionLockAcquire(TransactionId xid)
Definition: lmgr.c:669
bool canSetTag
Definition: plannodes.h:220
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
CmdType operation
Definition: execnodes.h:1036
Snapshot es_snapshot
Definition: execnodes.h:478
void ExecSetupChildParentMapForLeaf(PartitionTupleRouting *proute)
ResultRelInfo * rootResultRelInfo
Definition: execnodes.h:1043
void ExecARInsertTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:2581
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
void EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
Definition: execMain.c:2847
EState * state
Definition: execnodes.h:913
TupleTableSlot * partition_tuple_slot
Form_pg_class rd_rel
Definition: rel.h:84
unsigned int Oid
Definition: postgres_ext.h:31
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:361
void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate, bool check_partition_constraint)
Definition: execMain.c:1961
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:264
bool ExecCheckIndexConstraints(TupleTableSlot *slot, EState *estate, ItemPointer conflictTid, List *arbiterIndexes)
Definition: execIndexing.c:475
int natts
Definition: tupdesc.h:82
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:566
List * plans
Definition: plannodes.h:228
HeapTuple tcs_original_insert_tuple
Definition: trigger.h:82
ResultRelInfo ** partitions
#define HEAP_INSERT_SPECULATIVE
Definition: heapam.h:31
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc)
Definition: execTuples.c:931
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:205
Index ri_RangeTableIndex
Definition: execnodes.h:394
void ExecASDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2719
#define HeapTupleSatisfiesVisibility(tuple, snapshot, buffer)
Definition: tqual.h:45
char relkind
Definition: pg_class.h:51
List * onConflictSet
Definition: plannodes.h:237
int rootResultRelIndex
Definition: plannodes.h:227
TupleTableSlot * ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, HeapTuple trigtuple, TupleTableSlot *slot)
Definition: trigger.c:3110
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:149
TupleTableSlot * mt_existing
Definition: execnodes.h:1048
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:3251
ItemPointerData * ItemPointer
Definition: itemptr.h:49
PartitionTupleRouting * ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
Definition: execPartition.c:76
HeapTupleHeader t_data
Definition: htup.h:68
List * ri_WithCheckOptionExprs
Definition: execnodes.h:433
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:715
void ExecARDeleteTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, TransitionCaptureState *transition_capture)
Definition: trigger.c:2799
LockTupleMode
Definition: heapam.h:38
bool trig_insert_instead_row
Definition: reltrigger.h:57
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:945
static void ExecSetupChildParentMapForSubplan(ModifyTableState *mtstate)
List * rowMarks
Definition: plannodes.h:233
bool resjunk
Definition: primnodes.h:1383
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:1035
static void ExecCheckTIDVisible(EState *estate, ResultRelInfo *relinfo, ItemPointer tid)
ProjectionInfo * oc_ProjInfo
Definition: execnodes.h:377
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
TupleConversionMap * tcs_map
Definition: trigger.h:73
ItemPointerData t_self
Definition: htup.h:65
bool ri_usesFdwDirectModify
Definition: execnodes.h:427
static void ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
Definition: execMain.c:1104
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define lfirst_node(type, lc)
Definition: pg_list.h:109
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:417
static void ExecCheckHeapTupleVisible(EState *estate, HeapTuple tuple, Buffer buffer)
uint32 t_len
Definition: htup.h:64
void ExecBSInsertTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2447
void * list_nth(const List *list, int n)
Definition: list.c:410
struct TransitionCaptureState * mt_transition_capture
Definition: execnodes.h:1056
ResultRelInfo * es_result_relations
Definition: execnodes.h:490
JunkFilter * ExecInitJunkFilter(List *targetList, bool hasoid, TupleTableSlot *slot)
Definition: execJunk.c:61
List * fdwPrivLists
Definition: plannodes.h:231
int errdetail(const char *fmt,...)
Definition: elog.c:873
EPQState mt_epqstate
Definition: execnodes.h:1046
void ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1900
CommandId cmax
Definition: heapam.h:72
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:220
bool trig_update_before_row
Definition: reltrigger.h:60
HTSU_Result
Definition: snapshot.h:121
ProjectionInfo * ri_projectReturning
Definition: execnodes.h:445
#define TupIsNull(slot)
Definition: tuptable.h:146
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:421
unsigned int uint32
Definition: c.h:325
ModifyTableState * ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
Oid t_tableOid
Definition: htup.h:66
PartitionDispatch * partition_dispatch_info
Definition: execPartition.h:97
void ExecInitRoutingInfo(ModifyTableState *mtstate, EState *estate, PartitionTupleRouting *proute, ResultRelInfo *partRelInfo, int partidx)
#define InstrCountTuples2(node, delta)
Definition: execnodes.h:968
ExprState * oc_WhereClause
Definition: execnodes.h:379
void setLastTid(const ItemPointer tid)
Definition: tid.c:252
TupleTableSlot * es_trig_tuple_slot
Definition: execnodes.h:512
EndForeignModify_function EndForeignModify
Definition: fdwapi.h:213
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:973
WCOKind
Definition: parsenodes.h:1128
#define ereport(elevel, rest)
Definition: elog.h:122
Oid heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2441
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:409
void ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple, HeapTuple newtuple, List *recheckIndexes, TransitionCaptureState *transition_capture)
Definition: trigger.c:3068
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:965
TupleConversionMap * convert_tuples_by_name(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: tupconvert.c:210
List * lappend(List *list, void *datum)
Definition: list.c:128
bool trig_update_instead_row
Definition: reltrigger.h:62
static TupleTableSlot * ExecProcessReturning(ResultRelInfo *resultRelInfo, TupleTableSlot *tupleSlot, TupleTableSlot *planSlot)
PlanState ** mt_plans
Definition: execnodes.h:1039
static TupleTableSlot * ExecPrepareTupleRouting(ModifyTableState *mtstate, EState *estate, PartitionTupleRouting *proute, ResultRelInfo *targetRelInfo, TupleTableSlot *slot)
TransactionId xmax
Definition: heapam.h:71
OnConflictSetState * ri_onConflict
Definition: execnodes.h:451
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
static void fireASTriggers(ModifyTableState *node)
static TupleTableSlot * ExecModifyTable(PlanState *pstate)
TupleConversionMap ** mt_per_subplan_tupconv_maps
Definition: execnodes.h:1062
bool trig_insert_before_row
Definition: reltrigger.h:55
bool trig_delete_instead_row
Definition: reltrigger.h:67
void ExecASUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2938
void * palloc0(Size size)
Definition: mcxt.c:955
List * es_auxmodifytables
Definition: execnodes.h:540
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:917
void ExecASInsertTriggers(EState *estate, ResultRelInfo *relinfo, TransitionCaptureState *transition_capture)
Definition: trigger.c:2504
uintptr_t Datum
Definition: postgres.h:365
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
Definition: execJunk.c:262
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:281
#define SnapshotAny
Definition: tqual.h:28
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:233
List * ri_WithCheckOptions
Definition: execnodes.h:430
HeapTuple ConvertPartitionTupleSlot(TupleConversionMap *map, HeapTuple tuple, TupleTableSlot *new_slot, TupleTableSlot **p_my_slot)
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
TransitionCaptureState * MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
Definition: trigger.c:4643
#define ItemPointerIndicatesMovedPartitions(pointer)
Definition: itemptr.h:162
List * ri_PartitionCheck
Definition: execnodes.h:454
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:890
TupleDesc rd_att
Definition: rel.h:85
void EvalPlanQualInit(EPQState *epqstate, EState *estate, Plan *subplan, List *auxrowmarks, int epqParam)
Definition: execMain.c:2828
Plan * plan
Definition: execnodes.h:911
#define InvalidOid
Definition: postgres_ext.h:36
HTSU_Result heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, HeapUpdateFailureData *hufd, LockTupleMode *lockmode)
Definition: heapam.c:3519
ExecForeignUpdate_function ExecForeignUpdate
Definition: fdwapi.h:211
static TupleTableSlot * ExecDelete(ModifyTableState *mtstate, ItemPointer tupleid, HeapTuple oldtuple, TupleTableSlot *planSlot, EPQState *epqstate, EState *estate, bool *tupleDeleted, bool processReturning, bool canSetTag, bool changingPart)
List * lcons(void *datum, List *list)
Definition: list.c:259
#define makeNode(_type_)
Definition: nodes.h:565
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:222
static void ExecCheckPlanOutput(Relation resultRel, List *targetList)
bool ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, ResultRelInfo *relinfo, ItemPointer tupleid, HeapTuple fdw_trigtuple)
Definition: trigger.c:2730
#define HeapTupleIsHeapOnly(tuple)
Definition: htup_details.h:703
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
LockTupleMode ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo)
Definition: execMain.c:2379
#define EXEC_FLAG_MARK
Definition: executor.h:61
OnConflictAction onConflictAction
Definition: plannodes.h:235
Expr * expr
Definition: primnodes.h:1376
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
void heap_finish_speculative(Relation relation, HeapTuple tuple)
Definition: heapam.c:6155
uint64 es_processed
Definition: execnodes.h:529
#define HeapTupleHeaderGetXmin(tup)
Definition: htup_details.h:318
TupleConstr * constr
Definition: tupdesc.h:87
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:428
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
static int list_length(const List *l)
Definition: pg_list.h:89
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:218
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
TupleTableSlot * root_tuple_slot
HeapTuple ExecMaterializeSlot(TupleTableSlot *slot)
Definition: execTuples.c:781
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
List * targetlist
Definition: plannodes.h:146
TupleConversionMap ** parent_child_tupconv_maps
#define DatumGetPointer(X)
Definition: postgres.h:532
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:372
List * mt_excludedtlist
Definition: execnodes.h:1049
void ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition: trigger.c:2878
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
void * palloc(Size size)
Definition: mcxt.c:924
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:349
int errmsg(const char *fmt,...)
Definition: elog.c:797
CmdType operation
Definition: plannodes.h:219
Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno, bool *isNull)
Definition: execJunk.c:248
ResultRelInfo * es_root_result_relations
Definition: execnodes.h:501
void list_free(List *list)
Definition: list.c:1133
int i
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
bool ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate)
Definition: execMain.c:1862
List * returningLists
Definition: plannodes.h:230
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
bool isParent
Definition: plannodes.h:1045
TupleTableSlot * ExecBRInsertTriggers(EState *estate, ResultRelInfo *relinfo, TupleTableSlot *slot)
Definition: trigger.c:2515
ItemPointerData ctid
Definition: heapam.h:70
#define elog
Definition: elog.h:219
ResultRelInfo * ExecInitPartitionInfo(ModifyTableState *mtstate, ResultRelInfo *resultRelInfo, PartitionTupleRouting *proute, EState *estate, int partidx)
ExprContext * pi_exprContext
Definition: execnodes.h:327
BeginForeignModify_function BeginForeignModify
Definition: fdwapi.h:209
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486
OnConflictAction
Definition: nodes.h:807
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:58
#define RelationGetRelid(relation)
Definition: rel.h:407
TupleDesc oc_ProjTupdesc
Definition: execnodes.h:378
List * ri_onConflictArbiterIndexes
Definition: execnodes.h:448
CmdType
Definition: nodes.h:657
void ExecEndModifyTable(ModifyTableState *node)
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:403
ExecAuxRowMark * ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
Definition: execMain.c:2429
List * exclRelTlist
Definition: plannodes.h:240
List * ri_returningList
Definition: execnodes.h:442
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:324
static void ExecSetupChildParentMapForTcs(ModifyTableState *mtstate)
#define ResetExprContext(econtext)
Definition: executor.h:483
List ** mt_arowmarks
Definition: execnodes.h:1045
#define EvalPlanQualSetSlot(epqstate, slot)
Definition: executor.h:208
int epqParam
Definition: plannodes.h:234
bool trig_delete_before_row
Definition: reltrigger.h:65
Node * onConflictWhere
Definition: plannodes.h:238
ExecRowMark * ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
Definition: execMain.c:2405
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:456
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:492