mirror of
https://github.com/postgres/postgres.git
synced 2026-10-23 13:29:09 +03:00
Fix FOR PORTION OF for inheritance children
ExecForPortionOfLeftovers() assumed that any result relation with ri_RootResultRelInfo should reinsert temporal leftovers through the root relation. That is correct for partitioned tables, where tuple routing is needed, but it is wrong for plain inheritance. When UPDATE/DELETE FOR PORTION OF is run on an inheritance parent and a child row is split, the leftover rows must be inserted back into the child relation. Reinserting through the parent can lose child-only columns and place the leftover rows in the wrong relation. Fix this by distinguishing partitioned-table routing from plain inheritance. For partitioned tables, keep using the root leftover slot and insert through the root relation. For plain inheritance children, use a leftover slot matching the child relation and insert directly into the child. Also keep translating the application-time column attno for child relations, so multiple-inheritance cases with different attribute numbers are handled correctly. Added an ExecInitForPortionOf function to set up the ForPortionOfState for each child table, which keeps most of these decisions localized instead of spread out through ExecForPortionOfLeftovers. Incidentally clarified a comment about the rangetype stored in ForPortionOfState. Add regression tests for UPDATE and DELETE FOR PORTION OF on inheritance children, including a multiple-inheritance case where the range column has a different attnum in the parent and child. Author: jian he <jian.universality@gmail.com> Co-authored-by: Chao Li <li.evan.chao@gmail.com> Reviewed-by: Paul A. Jungwirth <pj@illuminatedcomputing.com> Discussion: https://www.postgresql.org/message-id/flat/4245F94D-84F1-4E05-BF81-C458A6CF9901%40gmail.com
This commit is contained in:
@@ -199,6 +199,8 @@ static TupleTableSlot *ExecMergeNotMatched(ModifyTableContext *context,
|
||||
static void ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate);
|
||||
static void fireBSTriggers(ModifyTableState *node);
|
||||
static void fireASTriggers(ModifyTableState *node);
|
||||
static void ExecInitForPortionOf(ModifyTableState *mtstate, EState *estate,
|
||||
ResultRelInfo *resultRelInfo);
|
||||
|
||||
|
||||
/*
|
||||
@@ -1410,7 +1412,6 @@ ExecForPortionOfLeftovers(ModifyTableContext *context,
|
||||
ModifyTableState *mtstate = context->mtstate;
|
||||
ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
|
||||
ForPortionOfExpr *forPortionOf = (ForPortionOfExpr *) node->forPortionOf;
|
||||
AttrNumber rangeAttno;
|
||||
Datum oldRange;
|
||||
TypeCacheEntry *typcache;
|
||||
ForPortionOfState *fpoState;
|
||||
@@ -1425,37 +1426,13 @@ ExecForPortionOfLeftovers(ModifyTableContext *context,
|
||||
ReturnSetInfo rsi;
|
||||
bool didInit = false;
|
||||
bool shouldFree = false;
|
||||
ResultRelInfo *rootRelInfo = mtstate->rootResultRelInfo;
|
||||
bool partitionRouting =
|
||||
rootRelInfo &&
|
||||
rootRelInfo->ri_RelationDesc->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
|
||||
|
||||
LOCAL_FCINFO(fcinfo, 2);
|
||||
|
||||
if (!resultRelInfo->ri_forPortionOf)
|
||||
{
|
||||
/*
|
||||
* If we don't have a ForPortionOfState yet, we must be a partition
|
||||
* child being hit for the first time. Make a copy from the root, with
|
||||
* our own TupleTableSlot. We do this lazily so that we don't pay the
|
||||
* price of unused partitions.
|
||||
*/
|
||||
ForPortionOfState *leafState = makeNode(ForPortionOfState);
|
||||
|
||||
if (!mtstate->rootResultRelInfo)
|
||||
elog(ERROR, "no root relation but ri_forPortionOf is uninitialized");
|
||||
|
||||
fpoState = mtstate->rootResultRelInfo->ri_forPortionOf;
|
||||
Assert(fpoState);
|
||||
|
||||
leafState->fp_rangeName = fpoState->fp_rangeName;
|
||||
leafState->fp_rangeType = fpoState->fp_rangeType;
|
||||
leafState->fp_rangeAttno = fpoState->fp_rangeAttno;
|
||||
leafState->fp_targetRange = fpoState->fp_targetRange;
|
||||
leafState->fp_Leftover = fpoState->fp_Leftover;
|
||||
/* Each partition needs a slot matching its tuple descriptor */
|
||||
leafState->fp_Existing =
|
||||
table_slot_create(resultRelInfo->ri_RelationDesc,
|
||||
&mtstate->ps.state->es_tupleTable);
|
||||
|
||||
resultRelInfo->ri_forPortionOf = leafState;
|
||||
}
|
||||
fpoState = resultRelInfo->ri_forPortionOf;
|
||||
oldtupleSlot = fpoState->fp_Existing;
|
||||
leftoverSlot = fpoState->fp_Leftover;
|
||||
@@ -1476,21 +1453,12 @@ ExecForPortionOfLeftovers(ModifyTableContext *context,
|
||||
if (!table_tuple_fetch_row_version(resultRelInfo->ri_RelationDesc, tupleid, SnapshotAny, oldtupleSlot))
|
||||
elog(ERROR, "failed to fetch tuple for FOR PORTION OF");
|
||||
|
||||
/*
|
||||
* Get the old range of the record being updated/deleted. Must read with
|
||||
* the attno of the leaf partition being updated.
|
||||
*/
|
||||
|
||||
rangeAttno = forPortionOf->rangeVar->varattno;
|
||||
if (resultRelInfo->ri_RootResultRelInfo)
|
||||
map = ExecGetChildToRootMap(resultRelInfo);
|
||||
if (map != NULL)
|
||||
rangeAttno = map->attrMap->attnums[rangeAttno - 1];
|
||||
slot_getallattrs(oldtupleSlot);
|
||||
|
||||
if (oldtupleSlot->tts_isnull[rangeAttno - 1])
|
||||
/* Get the old range of the record being updated/deleted. */
|
||||
if (oldtupleSlot->tts_isnull[fpoState->fp_rangeAttno - 1])
|
||||
elog(ERROR, "found a NULL range in a temporal table");
|
||||
oldRange = oldtupleSlot->tts_values[rangeAttno - 1];
|
||||
oldRange = oldtupleSlot->tts_values[fpoState->fp_rangeAttno - 1];
|
||||
|
||||
/*
|
||||
* Get the range's type cache entry. This is worth caching for the whole
|
||||
@@ -1528,12 +1496,19 @@ ExecForPortionOfLeftovers(ModifyTableContext *context,
|
||||
fcinfo->args[1].isnull = false;
|
||||
|
||||
/*
|
||||
* If there are partitions, we must insert into the root table, so we get
|
||||
* tuple routing. We already set up leftoverSlot with the root tuple
|
||||
* descriptor.
|
||||
* For partitioned tables, we must read leftovers with the tuple
|
||||
* descriptor of the child table, but insert into the root table to enable
|
||||
* tuple routing. So leftoverSlot is configured with the root's tuple
|
||||
* descriptor. But for traditional table inheritance, we don't need tuple
|
||||
* routing and just insert directly into the child table to preserve
|
||||
* child-specific columns. In that case, leftoverSlot uses the child's
|
||||
* (resultRelInfo) tuple descriptor.
|
||||
*/
|
||||
if (resultRelInfo->ri_RootResultRelInfo)
|
||||
if (partitionRouting)
|
||||
{
|
||||
map = ExecGetChildToRootMap(resultRelInfo);
|
||||
resultRelInfo = resultRelInfo->ri_RootResultRelInfo;
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert a leftover for each value returned by the without_portion helper
|
||||
@@ -1575,9 +1550,9 @@ ExecForPortionOfLeftovers(ModifyTableContext *context,
|
||||
{
|
||||
/*
|
||||
* Make a copy of the pre-UPDATE row. Then we'll overwrite the
|
||||
* range column below. Convert oldtuple to the base table's format
|
||||
* if necessary. We need to insert temporal leftovers through the
|
||||
* root partition so they get routed correctly.
|
||||
* range column below. Only partitioned targets need conversion to
|
||||
* the root table's format, because they reinsert through the root
|
||||
* relation for tuple routing.
|
||||
*/
|
||||
if (map != NULL)
|
||||
{
|
||||
@@ -1614,8 +1589,8 @@ ExecForPortionOfLeftovers(ModifyTableContext *context,
|
||||
ExecForceStoreHeapTuple(oldtuple, leftoverSlot, false);
|
||||
}
|
||||
|
||||
leftoverSlot->tts_values[forPortionOf->rangeVar->varattno - 1] = leftover;
|
||||
leftoverSlot->tts_isnull[forPortionOf->rangeVar->varattno - 1] = false;
|
||||
leftoverSlot->tts_values[resultRelInfo->ri_forPortionOf->fp_rangeAttno - 1] = leftover;
|
||||
leftoverSlot->tts_isnull[resultRelInfo->ri_forPortionOf->fp_rangeAttno - 1] = false;
|
||||
ExecMaterializeSlot(leftoverSlot);
|
||||
|
||||
/*
|
||||
@@ -4790,6 +4765,16 @@ ExecModifyTable(PlanState *pstate)
|
||||
false, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* If we don't have a ForPortionOfState yet, we must be a partition or
|
||||
* inheritance child being hit for the first time. Make a copy from
|
||||
* the root, with our own TupleTableSlot. We do this lazily so that we
|
||||
* don't pay the price of unused partitions.
|
||||
*/
|
||||
if (((ModifyTable *) context.mtstate->ps.plan)->forPortionOf &&
|
||||
!resultRelInfo->ri_forPortionOf)
|
||||
ExecInitForPortionOf(context.mtstate, estate, resultRelInfo);
|
||||
|
||||
/*
|
||||
* If resultRelInfo->ri_usesFdwDirectModify is true, all we need to do
|
||||
* here is compute the RETURNING expressions.
|
||||
@@ -5873,3 +5858,76 @@ ExecReScanModifyTable(ModifyTableState *node)
|
||||
*/
|
||||
elog(ERROR, "ExecReScanModifyTable is not implemented");
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* ExecInitForPortionOf
|
||||
*
|
||||
* Initializes resultRelInfo->ri_forPortionOf for child tables.
|
||||
*
|
||||
* Partitions share the root leftover slot, since they must insert via
|
||||
* the root relation to get tuple routing. Plain inheritance children
|
||||
* must keep their own leftover slot and insert back into the child, or
|
||||
* else child-only column values and physical placement would be lost.
|
||||
* ----------------------------------------------------------------
|
||||
*/
|
||||
static void
|
||||
ExecInitForPortionOf(ModifyTableState *mtstate, EState *estate,
|
||||
ResultRelInfo *resultRelInfo)
|
||||
{
|
||||
MemoryContext oldcxt;
|
||||
ForPortionOfState *leafState;
|
||||
ResultRelInfo *rootRelInfo = mtstate->rootResultRelInfo;
|
||||
ForPortionOfState *fpoState;
|
||||
TupleConversionMap *map;
|
||||
|
||||
if (!rootRelInfo)
|
||||
elog(ERROR, "no root relation but ri_forPortionOf is uninitialized");
|
||||
|
||||
fpoState = rootRelInfo->ri_forPortionOf;
|
||||
Assert(fpoState);
|
||||
|
||||
/* Things built here have to last for the query duration. */
|
||||
oldcxt = MemoryContextSwitchTo(estate->es_query_cxt);
|
||||
|
||||
leafState = makeNode(ForPortionOfState);
|
||||
|
||||
leafState->fp_rangeName = fpoState->fp_rangeName;
|
||||
leafState->fp_rangeType = fpoState->fp_rangeType;
|
||||
leafState->fp_targetRange = fpoState->fp_targetRange;
|
||||
map = ExecGetChildToRootMap(resultRelInfo);
|
||||
|
||||
/*
|
||||
* fp_rangeAttno must match the tuple layout used for reading the old
|
||||
* range value. The query uses the target relation's attno, so translate
|
||||
* it to the child attno when the child has a different column layout.
|
||||
*/
|
||||
if (map)
|
||||
leafState->fp_rangeAttno = map->attrMap->attnums[fpoState->fp_rangeAttno - 1];
|
||||
else
|
||||
leafState->fp_rangeAttno = fpoState->fp_rangeAttno;
|
||||
|
||||
/*
|
||||
* For partitioned tables we must read the leftovers using the child
|
||||
* table's tuple descriptor, but then insert them into the root table
|
||||
* (using its tuple descriptor) so we get tuple routing.
|
||||
*
|
||||
* For traditional table inheritance, we read and insert directly into
|
||||
* this resultRelInfo; no tuple routing via the parent is required.
|
||||
*/
|
||||
if (rootRelInfo->ri_RelationDesc->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
|
||||
leafState->fp_Leftover = fpoState->fp_Leftover;
|
||||
else
|
||||
leafState->fp_Leftover =
|
||||
ExecInitExtraTupleSlot(mtstate->ps.state,
|
||||
RelationGetDescr(resultRelInfo->ri_RelationDesc),
|
||||
&TTSOpsVirtual);
|
||||
|
||||
/* Each child relation needs a slot matching its tuple descriptor. */
|
||||
leafState->fp_Existing =
|
||||
table_slot_create(resultRelInfo->ri_RelationDesc,
|
||||
&mtstate->ps.state->es_tupleTable);
|
||||
|
||||
resultRelInfo->ri_forPortionOf = leafState;
|
||||
|
||||
MemoryContextSwitchTo(oldcxt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user