1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-03 20:02:46 +03:00

Use slots more widely in tuple mapping code and make naming more consistent.

It's inefficient to use a single slot for mapping between tuple
descriptors for multiple tuples, as previously done when using
ConvertPartitionTupleSlot(), as that means the slot's tuple descriptors
change for every tuple.

Previously we also, via ConvertPartitionTupleSlot(), built new tuples
after the mapping even in cases where we, immediately afterwards,
access individual columns again.

Refactor the code so one slot, on demand, is used for each
partition. That avoids having to change the descriptor (and allows to
use the more efficient "fixed" tuple slots). Then use slot->slot
mapping, to avoid unnecessarily forming a tuple.

As the naming between the tuple and slot mapping functions wasn't
consistent, rename them to execute_attr_map_{tuple,slot}.  It's likely
that we'll also rename convert_tuples_by_* to denote that these
functions "only" build a map, but that's left for later.

Author: Amit Khandekar and Amit Langote, editorialized by me
Reviewed-By: Amit Langote, Amit Khandekar, Andres Freund
Discussion:
    https://postgr.es/m/CAJ3gD9fR0wRNeAE8VqffNTyONS_UfFPRpqxhnD9Q42vZB+Jvpg@mail.gmail.com
    https://postgr.es/m/e4f9d743-cd4b-efb0-7574-da21d86a7f36%40lab.ntt.co.jp
Backpatch: -
This commit is contained in:
Andres Freund
2018-10-02 11:14:26 -07:00
parent 625b38ea0e
commit cc2905e963
11 changed files with 272 additions and 191 deletions

View File

@ -3285,7 +3285,7 @@ ExecEvalConvertRowtype(ExprState *state, ExprEvalStep *op, ExprContext *econtext
if (op->d.convert_rowtype.map != NULL)
{
/* Full conversion with attribute rearrangement needed */
result = do_convert_tuple(&tmptup, op->d.convert_rowtype.map);
result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map);
/* Result already has appropriate composite-datum header fields */
*op->resvalue = HeapTupleGetDatum(result);
}

View File

@ -1955,21 +1955,22 @@ ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
*/
if (resultRelInfo->ri_PartitionRoot)
{
HeapTuple tuple = ExecFetchSlotTuple(slot);
TupleDesc old_tupdesc = RelationGetDescr(rel);
TupleConversionMap *map;
AttrNumber *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
map = convert_tuples_by_name(old_tupdesc, tupdesc,
gettext_noop("could not convert row type"));
map = convert_tuples_by_name_map_if_req(old_tupdesc, tupdesc,
gettext_noop("could not convert row type"));
/*
* Partition-specific slot's tupdesc can't be changed, so allocate a
* new one.
*/
if (map != NULL)
{
tuple = do_convert_tuple(tuple, map);
ExecSetSlotDescriptor(slot, tupdesc);
ExecStoreHeapTuple(tuple, slot, false);
}
slot = execute_attr_map_slot(map, slot,
MakeTupleTableSlot(tupdesc));
}
insertedCols = GetInsertedColumns(resultRelInfo, estate);
@ -2035,20 +2036,22 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
*/
if (resultRelInfo->ri_PartitionRoot)
{
HeapTuple tuple = ExecFetchSlotTuple(slot);
TupleConversionMap *map;
AttrNumber *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
map = convert_tuples_by_name(orig_tupdesc, tupdesc,
gettext_noop("could not convert row type"));
map = convert_tuples_by_name_map_if_req(orig_tupdesc,
tupdesc,
gettext_noop("could not convert row type"));
/*
* Partition-specific slot's tupdesc can't be changed, so
* allocate a new one.
*/
if (map != NULL)
{
tuple = do_convert_tuple(tuple, map);
ExecSetSlotDescriptor(slot, tupdesc);
ExecStoreHeapTuple(tuple, slot, false);
}
slot = execute_attr_map_slot(map, slot,
MakeTupleTableSlot(tupdesc));
}
insertedCols = GetInsertedColumns(resultRelInfo, estate);
@ -2082,21 +2085,23 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
/* See the comment above. */
if (resultRelInfo->ri_PartitionRoot)
{
HeapTuple tuple = ExecFetchSlotTuple(slot);
TupleDesc old_tupdesc = RelationGetDescr(rel);
TupleConversionMap *map;
AttrNumber *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
map = convert_tuples_by_name(old_tupdesc, tupdesc,
gettext_noop("could not convert row type"));
map = convert_tuples_by_name_map_if_req(old_tupdesc,
tupdesc,
gettext_noop("could not convert row type"));
/*
* Partition-specific slot's tupdesc can't be changed, so
* allocate a new one.
*/
if (map != NULL)
{
tuple = do_convert_tuple(tuple, map);
ExecSetSlotDescriptor(slot, tupdesc);
ExecStoreHeapTuple(tuple, slot, false);
}
slot = execute_attr_map_slot(map, slot,
MakeTupleTableSlot(tupdesc));
}
insertedCols = GetInsertedColumns(resultRelInfo, estate);
@ -2188,21 +2193,23 @@ ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
/* See the comment in ExecConstraints(). */
if (resultRelInfo->ri_PartitionRoot)
{
HeapTuple tuple = ExecFetchSlotTuple(slot);
TupleDesc old_tupdesc = RelationGetDescr(rel);
TupleConversionMap *map;
AttrNumber *map;
rel = resultRelInfo->ri_PartitionRoot;
tupdesc = RelationGetDescr(rel);
/* a reverse map */
map = convert_tuples_by_name(old_tupdesc, tupdesc,
gettext_noop("could not convert row type"));
map = convert_tuples_by_name_map_if_req(old_tupdesc,
tupdesc,
gettext_noop("could not convert row type"));
/*
* Partition-specific slot's tupdesc can't be changed,
* so allocate a new one.
*/
if (map != NULL)
{
tuple = do_convert_tuple(tuple, map);
ExecSetSlotDescriptor(slot, tupdesc);
ExecStoreHeapTuple(tuple, slot, false);
}
slot = execute_attr_map_slot(map, slot,
MakeTupleTableSlot(tupdesc));
}
insertedCols = GetInsertedColumns(resultRelInfo, estate);

View File

@ -57,7 +57,7 @@ typedef struct PartitionDispatchData
List *keystate; /* list of ExprState */
PartitionDesc partdesc;
TupleTableSlot *tupslot;
TupleConversionMap *tupmap;
AttrNumber *tupmap;
int *indexes;
} PartitionDispatchData;
@ -144,17 +144,9 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
* We need an additional tuple slot for storing transient tuples that
* are converted to the root table descriptor.
*/
proute->root_tuple_slot = MakeTupleTableSlot(NULL);
proute->root_tuple_slot = MakeTupleTableSlot(RelationGetDescr(rel));
}
/*
* Initialize an empty slot that will be used to manipulate tuples of any
* given partition's rowtype. It is attached to the caller-specified node
* (such as ModifyTableState) and released when the node finishes
* processing.
*/
proute->partition_tuple_slot = MakeTupleTableSlot(NULL);
i = 0;
foreach(cell, leaf_parts)
{
@ -228,7 +220,6 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
TupleTableSlot *myslot = NULL;
MemoryContext oldcxt;
HeapTuple tuple;
/* use per-tuple context here to avoid leaking memory */
oldcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
@ -241,11 +232,10 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
ExecPartitionCheck(resultRelInfo, slot, estate, true);
/* start with the root partitioned table */
tuple = ExecFetchSlotTuple(slot);
dispatch = pd[0];
while (true)
{
TupleConversionMap *map = dispatch->tupmap;
AttrNumber *map = dispatch->tupmap;
int cur_index = -1;
rel = dispatch->reldesc;
@ -256,11 +246,7 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
*/
myslot = dispatch->tupslot;
if (myslot != NULL && map != NULL)
{
tuple = do_convert_tuple(tuple, map);
ExecStoreHeapTuple(tuple, myslot, true);
slot = myslot;
}
slot = execute_attr_map_slot(map, slot, myslot);
/*
* Extract partition key from tuple. Expression evaluation machinery
@ -305,16 +291,6 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
{
/* move down one level */
dispatch = pd[-dispatch->indexes[cur_index]];
/*
* Release the dedicated slot, if it was used. Create a copy of
* the tuple first, for the next iteration.
*/
if (slot == myslot)
{
tuple = ExecCopySlotTuple(myslot);
ExecClearTuple(myslot);
}
}
}
@ -738,6 +714,35 @@ ExecInitRoutingInfo(ModifyTableState *mtstate,
RelationGetDescr(partRelInfo->ri_RelationDesc),
gettext_noop("could not convert row type"));
/*
* If a partition has a different rowtype than the root parent, initialize
* a slot dedicated to storing this partition's tuples. The slot is used
* for various operations that are applied to tuples after routing, such
* as checking constraints.
*/
if (proute->parent_child_tupconv_maps[partidx] != NULL)
{
Relation partrel = partRelInfo->ri_RelationDesc;
/*
* Initialize the array in proute where these slots are stored, if not
* already done.
*/
if (proute->partition_tuple_slots == NULL)
proute->partition_tuple_slots = (TupleTableSlot **)
palloc0(proute->num_partitions *
sizeof(TupleTableSlot *));
/*
* Initialize the slot itself setting its descriptor to this
* partition's TupleDesc; TupleDesc reference will be released at the
* end of the command.
*/
proute->partition_tuple_slots[partidx] =
ExecInitExtraTupleSlot(estate,
RelationGetDescr(partrel));
}
/*
* If the partition is a foreign table, let the FDW init itself for
* routing tuples to the partition.
@ -815,38 +820,6 @@ TupConvMapForLeaf(PartitionTupleRouting *proute,
return *map;
}
/*
* ConvertPartitionTupleSlot -- convenience function for tuple conversion.
* The tuple, if converted, is stored in new_slot, and *p_my_slot is
* updated to point to it. new_slot typically should be one of the
* dedicated partition tuple slots. If map is NULL, *p_my_slot is not changed.
*
* Returns the converted tuple, unless map is NULL, in which case original
* tuple is returned unmodified.
*/
HeapTuple
ConvertPartitionTupleSlot(TupleConversionMap *map,
HeapTuple tuple,
TupleTableSlot *new_slot,
TupleTableSlot **p_my_slot,
bool shouldFree)
{
if (!map)
return tuple;
tuple = do_convert_tuple(tuple, map);
/*
* Change the partition tuple slot descriptor, as per converted tuple.
*/
*p_my_slot = new_slot;
Assert(new_slot != NULL);
ExecSetSlotDescriptor(new_slot, map->outdesc);
ExecStoreHeapTuple(tuple, new_slot, shouldFree);
return tuple;
}
/*
* ExecCleanupTupleRouting -- Clean up objects allocated for partition tuple
* routing.
@ -915,8 +888,6 @@ ExecCleanupTupleRouting(ModifyTableState *mtstate,
/* Release the standalone partition tuple descriptors, if any */
if (proute->root_tuple_slot)
ExecDropSingleTupleTableSlot(proute->root_tuple_slot);
if (proute->partition_tuple_slot)
ExecDropSingleTupleTableSlot(proute->partition_tuple_slot);
}
/*
@ -1004,9 +975,9 @@ get_partition_dispatch_recurse(Relation rel, Relation parent,
* for tuple routing.
*/
pd->tupslot = MakeSingleTupleTableSlot(tupdesc);
pd->tupmap = convert_tuples_by_name(RelationGetDescr(parent),
tupdesc,
gettext_noop("could not convert row type"));
pd->tupmap = convert_tuples_by_name_map_if_req(RelationGetDescr(parent),
tupdesc,
gettext_noop("could not convert row type"));
}
else
{

View File

@ -1161,11 +1161,9 @@ lreplace:;
map_index = resultRelInfo - mtstate->resultRelInfo;
Assert(map_index >= 0 && map_index < mtstate->mt_nplans);
tupconv_map = tupconv_map_for_subplan(mtstate, map_index);
tuple = ConvertPartitionTupleSlot(tupconv_map,
tuple,
proute->root_tuple_slot,
&slot,
true);
if (tupconv_map != NULL)
slot = execute_attr_map_slot(tupconv_map->attrMap,
slot, proute->root_tuple_slot);
/*
* Prepare for tuple routing, making it look like we're inserting
@ -1703,6 +1701,7 @@ ExecPrepareTupleRouting(ModifyTableState *mtstate,
int partidx;
ResultRelInfo *partrel;
HeapTuple tuple;
TupleConversionMap *map;
/*
* Determine the target partition. If ExecFindPartition does not find a
@ -1790,11 +1789,16 @@ ExecPrepareTupleRouting(ModifyTableState *mtstate,
/*
* Convert the tuple, if necessary.
*/
ConvertPartitionTupleSlot(proute->parent_child_tupconv_maps[partidx],
tuple,
proute->partition_tuple_slot,
&slot,
true);
map = proute->parent_child_tupconv_maps[partidx];
if (map != NULL)
{
TupleTableSlot *new_slot;
Assert(proute->partition_tuple_slots != NULL &&
proute->partition_tuple_slots[partidx] != NULL);
new_slot = proute->partition_tuple_slots[partidx];
slot = execute_attr_map_slot(map->attrMap, slot, new_slot);
}
/* Initialize information needed to handle ON CONFLICT DO UPDATE. */
Assert(mtstate != NULL);