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, CmdType cmdtype)
 
ModifyTableStateExecInitModifyTable (ModifyTable *node, EState *estate, int eflags)
 
void ExecEndModifyTable (ModifyTableState *node)
 
void ExecReScanModifyTable (ModifyTableState *node)
 

Function Documentation

◆ ExecComputeStoredGenerated()

void ExecComputeStoredGenerated ( EState estate,
TupleTableSlot slot,
CmdType  cmdtype 
)

Definition at line 249 of file nodeModifyTable.c.

References Assert, bms_is_member(), build_column_default(), CMD_UPDATE, TupleDescData::constr, datumCopy(), ExprContext::ecxt_scantuple, elog, ERROR, EState::es_query_cxt, EState::es_result_relation_info, exec_rt_fetch(), ExecClearTuple(), ExecEvalExpr(), ExecMaterializeSlot(), ExecPrepareExpr(), ExecStoreVirtualTuple(), RangeTblEntry::extraUpdatedCols, FirstLowInvalidHeapAttributeNumber, GetPerTupleExprContext, GetPerTupleMemoryContext, TupleConstr::has_generated_stored, i, MemoryContextSwitchTo(), TupleDescData::natts, palloc(), RelationGetDescr, RelationGetRelationName, ResultRelInfo::ri_GeneratedExprs, ResultRelInfo::ri_NumGeneratedNeeded, ResultRelInfo::ri_RangeTableIndex, ResultRelInfo::ri_RelationDesc, slot_getallattrs(), TriggerDesc::trig_update_before_row, RelationData::trigdesc, 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  resultRelInfo->ri_NumGeneratedNeeded = 0;
273 
274  for (int i = 0; i < natts; i++)
275  {
276  if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
277  {
278  Expr *expr;
279 
280  /*
281  * If it's an update and the current column was not marked as
282  * being updated, then we can skip the computation. But if
283  * there is a BEFORE ROW UPDATE trigger, we cannot skip
284  * because the trigger might affect additional columns.
285  */
286  if (cmdtype == CMD_UPDATE &&
287  !(rel->trigdesc && rel->trigdesc->trig_update_before_row) &&
289  exec_rt_fetch(resultRelInfo->ri_RangeTableIndex, estate)->extraUpdatedCols))
290  {
291  resultRelInfo->ri_GeneratedExprs[i] = NULL;
292  continue;
293  }
294 
295  expr = (Expr *) build_column_default(rel, i + 1);
296  if (expr == NULL)
297  elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
298  i + 1, RelationGetRelationName(rel));
299 
300  resultRelInfo->ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
301  resultRelInfo->ri_NumGeneratedNeeded++;
302  }
303  }
304 
305  MemoryContextSwitchTo(oldContext);
306  }
307 
308  /*
309  * If no generated columns have been affected by this change, then skip
310  * the rest.
311  */
312  if (resultRelInfo->ri_NumGeneratedNeeded == 0)
313  return;
314 
315  oldContext = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
316 
317  values = palloc(sizeof(*values) * natts);
318  nulls = palloc(sizeof(*nulls) * natts);
319 
320  slot_getallattrs(slot);
321  memcpy(nulls, slot->tts_isnull, sizeof(*nulls) * natts);
322 
323  for (int i = 0; i < natts; i++)
324  {
325  Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
326 
327  if (attr->attgenerated == ATTRIBUTE_GENERATED_STORED &&
328  resultRelInfo->ri_GeneratedExprs[i])
329  {
330  ExprContext *econtext;
331  Datum val;
332  bool isnull;
333 
334  econtext = GetPerTupleExprContext(estate);
335  econtext->ecxt_scantuple = slot;
336 
337  val = ExecEvalExpr(resultRelInfo->ri_GeneratedExprs[i], econtext, &isnull);
338 
339  values[i] = val;
340  nulls[i] = isnull;
341  }
342  else
343  {
344  if (!nulls[i])
345  values[i] = datumCopy(slot->tts_values[i], attr->attbyval, attr->attlen);
346  }
347  }
348 
349  ExecClearTuple(slot);
350  memcpy(slot->tts_values, values, sizeof(*values) * natts);
351  memcpy(slot->tts_isnull, nulls, sizeof(*nulls) * natts);
352  ExecStoreVirtualTuple(slot);
353  ExecMaterializeSlot(slot);
354 
355  MemoryContextSwitchTo(oldContext);
356 }
Relation ri_RelationDesc
Definition: execnodes.h:411
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define RelationGetDescr(relation)
Definition: rel.h:454
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum * tts_values
Definition: tuptable.h:126
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
ExprState * ExecPrepareExpr(Expr *node, EState *estate)
Definition: execExpr.c:490
Index ri_RangeTableIndex
Definition: execnodes.h:408
#define GetPerTupleExprContext(estate)
Definition: executor.h:506
bool has_generated_stored
Definition: tupdesc.h:45
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1122
MemoryContext es_query_cxt
Definition: execnodes.h:553
#define ERROR
Definition: elog.h:43
TriggerDesc * trigdesc
Definition: rel.h:90
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:290
TupleConstr * constr
Definition: tupdesc.h:85
ExprState ** ri_GeneratedExprs
Definition: execnodes.h:458
bool trig_update_before_row
Definition: reltrigger.h:60
#define RelationGetRelationName(relation)
Definition: rel.h:462
static RangeTblEntry * exec_rt_fetch(Index rti, EState *estate)
Definition: executor.h:542
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:130
int ri_NumGeneratedNeeded
Definition: execnodes.h:461
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:738
TupleTableSlot * ecxt_scantuple
Definition: execnodes.h:224
#define GetPerTupleMemoryContext(estate)
Definition: executor.h:511
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
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
long val
Definition: informix.c:664
TupleTableSlot * ExecStoreVirtualTuple(TupleTableSlot *slot)
Definition: execTuples.c:1522
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:525

◆ ExecEndModifyTable()

void ExecEndModifyTable ( ModifyTableState node)

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

2755 {
2756  int i;
2757 
2758  /*
2759  * Allow any FDWs to shut down
2760  */
2761  for (i = 0; i < node->mt_nplans; i++)
2762  {
2763  ResultRelInfo *resultRelInfo = node->resultRelInfo + i;
2764 
2765  if (!resultRelInfo->ri_usesFdwDirectModify &&
2766  resultRelInfo->ri_FdwRoutine != NULL &&
2767  resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
2768  resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
2769  resultRelInfo);
2770  }
2771 
2772  /*
2773  * Close all the partitioned tables, leaf partitions, and their indices
2774  * and release the slot used for tuple routing, if set.
2775  */
2776  if (node->mt_partition_tuple_routing)
2777  {
2779 
2780  if (node->mt_root_tuple_slot)
2782  }
2783 
2784  /*
2785  * Free the exprcontext
2786  */
2787  ExecFreeExprContext(&node->ps);
2788 
2789  /*
2790  * clean out the tuple table
2791  */
2792  if (node->ps.ps_ResultTupleSlot)
2794 
2795  /*
2796  * Terminate EPQ execution if active
2797  */
2798  EvalPlanQualEnd(&node->mt_epqstate);
2799 
2800  /*
2801  * shut down subplans
2802  */
2803  for (i = 0; i < node->mt_nplans; i++)
2804  ExecEndNode(node->mt_plans[i]);
2805 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
struct PartitionTupleRouting * mt_partition_tuple_routing
Definition: execnodes.h:1186
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:537
ResultRelInfo * resultRelInfo
Definition: execnodes.h:1172
EState * state
Definition: execnodes.h:945
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:614
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:2931
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:981
PlanState ps
Definition: execnodes.h:1163
void ExecCleanupTupleRouting(ModifyTableState *mtstate, PartitionTupleRouting *proute)
bool ri_usesFdwDirectModify
Definition: execnodes.h:446
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
EPQState mt_epqstate
Definition: execnodes.h:1176
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:440
EndForeignModify_function EndForeignModify
Definition: fdwapi.h:214
PlanState ** mt_plans
Definition: execnodes.h:1167
TupleTableSlot * mt_root_tuple_slot
Definition: execnodes.h:1183
int i

◆ ExecInitModifyTable()

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

Definition at line 2289 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(), 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_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, 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().

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

◆ ExecReScanModifyTable()

void ExecReScanModifyTable ( ModifyTableState node)

Definition at line 2808 of file nodeModifyTable.c.

References elog, and ERROR.

Referenced by ExecReScan().

2809 {
2810  /*
2811  * Currently, we don't need to support rescan on ModifyTable nodes. The
2812  * semantics of that would be a bit debatable anyway.
2813  */
2814  elog(ERROR, "ExecReScanModifyTable is not implemented");
2815 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228