PostgreSQL Source Code  git master
nodeModifyTable.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeModifyTable.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ExecComputeStoredGenerated (EState *estate, TupleTableSlot *slot)
 
ModifyTableStateExecInitModifyTable (ModifyTable *node, EState *estate, int eflags)
 
void ExecEndModifyTable (ModifyTableState *node)
 
void ExecReScanModifyTable (ModifyTableState *node)
 

Function Documentation

◆ ExecComputeStoredGenerated()

void ExecComputeStoredGenerated ( EState estate,
TupleTableSlot slot 
)

Definition at line 249 of file nodeModifyTable.c.

References Assert, build_column_default(), TupleDescData::constr, datumCopy(), ExprContext::ecxt_scantuple, elog, ERROR, EState::es_query_cxt, EState::es_result_relation_info, ExecClearTuple(), ExecEvalExpr(), ExecMaterializeSlot(), ExecPrepareExpr(), ExecStoreVirtualTuple(), GetPerTupleExprContext, GetPerTupleMemoryContext, TupleConstr::has_generated_stored, i, MemoryContextSwitchTo(), TupleDescData::natts, palloc(), RelationGetDescr, RelationGetRelationName, ResultRelInfo::ri_GeneratedExprs, ResultRelInfo::ri_RelationDesc, slot_getallattrs(), TupleTableSlot::tts_isnull, TupleTableSlot::tts_values, TupleDescAttr, val, and values.

Referenced by CopyFrom(), ExecInsert(), ExecSimpleRelationInsert(), ExecSimpleRelationUpdate(), and ExecUpdate().

250 {
251  ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
252  Relation rel = resultRelInfo->ri_RelationDesc;
253  TupleDesc tupdesc = RelationGetDescr(rel);
254  int natts = tupdesc->natts;
255  MemoryContext oldContext;
256  Datum *values;
257  bool *nulls;
258 
259  Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
260 
261  /*
262  * If first time through for this result relation, build expression
263  * nodetrees for rel's stored generation expressions. Keep them in the
264  * per-query memory context so they'll survive throughout the query.
265  */
266  if (resultRelInfo->ri_GeneratedExprs == NULL)
267  {
268  oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
269 
270  resultRelInfo->ri_GeneratedExprs =
271  (ExprState **) palloc(natts * sizeof(ExprState *));
272 
273  for (int i = 0; i < natts; i++)
274  {
275  if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
276  {
277  Expr *expr;
278 
279  expr = (Expr *) build_column_default(rel, i + 1);
280  if (expr == NULL)
281  elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
282  i + 1, RelationGetRelationName(rel));
283 
284  resultRelInfo->ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
285  }
286  }
287 
288  MemoryContextSwitchTo(oldContext);
289  }
290 
291  oldContext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
292 
293  values = palloc(sizeof(*values) * natts);
294  nulls = palloc(sizeof(*nulls) * natts);
295 
296  slot_getallattrs(slot);
297  memcpy(nulls, slot->tts_isnull, sizeof(*nulls) * natts);
298 
299  for (int i = 0; i < natts; i++)
300  {
301  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
302 
303  if (attr->attgenerated == ATTRIBUTE_GENERATED_STORED)
304  {
305  ExprContext *econtext;
306  Datum val;
307  bool isnull;
308 
309  econtext = GetPerTupleExprContext(estate);
310  econtext->ecxt_scantuple = slot;
311 
312  val = ExecEvalExpr(resultRelInfo->ri_GeneratedExprs[i], econtext, &isnull);
313 
314  values[i] = val;
315  nulls[i] = isnull;
316  }
317  else
318  {
319  if (!nulls[i])
320  values[i] = datumCopy(slot->tts_values[i], attr->attbyval, attr->attlen);
321  }
322  }
323 
324  ExecClearTuple(slot);
325  memcpy(slot->tts_values, values, sizeof(*values) * natts);
326  memcpy(slot->tts_isnull, nulls, sizeof(*nulls) * natts);
327  ExecStoreVirtualTuple(slot);
328  ExecMaterializeSlot(slot);
329 
330  MemoryContextSwitchTo(oldContext);
331 }
Relation ri_RelationDesc
Definition: execnodes.h:410
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define RelationGetDescr(relation)
Definition: rel.h:448
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:126
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:490
#define GetPerTupleExprContext(estate)
Definition: executor.h:501
bool has_generated_stored
Definition: tupdesc.h:45
MemoryContext es_query_cxt
Definition: execnodes.h:549
#define ERROR
Definition: elog.h:43
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:354
bool * tts_isnull
Definition: tuptable.h:128
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:285
TupleConstr * constr
Definition: tupdesc.h:85
ExprState ** ri_GeneratedExprs
Definition: execnodes.h:457
#define RelationGetRelationName(relation)
Definition: rel.h:456
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
Node * build_column_default(Relation rel, int attrno)
uintptr_t Datum
Definition: postgres.h:367
static void ExecMaterializeSlot(TupleTableSlot *slot)
Definition: tuptable.h:443
#define Assert(condition)
Definition: c.h:739
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:223
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:506
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i
long val
Definition: informix.c:664
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:521

◆ ExecEndModifyTable()

void ExecEndModifyTable ( ModifyTableState node)

Definition at line 2732 of file nodeModifyTable.c.

References FdwRoutine::EndForeignModify, EvalPlanQualEnd(), ExecCleanupTupleRouting(), ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecEndNode(), ExecFreeExprContext(), i, ModifyTableState::mt_epqstate, ModifyTableState::mt_nplans, ModifyTableState::mt_partition_tuple_routing, ModifyTableState::mt_plans, ModifyTableState::mt_root_tuple_slot, ModifyTableState::ps, PlanState::ps_ResultTupleSlot, ModifyTableState::resultRelInfo, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_usesFdwDirectModify, and PlanState::state.

Referenced by ExecEndNode().

2733 {
2734  int i;
2735 
2736  /*
2737  * Allow any FDWs to shut down
2738  */
2739  for (i = 0; i < node->mt_nplans; i++)
2740  {
2741  ResultRelInfo *resultRelInfo = node->resultRelInfo + i;
2742 
2743  if (!resultRelInfo->ri_usesFdwDirectModify &&
2744  resultRelInfo->ri_FdwRoutine != NULL &&
2745  resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
2746  resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
2747  resultRelInfo);
2748  }
2749 
2750  /*
2751  * Close all the partitioned tables, leaf partitions, and their indices
2752  * and release the slot used for tuple routing, if set.
2753  */
2754  if (node->mt_partition_tuple_routing)
2755  {
2757 
2758  if (node->mt_root_tuple_slot)
2760  }
2761 
2762  /*
2763  * Free the exprcontext
2764  */
2765  ExecFreeExprContext(&node->ps);
2766 
2767  /*
2768  * clean out the tuple table
2769  */
2770  if (node->ps.ps_ResultTupleSlot)
2772 
2773  /*
2774  * Terminate EPQ execution if active
2775  */
2776  EvalPlanQualEnd(&node->mt_epqstate);
2777 
2778  /*
2779  * shut down subplans
2780  */
2781  for (i = 0; i < node->mt_nplans; i++)
2782  ExecEndNode(node->mt_plans[i]);
2783 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
struct PartitionTupleRouting * mt_partition_tuple_routing
Definition: execnodes.h:1183
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:537
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1168
EState * state
Definition: execnodes.h:941
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:614
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:2944
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
PlanState ps
Definition: execnodes.h:1159
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
bool ri_usesFdwDirectModify
Definition: execnodes.h:445
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
EPQState mt_epqstate
Definition: execnodes.h:1172
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:439
EndForeignModify_function EndForeignModify
Definition: fdwapi.h:214
PlanState ** mt_plans
Definition: execnodes.h:1163
TupleTableSlot * mt_root_tuple_slot
Definition: execnodes.h:1180
int i

◆ ExecInitModifyTable()

ModifyTableState* ExecInitModifyTable ( ModifyTable node,
EState estate,
int  eflags 
)

Definition at line 2264 of file nodeModifyTable.c.

References ModifyTable::arbiterIndexes, Assert, AttributeNumberIsValid, FdwRoutine::BeginForeignModify, bms_is_member(), ModifyTable::canSetTag, ModifyTableState::canSetTag, CheckValidResultRel(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, elog, ModifyTable::epqParam, ERROR, EState::es_auxmodifytables, EState::es_result_relation_info, EState::es_result_relations, EState::es_root_result_relations, EState::es_tupleTable, EvalPlanQualInit(), EvalPlanQualSetPlan(), ModifyTable::exclRelTlist, EXEC_FLAG_BACKWARD, EXEC_FLAG_EXPLAIN_ONLY, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecBuildAuxRowMark(), ExecBuildProjectionInfo(), ExecCheckPlanOutput(), ExecFindJunkAttribute(), ExecFindRowMark(), ExecGetResultType(), ExecInitExtraTupleSlot(), ExecInitJunkFilter(), ExecInitNode(), ExecInitQual(), ExecInitResultTupleSlotTL(), ExecInitResultTypeTL(), ExecModifyTable(), ExecOpenIndices(), PlanState::ExecProcNode, ExecSetupChildParentMapForSubplan(), ExecSetupPartitionTupleRouting(), ExecSetupTransitionCaptureState(), ExecTypeFromTL(), ModifyTable::fdwDirectModifyPlans, ModifyTable::fdwPrivLists, ModifyTableState::fireBSTriggers, getTargetResultRelInfo(), i, PlanRowMark::isParent, JunkFilter::jf_junkAttNo, lappend(), lcons(), lfirst, lfirst_node, linitial, list_length(), list_nth(), makeNode, ModifyTableState::mt_arowmarks, ModifyTableState::mt_done, ModifyTableState::mt_epqstate, ModifyTableState::mt_excludedtlist, ModifyTableState::mt_nplans, ModifyTableState::mt_partition_tuple_routing, ModifyTableState::mt_plans, ModifyTableState::mt_root_tuple_slot, ModifyTableState::mt_scans, ModifyTableState::mt_whichplan, NIL, OnConflictSetState::oc_Existing, OnConflictSetState::oc_ProjInfo, OnConflictSetState::oc_ProjSlot, OnConflictSetState::oc_WhereClause, ONCONFLICT_NONE, ONCONFLICT_UPDATE, ModifyTable::onConflictAction, ModifyTable::onConflictSet, ModifyTable::onConflictWhere, ModifyTable::operation, ModifyTableState::operation, palloc0(), ModifyTable::partColsUpdated, PlanState::plan, ModifyTable::plans, ModifyTableState::ps, PlanState::ps_ExprContext, PlanState::ps_ResultTupleSlot, WithCheckOption::qual, RelationData::rd_att, RelationData::rd_rel, relkind, TargetEntry::resjunk, ModifyTable::resultRelIndex, ModifyTableState::resultRelInfo, ModifyTable::returningLists, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_junkFilter, ResultRelInfo::ri_onConflict, ResultRelInfo::ri_onConflictArbiterIndexes, ResultRelInfo::ri_projectReturning, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_returningList, ResultRelInfo::ri_TrigDesc, ResultRelInfo::ri_usesFdwDirectModify, ResultRelInfo::ri_WithCheckOptionExprs, ResultRelInfo::ri_WithCheckOptions, ModifyTable::rootResultRelIndex, ModifyTableState::rootResultRelInfo, ModifyTable::rowMarks, PlanRowMark::rti, PlanState::state, table_slot_callbacks(), table_slot_create(), Plan::targetlist, TriggerDesc::trig_update_before_row, TTSOpsVirtual, and ModifyTable::withCheckOptionLists.

Referenced by ExecInitNode().

2265 {
2266  ModifyTableState *mtstate;
2267  CmdType operation = node->operation;
2268  int nplans = list_length(node->plans);
2269  ResultRelInfo *saved_resultRelInfo;
2270  ResultRelInfo *resultRelInfo;
2271  Plan *subplan;
2272  ListCell *l;
2273  int i;
2274  Relation rel;
2275  bool update_tuple_routing_needed = node->partColsUpdated;
2276 
2277  /* check for unsupported flags */
2278  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
2279 
2280  /*
2281  * create state structure
2282  */
2283  mtstate = makeNode(ModifyTableState);
2284  mtstate->ps.plan = (Plan *) node;
2285  mtstate->ps.state = estate;
2286  mtstate->ps.ExecProcNode = ExecModifyTable;
2287 
2288  mtstate->operation = operation;
2289  mtstate->canSetTag = node->canSetTag;
2290  mtstate->mt_done = false;
2291 
2292  mtstate->mt_plans = (PlanState **) palloc0(sizeof(PlanState *) * nplans);
2293  mtstate->resultRelInfo = estate->es_result_relations + node->resultRelIndex;
2294  mtstate->mt_scans = (TupleTableSlot **) palloc0(sizeof(TupleTableSlot *) * nplans);
2295 
2296  /* If modifying a partitioned table, initialize the root table info */
2297  if (node->rootResultRelIndex >= 0)
2298  mtstate->rootResultRelInfo = estate->es_root_result_relations +
2299  node->rootResultRelIndex;
2300 
2301  mtstate->mt_arowmarks = (List **) palloc0(sizeof(List *) * nplans);
2302  mtstate->mt_nplans = nplans;
2303 
2304  /* set up epqstate with dummy subplan data for the moment */
2305  EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL, node->epqParam);
2306  mtstate->fireBSTriggers = true;
2307 
2308  /*
2309  * call ExecInitNode on each of the plans to be executed and save the
2310  * results into the array "mt_plans". This is also a convenient place to
2311  * verify that the proposed target relations are valid and open their
2312  * indexes for insertion of new index entries. Note we *must* set
2313  * estate->es_result_relation_info correctly while we initialize each
2314  * sub-plan; external modules such as FDWs may depend on that (see
2315  * contrib/postgres_fdw/postgres_fdw.c: postgresBeginDirectModify() as one
2316  * example).
2317  */
2318  saved_resultRelInfo = estate->es_result_relation_info;
2319 
2320  resultRelInfo = mtstate->resultRelInfo;
2321  i = 0;
2322  foreach(l, node->plans)
2323  {
2324  subplan = (Plan *) lfirst(l);
2325 
2326  /* Initialize the usesFdwDirectModify flag */
2327  resultRelInfo->ri_usesFdwDirectModify = bms_is_member(i,
2328  node->fdwDirectModifyPlans);
2329 
2330  /*
2331  * Verify result relation is a valid target for the current operation
2332  */
2333  CheckValidResultRel(resultRelInfo, operation);
2334 
2335  /*
2336  * If there are indices on the result relation, open them and save
2337  * descriptors in the result relation info, so that we can add new
2338  * index entries for the tuples we add/update. We need not do this
2339  * for a DELETE, however, since deletion doesn't affect indexes. Also,
2340  * inside an EvalPlanQual operation, the indexes might be open
2341  * already, since we share the resultrel state with the original
2342  * query.
2343  */
2344  if (resultRelInfo->ri_RelationDesc->rd_rel->relhasindex &&
2345  operation != CMD_DELETE &&
2346  resultRelInfo->ri_IndexRelationDescs == NULL)
2347  ExecOpenIndices(resultRelInfo,
2349 
2350  /*
2351  * If this is an UPDATE and a BEFORE UPDATE trigger is present, the
2352  * trigger itself might modify the partition-key values. So arrange
2353  * for tuple routing.
2354  */
2355  if (resultRelInfo->ri_TrigDesc &&
2356  resultRelInfo->ri_TrigDesc->trig_update_before_row &&
2357  operation == CMD_UPDATE)
2358  update_tuple_routing_needed = true;
2359 
2360  /* Now init the plan for this result rel */
2361  estate->es_result_relation_info = resultRelInfo;
2362  mtstate->mt_plans[i] = ExecInitNode(subplan, estate, eflags);
2363  mtstate->mt_scans[i] =
2364  ExecInitExtraTupleSlot(mtstate->ps.state, ExecGetResultType(mtstate->mt_plans[i]),
2365  table_slot_callbacks(resultRelInfo->ri_RelationDesc));
2366 
2367  /* Also let FDWs init themselves for foreign-table result rels */
2368  if (!resultRelInfo->ri_usesFdwDirectModify &&
2369  resultRelInfo->ri_FdwRoutine != NULL &&
2370  resultRelInfo->ri_FdwRoutine->BeginForeignModify != NULL)
2371  {
2372  List *fdw_private = (List *) list_nth(node->fdwPrivLists, i);
2373 
2374  resultRelInfo->ri_FdwRoutine->BeginForeignModify(mtstate,
2375  resultRelInfo,
2376  fdw_private,
2377  i,
2378  eflags);
2379  }
2380 
2381  resultRelInfo++;
2382  i++;
2383  }
2384 
2385  estate->es_result_relation_info = saved_resultRelInfo;
2386 
2387  /* Get the target relation */
2388  rel = (getTargetResultRelInfo(mtstate))->ri_RelationDesc;
2389 
2390  /*
2391  * If it's not a partitioned table after all, UPDATE tuple routing should
2392  * not be attempted.
2393  */
2394  if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
2395  update_tuple_routing_needed = false;
2396 
2397  /*
2398  * Build state for tuple routing if it's an INSERT or if it's an UPDATE of
2399  * partition key.
2400  */
2401  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
2402  (operation == CMD_INSERT || update_tuple_routing_needed))
2403  mtstate->mt_partition_tuple_routing =
2404  ExecSetupPartitionTupleRouting(estate, mtstate, rel);
2405 
2406  /*
2407  * Build state for collecting transition tuples. This requires having a
2408  * valid trigger query context, so skip it in explain-only mode.
2409  */
2410  if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
2411  ExecSetupTransitionCaptureState(mtstate, estate);
2412 
2413  /*
2414  * Construct mapping from each of the per-subplan partition attnos to the
2415  * root attno. This is required when during update row movement the tuple
2416  * descriptor of a source partition does not match the root partitioned
2417  * table descriptor. In such a case we need to convert tuples to the root
2418  * tuple descriptor, because the search for destination partition starts
2419  * from the root. We'll also need a slot to store these converted tuples.
2420  * We can skip this setup if it's not a partition key update.
2421  */
2422  if (update_tuple_routing_needed)
2423  {
2425  mtstate->mt_root_tuple_slot = table_slot_create(rel, NULL);
2426  }
2427 
2428  /*
2429  * Initialize any WITH CHECK OPTION constraints if needed.
2430  */
2431  resultRelInfo = mtstate->resultRelInfo;
2432  i = 0;
2433  foreach(l, node->withCheckOptionLists)
2434  {
2435  List *wcoList = (List *) lfirst(l);
2436  List *wcoExprs = NIL;
2437  ListCell *ll;
2438 
2439  foreach(ll, wcoList)
2440  {
2441  WithCheckOption *wco = (WithCheckOption *) lfirst(ll);
2442  ExprState *wcoExpr = ExecInitQual((List *) wco->qual,
2443  &mtstate->ps);
2444 
2445  wcoExprs = lappend(wcoExprs, wcoExpr);
2446  }
2447 
2448  resultRelInfo->ri_WithCheckOptions = wcoList;
2449  resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
2450  resultRelInfo++;
2451  i++;
2452  }
2453 
2454  /*
2455  * Initialize RETURNING projections if needed.
2456  */
2457  if (node->returningLists)
2458  {
2459  TupleTableSlot *slot;
2460  ExprContext *econtext;
2461 
2462  /*
2463  * Initialize result tuple slot and assign its rowtype using the first
2464  * RETURNING list. We assume the rest will look the same.
2465  */
2466  mtstate->ps.plan->targetlist = (List *) linitial(node->returningLists);
2467 
2468  /* Set up a slot for the output of the RETURNING projection(s) */
2470  slot = mtstate->ps.ps_ResultTupleSlot;
2471 
2472  /* Need an econtext too */
2473  if (mtstate->ps.ps_ExprContext == NULL)
2474  ExecAssignExprContext(estate, &mtstate->ps);
2475  econtext = mtstate->ps.ps_ExprContext;
2476 
2477  /*
2478  * Build a projection for each result rel.
2479  */
2480  resultRelInfo = mtstate->resultRelInfo;
2481  foreach(l, node->returningLists)
2482  {
2483  List *rlist = (List *) lfirst(l);
2484 
2485  resultRelInfo->ri_returningList = rlist;
2486  resultRelInfo->ri_projectReturning =
2487  ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
2488  resultRelInfo->ri_RelationDesc->rd_att);
2489  resultRelInfo++;
2490  }
2491  }
2492  else
2493  {
2494  /*
2495  * We still must construct a dummy result tuple type, because InitPlan
2496  * expects one (maybe should change that?).
2497  */
2498  mtstate->ps.plan->targetlist = NIL;
2499  ExecInitResultTypeTL(&mtstate->ps);
2500 
2501  mtstate->ps.ps_ExprContext = NULL;
2502  }
2503 
2504  /* Set the list of arbiter indexes if needed for ON CONFLICT */
2505  resultRelInfo = mtstate->resultRelInfo;
2506  if (node->onConflictAction != ONCONFLICT_NONE)
2507  resultRelInfo->ri_onConflictArbiterIndexes = node->arbiterIndexes;
2508 
2509  /*
2510  * If needed, Initialize target list, projection and qual for ON CONFLICT
2511  * DO UPDATE.
2512  */
2513  if (node->onConflictAction == ONCONFLICT_UPDATE)
2514  {
2515  ExprContext *econtext;
2516  TupleDesc relationDesc;
2517  TupleDesc tupDesc;
2518 
2519  /* insert may only have one plan, inheritance is not expanded */
2520  Assert(nplans == 1);
2521 
2522  /* already exists if created by RETURNING processing above */
2523  if (mtstate->ps.ps_ExprContext == NULL)
2524  ExecAssignExprContext(estate, &mtstate->ps);
2525 
2526  econtext = mtstate->ps.ps_ExprContext;
2527  relationDesc = resultRelInfo->ri_RelationDesc->rd_att;
2528 
2529  /* carried forward solely for the benefit of explain */
2530  mtstate->mt_excludedtlist = node->exclRelTlist;
2531 
2532  /* create state for DO UPDATE SET operation */
2533  resultRelInfo->ri_onConflict = makeNode(OnConflictSetState);
2534 
2535  /* initialize slot for the existing tuple */
2536  resultRelInfo->ri_onConflict->oc_Existing =
2537  table_slot_create(resultRelInfo->ri_RelationDesc,
2538  &mtstate->ps.state->es_tupleTable);
2539 
2540  /*
2541  * Create the tuple slot for the UPDATE SET projection. We want a slot
2542  * of the table's type here, because the slot will be used to insert
2543  * into the table, and for RETURNING processing - which may access
2544  * system attributes.
2545  */
2546  tupDesc = ExecTypeFromTL((List *) node->onConflictSet);
2547  resultRelInfo->ri_onConflict->oc_ProjSlot =
2548  ExecInitExtraTupleSlot(mtstate->ps.state, tupDesc,
2549  table_slot_callbacks(resultRelInfo->ri_RelationDesc));
2550 
2551  /* build UPDATE SET projection state */
2552  resultRelInfo->ri_onConflict->oc_ProjInfo =
2553  ExecBuildProjectionInfo(node->onConflictSet, econtext,
2554  resultRelInfo->ri_onConflict->oc_ProjSlot,
2555  &mtstate->ps,
2556  relationDesc);
2557 
2558  /* initialize state to evaluate the WHERE clause, if any */
2559  if (node->onConflictWhere)
2560  {
2561  ExprState *qualexpr;
2562 
2563  qualexpr = ExecInitQual((List *) node->onConflictWhere,
2564  &mtstate->ps);
2565  resultRelInfo->ri_onConflict->oc_WhereClause = qualexpr;
2566  }
2567  }
2568 
2569  /*
2570  * If we have any secondary relations in an UPDATE or DELETE, they need to
2571  * be treated like non-locked relations in SELECT FOR UPDATE, ie, the
2572  * EvalPlanQual mechanism needs to be told about them. Locate the
2573  * relevant ExecRowMarks.
2574  */
2575  foreach(l, node->rowMarks)
2576  {
2578  ExecRowMark *erm;
2579 
2580  /* ignore "parent" rowmarks; they are irrelevant at runtime */
2581  if (rc->isParent)
2582  continue;
2583 
2584  /* find ExecRowMark (same for all subplans) */
2585  erm = ExecFindRowMark(estate, rc->rti, false);
2586 
2587  /* build ExecAuxRowMark for each subplan */
2588  for (i = 0; i < nplans; i++)
2589  {
2590  ExecAuxRowMark *aerm;
2591 
2592  subplan = mtstate->mt_plans[i]->plan;
2593  aerm = ExecBuildAuxRowMark(erm, subplan->targetlist);
2594  mtstate->mt_arowmarks[i] = lappend(mtstate->mt_arowmarks[i], aerm);
2595  }
2596  }
2597 
2598  /* select first subplan */
2599  mtstate->mt_whichplan = 0;
2600  subplan = (Plan *) linitial(node->plans);
2601  EvalPlanQualSetPlan(&mtstate->mt_epqstate, subplan,
2602  mtstate->mt_arowmarks[0]);
2603 
2604  /*
2605  * Initialize the junk filter(s) if needed. INSERT queries need a filter
2606  * if there are any junk attrs in the tlist. UPDATE and DELETE always
2607  * need a filter, since there's always at least one junk attribute present
2608  * --- no need to look first. Typically, this will be a 'ctid' or
2609  * 'wholerow' attribute, but in the case of a foreign data wrapper it
2610  * might be a set of junk attributes sufficient to identify the remote
2611  * row.
2612  *
2613  * If there are multiple result relations, each one needs its own junk
2614  * filter. Note multiple rels are only possible for UPDATE/DELETE, so we
2615  * can't be fooled by some needing a filter and some not.
2616  *
2617  * This section of code is also a convenient place to verify that the
2618  * output of an INSERT or UPDATE matches the target table(s).
2619  */
2620  {
2621  bool junk_filter_needed = false;
2622 
2623  switch (operation)
2624  {
2625  case CMD_INSERT:
2626  foreach(l, subplan->targetlist)
2627  {
2628  TargetEntry *tle = (TargetEntry *) lfirst(l);
2629 
2630  if (tle->resjunk)
2631  {
2632  junk_filter_needed = true;
2633  break;
2634  }
2635  }
2636  break;
2637  case CMD_UPDATE:
2638  case CMD_DELETE:
2639  junk_filter_needed = true;
2640  break;
2641  default:
2642  elog(ERROR, "unknown operation");
2643  break;
2644  }
2645 
2646  if (junk_filter_needed)
2647  {
2648  resultRelInfo = mtstate->resultRelInfo;
2649  for (i = 0; i < nplans; i++)
2650  {
2651  JunkFilter *j;
2652  TupleTableSlot *junkresslot;
2653 
2654  subplan = mtstate->mt_plans[i]->plan;
2655  if (operation == CMD_INSERT || operation == CMD_UPDATE)
2656  ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
2657  subplan->targetlist);
2658 
2659  junkresslot =
2660  ExecInitExtraTupleSlot(estate, NULL,
2661  table_slot_callbacks(resultRelInfo->ri_RelationDesc));
2662  j = ExecInitJunkFilter(subplan->targetlist,
2663  junkresslot);
2664 
2665  if (operation == CMD_UPDATE || operation == CMD_DELETE)
2666  {
2667  /* For UPDATE/DELETE, find the appropriate junk attr now */
2668  char relkind;
2669 
2670  relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
2671  if (relkind == RELKIND_RELATION ||
2672  relkind == RELKIND_MATVIEW ||
2673  relkind == RELKIND_PARTITIONED_TABLE)
2674  {
2675  j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
2677  elog(ERROR, "could not find junk ctid column");
2678  }
2679  else if (relkind == RELKIND_FOREIGN_TABLE)
2680  {
2681  /*
2682  * When there is a row-level trigger, there should be
2683  * a wholerow attribute.
2684  */
2685  j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
2686  }
2687  else
2688  {
2689  j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
2691  elog(ERROR, "could not find junk wholerow column");
2692  }
2693  }
2694 
2695  resultRelInfo->ri_junkFilter = j;
2696  resultRelInfo++;
2697  }
2698  }
2699  else
2700  {
2701  if (operation == CMD_INSERT)
2703  subplan->targetlist);
2704  }
2705  }
2706 
2707  /*
2708  * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it
2709  * to estate->es_auxmodifytables so that it will be run to completion by
2710  * ExecPostprocessPlan. (It'd actually work fine to add the primary
2711  * ModifyTable node too, but there's no need.) Note the use of lcons not
2712  * lappend: we need later-initialized ModifyTable nodes to be shut down
2713  * before earlier ones. This ensures that we don't throw away RETURNING
2714  * rows that need to be seen by a later CTE subplan.
2715  */
2716  if (!mtstate->canSetTag)
2717  estate->es_auxmodifytables = lcons(mtstate,
2718  estate->es_auxmodifytables);
2719 
2720  return mtstate;
2721 }
TupleTableSlot * table_slot_create(Relation relation, List **reglist)
Definition: tableam.c:77
AttrNumber jf_junkAttNo
Definition: execnodes.h:370
#define NIL
Definition: pg_list.h:65
JunkFilter * ri_junkFilter
Definition: execnodes.h:460
List * arbiterIndexes
Definition: plannodes.h:235
Relation ri_RelationDesc
Definition: execnodes.h:410
Bitmapset * fdwDirectModifyPlans
Definition: plannodes.h:231
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1801
static ResultRelInfo * getTargetResultRelInfo(ModifyTableState *node)
struct PartitionTupleRouting * mt_partition_tuple_routing
Definition: execnodes.h:1183
List * withCheckOptionLists
Definition: plannodes.h:228
int resultRelIndex
Definition: plannodes.h:225
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1168
AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter, const char *attrName)
Definition: execJunk.c:208
ExprContext * ps_ExprContext
Definition: execnodes.h:978
TupleTableSlot ** mt_scans
Definition: execnodes.h:1166
const TupleTableSlotOps * table_slot_callbacks(Relation relation)
Definition: tableam.c:44
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
bool partColsUpdated
Definition: plannodes.h:223
bool canSetTag
Definition: plannodes.h:220
CmdType operation
Definition: execnodes.h:1160
ResultRelInfo * rootResultRelInfo
Definition: execnodes.h:1169
void EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
Definition: execMain.c:2522
EState * state
Definition: execnodes.h:941
Form_pg_class rd_rel
Definition: rel.h:83
List * plans
Definition: plannodes.h:227
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:207
char relkind
Definition: pg_class.h:81
List * onConflictSet
Definition: plannodes.h:236
int rootResultRelIndex
Definition: plannodes.h:226
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:151
List * ri_WithCheckOptionExprs
Definition: execnodes.h:451
TupleTableSlot * oc_Existing
Definition: execnodes.h:382
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
static void ExecSetupChildParentMapForSubplan(ModifyTableState *mtstate)
List * rowMarks
Definition: plannodes.h:232
bool resjunk
Definition: primnodes.h:1400
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:1159
ProjectionInfo * oc_ProjInfo
Definition: execnodes.h:384
static void * list_nth(const List *list, int n)
Definition: pg_list.h:277
bool ri_usesFdwDirectModify
Definition: execnodes.h:445
static void ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
Definition: execMain.c:1076
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define lfirst_node(type, lc)
Definition: pg_list.h:193
ResultRelInfo * es_result_relations
Definition: execnodes.h:519
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1725
List * fdwPrivLists
Definition: plannodes.h:230
EPQState mt_epqstate
Definition: execnodes.h:1172
bool trig_update_before_row
Definition: reltrigger.h:60
ProjectionInfo * ri_projectReturning
Definition: execnodes.h:466
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:439
ExprState * oc_WhereClause
Definition: execnodes.h:385
PartitionTupleRouting * ExecSetupPartitionTupleRouting(EState *estate, ModifyTableState *mtstate, Relation rel)
TriggerDesc * ri_TrigDesc
Definition: execnodes.h:422
List * lappend(List *list, void *datum)
Definition: list.c:322
PlanState ** mt_plans
Definition: execnodes.h:1163
void EvalPlanQualInit(EPQState *epqstate, EState *parentestate, Plan *subplan, List *auxrowmarks, int epqParam)
Definition: execMain.c:2484
OnConflictSetState * ri_onConflict
Definition: execnodes.h:472
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
static TupleTableSlot * ExecModifyTable(PlanState *pstate)
List * es_tupleTable
Definition: execnodes.h:551
void * palloc0(Size size)
Definition: mcxt.c:980
List * es_auxmodifytables
Definition: execnodes.h:563
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:945
List * ri_WithCheckOptions
Definition: execnodes.h:448
TupleDesc rd_att
Definition: rel.h:84
Plan * plan
Definition: execnodes.h:939
TupleTableSlot * oc_ProjSlot
Definition: execnodes.h:383
List * lcons(void *datum, List *list)
Definition: list.c:454
#define makeNode(_type_)
Definition: nodes.h:573
static void ExecCheckPlanOutput(Relation resultRel, List *targetList)
#define Assert(condition)
Definition: c.h:739
#define lfirst(lc)
Definition: pg_list.h:190
#define EXEC_FLAG_MARK
Definition: executor.h:59
OnConflictAction onConflictAction
Definition: plannodes.h:234
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:444
static int list_length(const List *l)
Definition: pg_list.h:169
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1908
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1769
TupleTableSlot * mt_root_tuple_slot
Definition: execnodes.h:1180
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:454
List * targetlist
Definition: plannodes.h:140
List * mt_excludedtlist
Definition: execnodes.h:1174
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:351
CmdType operation
Definition: plannodes.h:219
ResultRelInfo * es_root_result_relations
Definition: execnodes.h:529
#define elog(elevel,...)
Definition: elog.h:228
int i
List * returningLists
Definition: plannodes.h:229
bool isParent
Definition: plannodes.h:1064
BeginForeignModify_function BeginForeignModify
Definition: fdwapi.h:210
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:138
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:56
JunkFilter * ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
Definition: execJunk.c:60
List * ri_onConflictArbiterIndexes
Definition: execnodes.h:469
CmdType
Definition: nodes.h:668
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:416
ExecAuxRowMark * ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
Definition: execMain.c:2360
List * exclRelTlist
Definition: plannodes.h:239
List * ri_returningList
Definition: execnodes.h:463
List ** mt_arowmarks
Definition: execnodes.h:1171
int epqParam
Definition: plannodes.h:233
Node * onConflictWhere
Definition: plannodes.h:237
ExecRowMark * ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
Definition: execMain.c:2337
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:521

◆ ExecReScanModifyTable()

void ExecReScanModifyTable ( ModifyTableState node)

Definition at line 2786 of file nodeModifyTable.c.

References elog, and ERROR.

Referenced by ExecReScan().

2787 {
2788  /*
2789  * Currently, we don't need to support rescan on ModifyTable nodes. The
2790  * semantics of that would be a bit debatable anyway.
2791  */
2792  elog(ERROR, "ExecReScanModifyTable is not implemented");
2793 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228