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:411
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:426
#define RelationGetDescr(relation)
Definition: rel.h:445
#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:550
#define ERROR
Definition: elog.h:43
static void slot_getallattrs(TupleTableSlot *slot)
Definition: tuptable.h:355
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:458
#define RelationGetRelationName(relation)
Definition: rel.h:453
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:444
#define Assert(condition)
Definition: c.h:732
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:224
#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:226
int i
long val
Definition: informix.c:684
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1517
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:522

◆ ExecEndModifyTable()

void ExecEndModifyTable ( ModifyTableState node)

Definition at line 2733 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().

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

◆ ExecInitModifyTable()

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

Definition at line 2265 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().

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

◆ ExecReScanModifyTable()

void ExecReScanModifyTable ( ModifyTableState node)

Definition at line 2787 of file nodeModifyTable.c.

References elog, and ERROR.

Referenced by ExecReScan().

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