PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

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

Function Documentation

void ExecEndModifyTable ( ModifyTableState node)

Definition at line 2344 of file nodeModifyTable.c.

References FdwRoutine::EndForeignModify, EvalPlanQualEnd(), ExecClearTuple(), ExecCloseIndices(), ExecDropSingleTupleTableSlot(), ExecEndNode(), ExecFreeExprContext(), heap_close, i, ModifyTableState::mt_epqstate, ModifyTableState::mt_nplans, ModifyTableState::mt_num_dispatch, ModifyTableState::mt_num_partitions, ModifyTableState::mt_partition_dispatch_info, ModifyTableState::mt_partition_tuple_slot, ModifyTableState::mt_partitions, ModifyTableState::mt_plans, NoLock, ModifyTableState::ps, PlanState::ps_ResultTupleSlot, PartitionDispatchData::reldesc, ModifyTableState::resultRelInfo, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_usesFdwDirectModify, PlanState::state, and PartitionDispatchData::tupslot.

Referenced by ExecEndNode().

2345 {
2346  int i;
2347 
2348  /*
2349  * Allow any FDWs to shut down
2350  */
2351  for (i = 0; i < node->mt_nplans; i++)
2352  {
2353  ResultRelInfo *resultRelInfo = node->resultRelInfo + i;
2354 
2355  if (!resultRelInfo->ri_usesFdwDirectModify &&
2356  resultRelInfo->ri_FdwRoutine != NULL &&
2357  resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
2358  resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
2359  resultRelInfo);
2360  }
2361 
2362  /*
2363  * Close all the partitioned tables, leaf partitions, and their indices
2364  *
2365  * Remember node->mt_partition_dispatch_info[0] corresponds to the root
2366  * partitioned table, which we must not try to close, because it is the
2367  * main target table of the query that will be closed by ExecEndPlan().
2368  * Also, tupslot is NULL for the root partitioned table.
2369  */
2370  for (i = 1; i < node->mt_num_dispatch; i++)
2371  {
2373 
2374  heap_close(pd->reldesc, NoLock);
2376  }
2377  for (i = 0; i < node->mt_num_partitions; i++)
2378  {
2379  ResultRelInfo *resultRelInfo = node->mt_partitions + i;
2380 
2381  ExecCloseIndices(resultRelInfo);
2382  heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2383  }
2384 
2385  /* Release the standalone partition tuple descriptor, if any */
2386  if (node->mt_partition_tuple_slot)
2388 
2389  /*
2390  * Free the exprcontext
2391  */
2392  ExecFreeExprContext(&node->ps);
2393 
2394  /*
2395  * clean out the tuple table
2396  */
2398 
2399  /*
2400  * Terminate EPQ execution if active
2401  */
2402  EvalPlanQualEnd(&node->mt_epqstate);
2403 
2404  /*
2405  * shut down subplans
2406  */
2407  for (i = 0; i < node->mt_nplans; i++)
2408  ExecEndNode(node->mt_plans[i]);
2409 }
Relation ri_RelationDesc
Definition: execnodes.h:354
ResultRelInfo * mt_partitions
Definition: execnodes.h:981
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
ResultRelInfo * resultRelInfo
Definition: execnodes.h:964
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
#define heap_close(r, l)
Definition: heapam.h:97
EState * state
Definition: execnodes.h:849
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:521
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:3206
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
PlanState ps
Definition: execnodes.h:957
bool ri_usesFdwDirectModify
Definition: execnodes.h:384
#define NoLock
Definition: lockdefs.h:34
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:216
EPQState mt_epqstate
Definition: execnodes.h:968
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:378
EndForeignModify_function EndForeignModify
Definition: fdwapi.h:203
PlanState ** mt_plans
Definition: execnodes.h:961
TupleTableSlot * tupslot
Definition: partition.h:66
TupleTableSlot * mt_partition_tuple_slot
Definition: execnodes.h:984
int i
struct PartitionDispatchData ** mt_partition_dispatch_info
Definition: execnodes.h:976
void ExecCloseIndices(ResultRelInfo *resultRelInfo)
Definition: execIndexing.c:224
ModifyTableState* ExecInitModifyTable ( ModifyTable node,
EState estate,
int  eflags 
)

Definition at line 1812 of file nodeModifyTable.c.

References ModifyTable::arbiterIndexes, Assert, AttributeNumberIsValid, FdwRoutine::BeginForeignModify, bms_is_member(), ModifyTable::canSetTag, ModifyTableState::canSetTag, castNode, CheckValidResultRel(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, elog, ModifyTable::epqParam, ERROR, EState::es_auxmodifytables, EState::es_range_table, EState::es_result_relation_info, EState::es_result_relations, EState::es_root_result_relations, EState::es_trig_tuple_slot, EvalPlanQualInit(), EvalPlanQualSetPlan(), ModifyTable::exclRelTlist, EXEC_FLAG_BACKWARD, EXEC_FLAG_EXPLAIN_ONLY, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignResultType(), ExecBuildAuxRowMark(), ExecBuildProjectionInfo(), ExecCheckPlanOutput(), ExecFindJunkAttribute(), ExecFindRowMark(), ExecInitExtraTupleSlot(), ExecInitJunkFilter(), ExecInitNode(), ExecInitQual(), ExecInitResultTupleSlot(), ExecModifyTable(), ExecOpenIndices(), PlanState::ExecProcNode, ExecSetSlotDescriptor(), ExecSetupPartitionTupleRouting(), ExecSetupTransitionCaptureState(), ExecTypeFromTL(), ModifyTable::fdwDirectModifyPlans, ModifyTable::fdwPrivLists, ModifyTableState::fireBSTriggers, getrelid, heap_close, heap_open(), i, PlanRowMark::isParent, JunkFilter::jf_junkAttNo, lappend(), lcons(), lfirst, lfirst_node, linitial, linitial_int, list_length(), list_nth(), makeNode, map_partition_varattnos(), ModifyTableState::mt_arbiterindexes, ModifyTableState::mt_arowmarks, ModifyTableState::mt_conflproj, ModifyTableState::mt_done, ModifyTableState::mt_epqstate, ModifyTableState::mt_excludedtlist, ModifyTableState::mt_existing, ModifyTableState::mt_nplans, ModifyTableState::mt_num_dispatch, ModifyTableState::mt_num_partitions, ModifyTableState::mt_onconflict, ModifyTableState::mt_partition_dispatch_info, ModifyTableState::mt_partition_tupconv_maps, ModifyTableState::mt_partition_tuple_slot, ModifyTableState::mt_partitions, ModifyTableState::mt_plans, ModifyTableState::mt_whichplan, NIL, NoLock, ModifyTable::nominalRelation, ONCONFLICT_NONE, ONCONFLICT_UPDATE, ModifyTable::onConflictAction, ModifyTable::onConflictSet, ModifyTable::onConflictWhere, ModifyTable::operation, ModifyTableState::operation, palloc0(), ModifyTable::partitioned_rels, PlanState::plan, ModifyTable::plans, ModifyTableState::ps, PlanState::ps_ExprContext, PlanState::ps_ResultTupleSlot, WithCheckOption::qual, RelationData::rd_att, RelationData::rd_rel, RELKIND_FOREIGN_TABLE, RELKIND_MATVIEW, RELKIND_PARTITIONED_TABLE, RELKIND_RELATION, TargetEntry::resjunk, ModifyTable::resultRelIndex, ModifyTableState::resultRelInfo, ModifyTable::returningLists, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_junkFilter, ResultRelInfo::ri_onConflictSetProj, ResultRelInfo::ri_onConflictSetWhere, ResultRelInfo::ri_projectReturning, ResultRelInfo::ri_RelationDesc, ResultRelInfo::ri_usesFdwDirectModify, ResultRelInfo::ri_WithCheckOptionExprs, ResultRelInfo::ri_WithCheckOptions, ModifyTable::rootResultRelIndex, ModifyTableState::rootResultRelInfo, ModifyTable::rowMarks, PlanRowMark::rti, PlanState::state, Plan::targetlist, tupleDesc::tdhasoid, and ModifyTable::withCheckOptionLists.

Referenced by ExecInitNode().

1813 {
1814  ModifyTableState *mtstate;
1815  CmdType operation = node->operation;
1816  int nplans = list_length(node->plans);
1817  ResultRelInfo *saved_resultRelInfo;
1818  ResultRelInfo *resultRelInfo;
1819  TupleDesc tupDesc;
1820  Plan *subplan;
1821  ListCell *l;
1822  int i;
1823  Relation rel;
1824 
1825  /* check for unsupported flags */
1826  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1827 
1828  /*
1829  * create state structure
1830  */
1831  mtstate = makeNode(ModifyTableState);
1832  mtstate->ps.plan = (Plan *) node;
1833  mtstate->ps.state = estate;
1834  mtstate->ps.ExecProcNode = ExecModifyTable;
1835 
1836  mtstate->operation = operation;
1837  mtstate->canSetTag = node->canSetTag;
1838  mtstate->mt_done = false;
1839 
1840  mtstate->mt_plans = (PlanState **) palloc0(sizeof(PlanState *) * nplans);
1841  mtstate->resultRelInfo = estate->es_result_relations + node->resultRelIndex;
1842 
1843  /* If modifying a partitioned table, initialize the root table info */
1844  if (node->rootResultRelIndex >= 0)
1845  mtstate->rootResultRelInfo = estate->es_root_result_relations +
1846  node->rootResultRelIndex;
1847 
1848  mtstate->mt_arowmarks = (List **) palloc0(sizeof(List *) * nplans);
1849  mtstate->mt_nplans = nplans;
1850  mtstate->mt_onconflict = node->onConflictAction;
1851  mtstate->mt_arbiterindexes = node->arbiterIndexes;
1852 
1853  /* set up epqstate with dummy subplan data for the moment */
1854  EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL, node->epqParam);
1855  mtstate->fireBSTriggers = true;
1856 
1857  /*
1858  * call ExecInitNode on each of the plans to be executed and save the
1859  * results into the array "mt_plans". This is also a convenient place to
1860  * verify that the proposed target relations are valid and open their
1861  * indexes for insertion of new index entries. Note we *must* set
1862  * estate->es_result_relation_info correctly while we initialize each
1863  * sub-plan; ExecContextForcesOids depends on that!
1864  */
1865  saved_resultRelInfo = estate->es_result_relation_info;
1866 
1867  resultRelInfo = mtstate->resultRelInfo;
1868  i = 0;
1869  foreach(l, node->plans)
1870  {
1871  subplan = (Plan *) lfirst(l);
1872 
1873  /* Initialize the usesFdwDirectModify flag */
1874  resultRelInfo->ri_usesFdwDirectModify = bms_is_member(i,
1875  node->fdwDirectModifyPlans);
1876 
1877  /*
1878  * Verify result relation is a valid target for the current operation
1879  */
1880  CheckValidResultRel(resultRelInfo, operation);
1881 
1882  /*
1883  * If there are indices on the result relation, open them and save
1884  * descriptors in the result relation info, so that we can add new
1885  * index entries for the tuples we add/update. We need not do this
1886  * for a DELETE, however, since deletion doesn't affect indexes. Also,
1887  * inside an EvalPlanQual operation, the indexes might be open
1888  * already, since we share the resultrel state with the original
1889  * query.
1890  */
1891  if (resultRelInfo->ri_RelationDesc->rd_rel->relhasindex &&
1892  operation != CMD_DELETE &&
1893  resultRelInfo->ri_IndexRelationDescs == NULL)
1894  ExecOpenIndices(resultRelInfo, mtstate->mt_onconflict != ONCONFLICT_NONE);
1895 
1896  /* Now init the plan for this result rel */
1897  estate->es_result_relation_info = resultRelInfo;
1898  mtstate->mt_plans[i] = ExecInitNode(subplan, estate, eflags);
1899 
1900  /* Also let FDWs init themselves for foreign-table result rels */
1901  if (!resultRelInfo->ri_usesFdwDirectModify &&
1902  resultRelInfo->ri_FdwRoutine != NULL &&
1903  resultRelInfo->ri_FdwRoutine->BeginForeignModify != NULL)
1904  {
1905  List *fdw_private = (List *) list_nth(node->fdwPrivLists, i);
1906 
1907  resultRelInfo->ri_FdwRoutine->BeginForeignModify(mtstate,
1908  resultRelInfo,
1909  fdw_private,
1910  i,
1911  eflags);
1912  }
1913 
1914  resultRelInfo++;
1915  i++;
1916  }
1917 
1918  estate->es_result_relation_info = saved_resultRelInfo;
1919 
1920  /* The root table RT index is at the head of the partitioned_rels list */
1921  if (node->partitioned_rels)
1922  {
1923  Index root_rti;
1924  Oid root_oid;
1925 
1926  root_rti = linitial_int(node->partitioned_rels);
1927  root_oid = getrelid(root_rti, estate->es_range_table);
1928  rel = heap_open(root_oid, NoLock); /* locked by InitPlan */
1929  }
1930  else
1931  rel = mtstate->resultRelInfo->ri_RelationDesc;
1932 
1933  /* Build state for INSERT tuple routing */
1934  if (operation == CMD_INSERT &&
1935  rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1936  {
1937  PartitionDispatch *partition_dispatch_info;
1938  ResultRelInfo *partitions;
1939  TupleConversionMap **partition_tupconv_maps;
1940  TupleTableSlot *partition_tuple_slot;
1941  int num_parted,
1942  num_partitions;
1943 
1945  node->nominalRelation,
1946  estate,
1947  &partition_dispatch_info,
1948  &partitions,
1949  &partition_tupconv_maps,
1950  &partition_tuple_slot,
1951  &num_parted, &num_partitions);
1952  mtstate->mt_partition_dispatch_info = partition_dispatch_info;
1953  mtstate->mt_num_dispatch = num_parted;
1954  mtstate->mt_partitions = partitions;
1955  mtstate->mt_num_partitions = num_partitions;
1956  mtstate->mt_partition_tupconv_maps = partition_tupconv_maps;
1957  mtstate->mt_partition_tuple_slot = partition_tuple_slot;
1958  }
1959 
1960  /*
1961  * Build state for collecting transition tuples. This requires having a
1962  * valid trigger query context, so skip it in explain-only mode.
1963  */
1964  if (!(eflags & EXEC_FLAG_EXPLAIN_ONLY))
1965  ExecSetupTransitionCaptureState(mtstate, estate);
1966 
1967  /*
1968  * Initialize any WITH CHECK OPTION constraints if needed.
1969  */
1970  resultRelInfo = mtstate->resultRelInfo;
1971  i = 0;
1972  foreach(l, node->withCheckOptionLists)
1973  {
1974  List *wcoList = (List *) lfirst(l);
1975  List *wcoExprs = NIL;
1976  ListCell *ll;
1977 
1978  foreach(ll, wcoList)
1979  {
1980  WithCheckOption *wco = (WithCheckOption *) lfirst(ll);
1981  ExprState *wcoExpr = ExecInitQual((List *) wco->qual,
1982  mtstate->mt_plans[i]);
1983 
1984  wcoExprs = lappend(wcoExprs, wcoExpr);
1985  }
1986 
1987  resultRelInfo->ri_WithCheckOptions = wcoList;
1988  resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
1989  resultRelInfo++;
1990  i++;
1991  }
1992 
1993  /*
1994  * Build WITH CHECK OPTION constraints for each leaf partition rel. Note
1995  * that we didn't build the withCheckOptionList for each partition within
1996  * the planner, but simple translation of the varattnos for each partition
1997  * will suffice. This only occurs for the INSERT case; UPDATE/DELETE
1998  * cases are handled above.
1999  */
2000  if (node->withCheckOptionLists != NIL && mtstate->mt_num_partitions > 0)
2001  {
2002  List *wcoList;
2003  PlanState *plan;
2004 
2005  /*
2006  * In case of INSERT on partitioned tables, there is only one plan.
2007  * Likewise, there is only one WITH CHECK OPTIONS list, not one per
2008  * partition. We make a copy of the WCO qual for each partition; note
2009  * that, if there are SubPlans in there, they all end up attached to
2010  * the one parent Plan node.
2011  */
2012  Assert(operation == CMD_INSERT &&
2013  list_length(node->withCheckOptionLists) == 1 &&
2014  mtstate->mt_nplans == 1);
2015  wcoList = linitial(node->withCheckOptionLists);
2016  plan = mtstate->mt_plans[0];
2017  resultRelInfo = mtstate->mt_partitions;
2018  for (i = 0; i < mtstate->mt_num_partitions; i++)
2019  {
2020  Relation partrel = resultRelInfo->ri_RelationDesc;
2021  List *mapped_wcoList;
2022  List *wcoExprs = NIL;
2023  ListCell *ll;
2024 
2025  /* varno = node->nominalRelation */
2026  mapped_wcoList = map_partition_varattnos(wcoList,
2027  node->nominalRelation,
2028  partrel, rel, NULL);
2029  foreach(ll, mapped_wcoList)
2030  {
2032  ExprState *wcoExpr = ExecInitQual(castNode(List, wco->qual),
2033  plan);
2034 
2035  wcoExprs = lappend(wcoExprs, wcoExpr);
2036  }
2037 
2038  resultRelInfo->ri_WithCheckOptions = mapped_wcoList;
2039  resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
2040  resultRelInfo++;
2041  }
2042  }
2043 
2044  /*
2045  * Initialize RETURNING projections if needed.
2046  */
2047  if (node->returningLists)
2048  {
2049  TupleTableSlot *slot;
2050  ExprContext *econtext;
2051  List *returningList;
2052 
2053  /*
2054  * Initialize result tuple slot and assign its rowtype using the first
2055  * RETURNING list. We assume the rest will look the same.
2056  */
2057  tupDesc = ExecTypeFromTL((List *) linitial(node->returningLists),
2058  false);
2059 
2060  /* Set up a slot for the output of the RETURNING projection(s) */
2061  ExecInitResultTupleSlot(estate, &mtstate->ps);
2062  ExecAssignResultType(&mtstate->ps, tupDesc);
2063  slot = mtstate->ps.ps_ResultTupleSlot;
2064 
2065  /* Need an econtext too */
2066  if (mtstate->ps.ps_ExprContext == NULL)
2067  ExecAssignExprContext(estate, &mtstate->ps);
2068  econtext = mtstate->ps.ps_ExprContext;
2069 
2070  /*
2071  * Build a projection for each result rel.
2072  */
2073  resultRelInfo = mtstate->resultRelInfo;
2074  foreach(l, node->returningLists)
2075  {
2076  List *rlist = (List *) lfirst(l);
2077 
2078  resultRelInfo->ri_projectReturning =
2079  ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
2080  resultRelInfo->ri_RelationDesc->rd_att);
2081  resultRelInfo++;
2082  }
2083 
2084  /*
2085  * Build a projection for each leaf partition rel. Note that we
2086  * didn't build the returningList for each partition within the
2087  * planner, but simple translation of the varattnos for each partition
2088  * will suffice. This only occurs for the INSERT case; UPDATE/DELETE
2089  * are handled above.
2090  */
2091  resultRelInfo = mtstate->mt_partitions;
2092  returningList = linitial(node->returningLists);
2093  for (i = 0; i < mtstate->mt_num_partitions; i++)
2094  {
2095  Relation partrel = resultRelInfo->ri_RelationDesc;
2096  List *rlist;
2097 
2098  /* varno = node->nominalRelation */
2099  rlist = map_partition_varattnos(returningList,
2100  node->nominalRelation,
2101  partrel, rel, NULL);
2102  resultRelInfo->ri_projectReturning =
2103  ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
2104  resultRelInfo->ri_RelationDesc->rd_att);
2105  resultRelInfo++;
2106  }
2107  }
2108  else
2109  {
2110  /*
2111  * We still must construct a dummy result tuple type, because InitPlan
2112  * expects one (maybe should change that?).
2113  */
2114  tupDesc = ExecTypeFromTL(NIL, false);
2115  ExecInitResultTupleSlot(estate, &mtstate->ps);
2116  ExecAssignResultType(&mtstate->ps, tupDesc);
2117 
2118  mtstate->ps.ps_ExprContext = NULL;
2119  }
2120 
2121  /* Close the root partitioned rel if we opened it above. */
2122  if (rel != mtstate->resultRelInfo->ri_RelationDesc)
2123  heap_close(rel, NoLock);
2124 
2125  /*
2126  * If needed, Initialize target list, projection and qual for ON CONFLICT
2127  * DO UPDATE.
2128  */
2129  resultRelInfo = mtstate->resultRelInfo;
2130  if (node->onConflictAction == ONCONFLICT_UPDATE)
2131  {
2132  ExprContext *econtext;
2133  TupleDesc tupDesc;
2134 
2135  /* insert may only have one plan, inheritance is not expanded */
2136  Assert(nplans == 1);
2137 
2138  /* already exists if created by RETURNING processing above */
2139  if (mtstate->ps.ps_ExprContext == NULL)
2140  ExecAssignExprContext(estate, &mtstate->ps);
2141 
2142  econtext = mtstate->ps.ps_ExprContext;
2143 
2144  /* initialize slot for the existing tuple */
2145  mtstate->mt_existing = ExecInitExtraTupleSlot(mtstate->ps.state);
2147  resultRelInfo->ri_RelationDesc->rd_att);
2148 
2149  /* carried forward solely for the benefit of explain */
2150  mtstate->mt_excludedtlist = node->exclRelTlist;
2151 
2152  /* create target slot for UPDATE SET projection */
2153  tupDesc = ExecTypeFromTL((List *) node->onConflictSet,
2154  resultRelInfo->ri_RelationDesc->rd_rel->relhasoids);
2155  mtstate->mt_conflproj = ExecInitExtraTupleSlot(mtstate->ps.state);
2156  ExecSetSlotDescriptor(mtstate->mt_conflproj, tupDesc);
2157 
2158  /* build UPDATE SET projection state */
2159  resultRelInfo->ri_onConflictSetProj =
2160  ExecBuildProjectionInfo(node->onConflictSet, econtext,
2161  mtstate->mt_conflproj, &mtstate->ps,
2162  resultRelInfo->ri_RelationDesc->rd_att);
2163 
2164  /* build DO UPDATE WHERE clause expression */
2165  if (node->onConflictWhere)
2166  {
2167  ExprState *qualexpr;
2168 
2169  qualexpr = ExecInitQual((List *) node->onConflictWhere,
2170  &mtstate->ps);
2171 
2172  resultRelInfo->ri_onConflictSetWhere = qualexpr;
2173  }
2174  }
2175 
2176  /*
2177  * If we have any secondary relations in an UPDATE or DELETE, they need to
2178  * be treated like non-locked relations in SELECT FOR UPDATE, ie, the
2179  * EvalPlanQual mechanism needs to be told about them. Locate the
2180  * relevant ExecRowMarks.
2181  */
2182  foreach(l, node->rowMarks)
2183  {
2185  ExecRowMark *erm;
2186 
2187  /* ignore "parent" rowmarks; they are irrelevant at runtime */
2188  if (rc->isParent)
2189  continue;
2190 
2191  /* find ExecRowMark (same for all subplans) */
2192  erm = ExecFindRowMark(estate, rc->rti, false);
2193 
2194  /* build ExecAuxRowMark for each subplan */
2195  for (i = 0; i < nplans; i++)
2196  {
2197  ExecAuxRowMark *aerm;
2198 
2199  subplan = mtstate->mt_plans[i]->plan;
2200  aerm = ExecBuildAuxRowMark(erm, subplan->targetlist);
2201  mtstate->mt_arowmarks[i] = lappend(mtstate->mt_arowmarks[i], aerm);
2202  }
2203  }
2204 
2205  /* select first subplan */
2206  mtstate->mt_whichplan = 0;
2207  subplan = (Plan *) linitial(node->plans);
2208  EvalPlanQualSetPlan(&mtstate->mt_epqstate, subplan,
2209  mtstate->mt_arowmarks[0]);
2210 
2211  /*
2212  * Initialize the junk filter(s) if needed. INSERT queries need a filter
2213  * if there are any junk attrs in the tlist. UPDATE and DELETE always
2214  * need a filter, since there's always at least one junk attribute present
2215  * --- no need to look first. Typically, this will be a 'ctid' or
2216  * 'wholerow' attribute, but in the case of a foreign data wrapper it
2217  * might be a set of junk attributes sufficient to identify the remote
2218  * row.
2219  *
2220  * If there are multiple result relations, each one needs its own junk
2221  * filter. Note multiple rels are only possible for UPDATE/DELETE, so we
2222  * can't be fooled by some needing a filter and some not.
2223  *
2224  * This section of code is also a convenient place to verify that the
2225  * output of an INSERT or UPDATE matches the target table(s).
2226  */
2227  {
2228  bool junk_filter_needed = false;
2229 
2230  switch (operation)
2231  {
2232  case CMD_INSERT:
2233  foreach(l, subplan->targetlist)
2234  {
2235  TargetEntry *tle = (TargetEntry *) lfirst(l);
2236 
2237  if (tle->resjunk)
2238  {
2239  junk_filter_needed = true;
2240  break;
2241  }
2242  }
2243  break;
2244  case CMD_UPDATE:
2245  case CMD_DELETE:
2246  junk_filter_needed = true;
2247  break;
2248  default:
2249  elog(ERROR, "unknown operation");
2250  break;
2251  }
2252 
2253  if (junk_filter_needed)
2254  {
2255  resultRelInfo = mtstate->resultRelInfo;
2256  for (i = 0; i < nplans; i++)
2257  {
2258  JunkFilter *j;
2259 
2260  subplan = mtstate->mt_plans[i]->plan;
2261  if (operation == CMD_INSERT || operation == CMD_UPDATE)
2262  ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
2263  subplan->targetlist);
2264 
2265  j = ExecInitJunkFilter(subplan->targetlist,
2266  resultRelInfo->ri_RelationDesc->rd_att->tdhasoid,
2267  ExecInitExtraTupleSlot(estate));
2268 
2269  if (operation == CMD_UPDATE || operation == CMD_DELETE)
2270  {
2271  /* For UPDATE/DELETE, find the appropriate junk attr now */
2272  char relkind;
2273 
2274  relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
2275  if (relkind == RELKIND_RELATION ||
2276  relkind == RELKIND_MATVIEW ||
2277  relkind == RELKIND_PARTITIONED_TABLE)
2278  {
2279  j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
2281  elog(ERROR, "could not find junk ctid column");
2282  }
2283  else if (relkind == RELKIND_FOREIGN_TABLE)
2284  {
2285  /*
2286  * When there is a row-level trigger, there should be
2287  * a wholerow attribute.
2288  */
2289  j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
2290  }
2291  else
2292  {
2293  j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
2295  elog(ERROR, "could not find junk wholerow column");
2296  }
2297  }
2298 
2299  resultRelInfo->ri_junkFilter = j;
2300  resultRelInfo++;
2301  }
2302  }
2303  else
2304  {
2305  if (operation == CMD_INSERT)
2307  subplan->targetlist);
2308  }
2309  }
2310 
2311  /*
2312  * Set up a tuple table slot for use for trigger output tuples. In a plan
2313  * containing multiple ModifyTable nodes, all can share one such slot, so
2314  * we keep it in the estate.
2315  */
2316  if (estate->es_trig_tuple_slot == NULL)
2317  estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
2318 
2319  /*
2320  * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it
2321  * to estate->es_auxmodifytables so that it will be run to completion by
2322  * ExecPostprocessPlan. (It'd actually work fine to add the primary
2323  * ModifyTable node too, but there's no need.) Note the use of lcons not
2324  * lappend: we need later-initialized ModifyTable nodes to be shut down
2325  * before earlier ones. This ensures that we don't throw away RETURNING
2326  * rows that need to be seen by a later CTE subplan.
2327  */
2328  if (!mtstate->canSetTag)
2329  estate->es_auxmodifytables = lcons(mtstate,
2330  estate->es_auxmodifytables);
2331 
2332  return mtstate;
2333 }
AttrNumber jf_junkAttNo
Definition: execnodes.h:336
#define NIL
Definition: pg_list.h:69
JunkFilter * ri_junkFilter
Definition: execnodes.h:396
List * arbiterIndexes
Definition: plannodes.h:233
Relation ri_RelationDesc
Definition: execnodes.h:354
Bitmapset * fdwDirectModifyPlans
Definition: plannodes.h:229
ExprState * ri_onConflictSetWhere
Definition: execnodes.h:405
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
Index nominalRelation
Definition: plannodes.h:219
bool tdhasoid
Definition: tupdesc.h:76
ProjectionInfo * ri_onConflictSetProj
Definition: execnodes.h:402
ResultRelInfo * mt_partitions
Definition: execnodes.h:981
List * withCheckOptionLists
Definition: plannodes.h:226
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
int resultRelIndex
Definition: plannodes.h:223
ResultRelInfo * resultRelInfo
Definition: execnodes.h:964
AttrNumber ExecFindJunkAttribute(JunkFilter *junkfilter, const char *attrName)
Definition: execJunk.c:209
ExprContext * ps_ExprContext
Definition: execnodes.h:881
TupleTableSlot * mt_conflproj
Definition: execnodes.h:975
#define RELKIND_MATVIEW
Definition: pg_class.h:165
bool canSetTag
Definition: plannodes.h:218
CmdType operation
Definition: execnodes.h:958
ResultRelInfo * rootResultRelInfo
Definition: execnodes.h:965
void EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
Definition: execMain.c:2814
#define heap_close(r, l)
Definition: heapam.h:97
EState * state
Definition: execnodes.h:849
List * es_range_table
Definition: execnodes.h:431
Form_pg_class rd_rel
Definition: rel.h:114
unsigned int Oid
Definition: postgres_ext.h:31
List * plans
Definition: plannodes.h:225
void ExecAssignResultType(PlanState *planstate, TupleDesc tupDesc)
Definition: execUtils.c:433
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
List * map_partition_varattnos(List *expr, int target_varno, Relation partrel, Relation parent, bool *found_whole_row)
Definition: partition.c:1121
List * onConflictSet
Definition: plannodes.h:234
int rootResultRelIndex
Definition: plannodes.h:224
void ExecOpenIndices(ResultRelInfo *resultRelInfo, bool speculative)
Definition: execIndexing.c:149
TupleTableSlot * mt_existing
Definition: execnodes.h:973
List * ri_WithCheckOptionExprs
Definition: execnodes.h:390
#define linitial_int(l)
Definition: pg_list.h:112
OnConflictAction mt_onconflict
Definition: execnodes.h:970
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
List * rowMarks
Definition: plannodes.h:230
bool resjunk
Definition: primnodes.h:1375
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
PlanState ps
Definition: execnodes.h:957
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
bool ri_usesFdwDirectModify
Definition: execnodes.h:384
static void ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
void CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
Definition: execMain.c:1100
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define lfirst_node(type, lc)
Definition: pg_list.h:109
void * list_nth(const List *list, int n)
Definition: list.c:410
#define NoLock
Definition: lockdefs.h:34
ResultRelInfo * es_result_relations
Definition: execnodes.h:441
JunkFilter * ExecInitJunkFilter(List *targetList, bool hasoid, TupleTableSlot *slot)
Definition: execJunk.c:61
List * fdwPrivLists
Definition: plannodes.h:228
EPQState mt_epqstate
Definition: execnodes.h:968
ProjectionInfo * ri_projectReturning
Definition: execnodes.h:399
struct FdwRoutine * ri_FdwRoutine
Definition: execnodes.h:378
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
List * partitioned_rels
Definition: plannodes.h:221
TupleTableSlot * es_trig_tuple_slot
Definition: execnodes.h:460
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
List * lappend(List *list, void *datum)
Definition: list.c:128
PlanState ** mt_plans
Definition: execnodes.h:961
#define AttributeNumberIsValid(attributeNumber)
Definition: attnum.h:34
static TupleTableSlot * ExecModifyTable(PlanState *pstate)
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
void * palloc0(Size size)
Definition: mcxt.c:877
List * es_auxmodifytables
Definition: execnodes.h:488
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
List * ri_WithCheckOptions
Definition: execnodes.h:387
TupleTableSlot * mt_partition_tuple_slot
Definition: execnodes.h:984
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
unsigned int Index
Definition: c.h:359
TupleDesc rd_att
Definition: rel.h:115
void EvalPlanQualInit(EPQState *epqstate, EState *estate, Plan *subplan, List *auxrowmarks, int epqParam)
Definition: execMain.c:2795
Plan * plan
Definition: execnodes.h:847
List * lcons(void *datum, List *list)
Definition: list.c:259
#define makeNode(_type_)
Definition: nodes.h:557
static void ExecCheckPlanOutput(Relation resultRel, List *targetList)
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
#define EXEC_FLAG_MARK
Definition: executor.h:61
OnConflictAction onConflictAction
Definition: plannodes.h:232
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:423
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:144
List * mt_arbiterindexes
Definition: execnodes.h:971
List * mt_excludedtlist
Definition: execnodes.h:974
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:301
#define getrelid(rangeindex, rangetable)
Definition: parsetree.h:41
CmdType operation
Definition: plannodes.h:217
ResultRelInfo * es_root_result_relations
Definition: execnodes.h:452
int i
struct PartitionDispatchData ** mt_partition_dispatch_info
Definition: execnodes.h:976
List * returningLists
Definition: plannodes.h:227
bool isParent
Definition: plannodes.h:1020
TupleConversionMap ** mt_partition_tupconv_maps
Definition: execnodes.h:982
void ExecSetupPartitionTupleRouting(Relation rel, Index resultRTindex, EState *estate, PartitionDispatch **pd, ResultRelInfo **partitions, TupleConversionMap ***tup_conv_maps, TupleTableSlot **partition_tuple_slot, int *num_parted, int *num_partitions)
Definition: execMain.c:3268
#define elog
Definition: elog.h:219
BeginForeignModify_function BeginForeignModify
Definition: fdwapi.h:199
#define RELKIND_RELATION
Definition: pg_class.h:160
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:420
#define EXEC_FLAG_EXPLAIN_ONLY
Definition: executor.h:58
CmdType
Definition: nodes.h:649
RelationPtr ri_IndexRelationDescs
Definition: execnodes.h:360
ExecAuxRowMark * ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
Definition: execMain.c:2408
List * exclRelTlist
Definition: plannodes.h:237
List ** mt_arowmarks
Definition: execnodes.h:967
int epqParam
Definition: plannodes.h:231
Node * onConflictWhere
Definition: plannodes.h:235
ExecRowMark * ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
Definition: execMain.c:2384
ResultRelInfo * es_result_relation_info
Definition: execnodes.h:443
void ExecReScanModifyTable ( ModifyTableState node)

Definition at line 2412 of file nodeModifyTable.c.

References elog, and ERROR.

Referenced by ExecReScan().

2413 {
2414  /*
2415  * Currently, we don't need to support rescan on ModifyTable nodes. The
2416  * semantics of that would be a bit debatable anyway.
2417  */
2418  elog(ERROR, "ExecReScanModifyTable is not implemented");
2419 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219