Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 60f7c0a

Browse files
committed
Use ResultRelInfo ** rather than ResultRelInfo * for tuple routing.
The previous convention doesn't lend itself to creating ResultRelInfos lazily, as we already do in ExecGetTriggerResultRel. This patch doesn't make anything lazier than before, but the pending patch for UPDATE tuple routing proposes to do so (and there might be other opportunities as well). Amit Khandekar with some adjustments by me. Discussion: http://postgr.es/m/CA+TgmoYPVP9Lyf6vUFA5DwxS4c--x6LOj2y36BsJaYtp62eXPQ@mail.gmail.com
1 parent 305cf1f commit 60f7c0a

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

src/backend/commands/copy.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ typedef struct CopyStateData
167167
PartitionDispatch *partition_dispatch_info;
168168
int num_dispatch; /* Number of entries in the above array */
169169
int num_partitions; /* Number of members in the following arrays */
170-
ResultRelInfo *partitions; /* Per partition result relation */
170+
ResultRelInfo **partitions; /* Per partition result relation pointers */
171171
TupleConversionMap **partition_tupconv_maps;
172172
TupleTableSlot *partition_tuple_slot;
173173
TransitionCaptureState *transition_capture;
@@ -2459,7 +2459,7 @@ CopyFrom(CopyState cstate)
24592459
if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
24602460
{
24612461
PartitionDispatch *partition_dispatch_info;
2462-
ResultRelInfo *partitions;
2462+
ResultRelInfo **partitions;
24632463
TupleConversionMap **partition_tupconv_maps;
24642464
TupleTableSlot *partition_tuple_slot;
24652465
int num_parted,
@@ -2495,7 +2495,7 @@ CopyFrom(CopyState cstate)
24952495
for (i = 0; i < cstate->num_partitions; ++i)
24962496
{
24972497
cstate->transition_tupconv_maps[i] =
2498-
convert_tuples_by_name(RelationGetDescr(cstate->partitions[i].ri_RelationDesc),
2498+
convert_tuples_by_name(RelationGetDescr(cstate->partitions[i]->ri_RelationDesc),
24992499
RelationGetDescr(cstate->rel),
25002500
gettext_noop("could not convert row type"));
25012501
}
@@ -2626,7 +2626,7 @@ CopyFrom(CopyState cstate)
26262626
* to the selected partition.
26272627
*/
26282628
saved_resultRelInfo = resultRelInfo;
2629-
resultRelInfo = cstate->partitions + leaf_part_index;
2629+
resultRelInfo = cstate->partitions[leaf_part_index];
26302630

26312631
/* We do not yet have a way to insert into a foreign partition */
26322632
if (resultRelInfo->ri_FdwRoutine)
@@ -2856,7 +2856,7 @@ CopyFrom(CopyState cstate)
28562856
}
28572857
for (i = 0; i < cstate->num_partitions; i++)
28582858
{
2859-
ResultRelInfo *resultRelInfo = cstate->partitions + i;
2859+
ResultRelInfo *resultRelInfo = cstate->partitions[i];
28602860

28612861
ExecCloseIndices(resultRelInfo);
28622862
heap_close(resultRelInfo->ri_RelationDesc, NoLock);

src/backend/executor/execMain.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -3242,7 +3242,7 @@ EvalPlanQualEnd(EPQState *epqstate)
32423242
* Output arguments:
32433243
* 'pd' receives an array of PartitionDispatch objects with one entry for
32443244
* every partitioned table in the partition tree
3245-
* 'partitions' receives an array of ResultRelInfo objects with one entry for
3245+
* 'partitions' receives an array of ResultRelInfo* objects with one entry for
32463246
* every leaf partition in the partition tree
32473247
* 'tup_conv_maps' receives an array of TupleConversionMap objects with one
32483248
* entry for every leaf partition (required to convert input tuple based
@@ -3265,7 +3265,7 @@ ExecSetupPartitionTupleRouting(Relation rel,
32653265
Index resultRTindex,
32663266
EState *estate,
32673267
PartitionDispatch **pd,
3268-
ResultRelInfo **partitions,
3268+
ResultRelInfo ***partitions,
32693269
TupleConversionMap ***tup_conv_maps,
32703270
TupleTableSlot **partition_tuple_slot,
32713271
int *num_parted, int *num_partitions)
@@ -3283,8 +3283,8 @@ ExecSetupPartitionTupleRouting(Relation rel,
32833283
(void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL);
32843284
*pd = RelationGetPartitionDispatchInfo(rel, num_parted, &leaf_parts);
32853285
*num_partitions = list_length(leaf_parts);
3286-
*partitions = (ResultRelInfo *) palloc(*num_partitions *
3287-
sizeof(ResultRelInfo));
3286+
*partitions = (ResultRelInfo **) palloc(*num_partitions *
3287+
sizeof(ResultRelInfo *));
32883288
*tup_conv_maps = (TupleConversionMap **) palloc0(*num_partitions *
32893289
sizeof(TupleConversionMap *));
32903290

@@ -3296,7 +3296,8 @@ ExecSetupPartitionTupleRouting(Relation rel,
32963296
*/
32973297
*partition_tuple_slot = MakeTupleTableSlot();
32983298

3299-
leaf_part_rri = *partitions;
3299+
leaf_part_rri = (ResultRelInfo *) palloc0(*num_partitions *
3300+
sizeof(ResultRelInfo));
33003301
i = 0;
33013302
foreach(cell, leaf_parts)
33023303
{
@@ -3341,7 +3342,7 @@ ExecSetupPartitionTupleRouting(Relation rel,
33413342
estate->es_leaf_result_relations =
33423343
lappend(estate->es_leaf_result_relations, leaf_part_rri);
33433344

3344-
leaf_part_rri++;
3345+
(*partitions)[i] = leaf_part_rri++;
33453346
i++;
33463347
}
33473348
}

src/backend/executor/nodeModifyTable.c

+43-31
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ ExecInsert(ModifyTableState *mtstate,
303303
* the selected partition.
304304
*/
305305
saved_resultRelInfo = resultRelInfo;
306-
resultRelInfo = mtstate->mt_partitions + leaf_part_index;
306+
resultRelInfo = mtstate->mt_partitions[leaf_part_index];
307307

308308
/* We do not yet have a way to insert into a foreign partition */
309309
if (resultRelInfo->ri_FdwRoutine)
@@ -1498,25 +1498,11 @@ ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
14981498
if (mtstate->mt_transition_capture != NULL ||
14991499
mtstate->mt_oc_transition_capture != NULL)
15001500
{
1501-
ResultRelInfo *resultRelInfos;
15021501
int numResultRelInfos;
15031502

1504-
/* Find the set of partitions so that we can find their TupleDescs. */
1505-
if (mtstate->mt_partition_dispatch_info != NULL)
1506-
{
1507-
/*
1508-
* For INSERT via partitioned table, so we need TupleDescs based
1509-
* on the partition routing table.
1510-
*/
1511-
resultRelInfos = mtstate->mt_partitions;
1512-
numResultRelInfos = mtstate->mt_num_partitions;
1513-
}
1514-
else
1515-
{
1516-
/* Otherwise we need the ResultRelInfo for each subplan. */
1517-
resultRelInfos = mtstate->resultRelInfo;
1518-
numResultRelInfos = mtstate->mt_nplans;
1519-
}
1503+
numResultRelInfos = (mtstate->mt_partition_tuple_slot != NULL ?
1504+
mtstate->mt_num_partitions :
1505+
mtstate->mt_nplans);
15201506

15211507
/*
15221508
* Build array of conversion maps from each child's TupleDesc to the
@@ -1526,12 +1512,36 @@ ExecSetupTransitionCaptureState(ModifyTableState *mtstate, EState *estate)
15261512
*/
15271513
mtstate->mt_transition_tupconv_maps = (TupleConversionMap **)
15281514
palloc0(sizeof(TupleConversionMap *) * numResultRelInfos);
1529-
for (i = 0; i < numResultRelInfos; ++i)
1515+
1516+
/* Choose the right set of partitions */
1517+
if (mtstate->mt_partition_dispatch_info != NULL)
15301518
{
1531-
mtstate->mt_transition_tupconv_maps[i] =
1532-
convert_tuples_by_name(RelationGetDescr(resultRelInfos[i].ri_RelationDesc),
1533-
RelationGetDescr(targetRelInfo->ri_RelationDesc),
1534-
gettext_noop("could not convert row type"));
1519+
/*
1520+
* For tuple routing among partitions, we need TupleDescs based
1521+
* on the partition routing table.
1522+
*/
1523+
ResultRelInfo **resultRelInfos = mtstate->mt_partitions;
1524+
1525+
for (i = 0; i < numResultRelInfos; ++i)
1526+
{
1527+
mtstate->mt_transition_tupconv_maps[i] =
1528+
convert_tuples_by_name(RelationGetDescr(resultRelInfos[i]->ri_RelationDesc),
1529+
RelationGetDescr(targetRelInfo->ri_RelationDesc),
1530+
gettext_noop("could not convert row type"));
1531+
}
1532+
}
1533+
else
1534+
{
1535+
/* Otherwise we need the ResultRelInfo for each subplan. */
1536+
ResultRelInfo *resultRelInfos = mtstate->resultRelInfo;
1537+
1538+
for (i = 0; i < numResultRelInfos; ++i)
1539+
{
1540+
mtstate->mt_transition_tupconv_maps[i] =
1541+
convert_tuples_by_name(RelationGetDescr(resultRelInfos[i].ri_RelationDesc),
1542+
RelationGetDescr(targetRelInfo->ri_RelationDesc),
1543+
gettext_noop("could not convert row type"));
1544+
}
15351545
}
15361546

15371547
/*
@@ -1935,7 +1945,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
19351945
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
19361946
{
19371947
PartitionDispatch *partition_dispatch_info;
1938-
ResultRelInfo *partitions;
1948+
ResultRelInfo **partitions;
19391949
TupleConversionMap **partition_tupconv_maps;
19401950
TupleTableSlot *partition_tuple_slot;
19411951
int num_parted,
@@ -2014,14 +2024,16 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
20142024
mtstate->mt_nplans == 1);
20152025
wcoList = linitial(node->withCheckOptionLists);
20162026
plan = mtstate->mt_plans[0];
2017-
resultRelInfo = mtstate->mt_partitions;
20182027
for (i = 0; i < mtstate->mt_num_partitions; i++)
20192028
{
2020-
Relation partrel = resultRelInfo->ri_RelationDesc;
2029+
Relation partrel;
20212030
List *mapped_wcoList;
20222031
List *wcoExprs = NIL;
20232032
ListCell *ll;
20242033

2034+
resultRelInfo = mtstate->mt_partitions[i];
2035+
partrel = resultRelInfo->ri_RelationDesc;
2036+
20252037
/* varno = node->nominalRelation */
20262038
mapped_wcoList = map_partition_varattnos(wcoList,
20272039
node->nominalRelation,
@@ -2037,7 +2049,6 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
20372049

20382050
resultRelInfo->ri_WithCheckOptions = mapped_wcoList;
20392051
resultRelInfo->ri_WithCheckOptionExprs = wcoExprs;
2040-
resultRelInfo++;
20412052
}
20422053
}
20432054

@@ -2088,21 +2099,22 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
20882099
* will suffice. This only occurs for the INSERT case; UPDATE/DELETE
20892100
* are handled above.
20902101
*/
2091-
resultRelInfo = mtstate->mt_partitions;
20922102
returningList = linitial(node->returningLists);
20932103
for (i = 0; i < mtstate->mt_num_partitions; i++)
20942104
{
2095-
Relation partrel = resultRelInfo->ri_RelationDesc;
2105+
Relation partrel;
20962106
List *rlist;
20972107

2108+
resultRelInfo = mtstate->mt_partitions[i];
2109+
partrel = resultRelInfo->ri_RelationDesc;
2110+
20982111
/* varno = node->nominalRelation */
20992112
rlist = map_partition_varattnos(returningList,
21002113
node->nominalRelation,
21012114
partrel, rel, NULL);
21022115
resultRelInfo->ri_projectReturning =
21032116
ExecBuildProjectionInfo(rlist, econtext, slot, &mtstate->ps,
21042117
resultRelInfo->ri_RelationDesc->rd_att);
2105-
resultRelInfo++;
21062118
}
21072119
}
21082120
else
@@ -2376,7 +2388,7 @@ ExecEndModifyTable(ModifyTableState *node)
23762388
}
23772389
for (i = 0; i < node->mt_num_partitions; i++)
23782390
{
2379-
ResultRelInfo *resultRelInfo = node->mt_partitions + i;
2391+
ResultRelInfo *resultRelInfo = node->mt_partitions[i];
23802392

23812393
ExecCloseIndices(resultRelInfo);
23822394
heap_close(resultRelInfo->ri_RelationDesc, NoLock);

src/include/executor/executor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extern void ExecSetupPartitionTupleRouting(Relation rel,
210210
Index resultRTindex,
211211
EState *estate,
212212
PartitionDispatch **pd,
213-
ResultRelInfo **partitions,
213+
ResultRelInfo ***partitions,
214214
TupleConversionMap ***tup_conv_maps,
215215
TupleTableSlot **partition_tuple_slot,
216216
int *num_parted, int *num_partitions);

src/include/nodes/execnodes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ typedef struct ModifyTableState
979979
int mt_num_dispatch; /* Number of entries in the above array */
980980
int mt_num_partitions; /* Number of members in the following
981981
* arrays */
982-
ResultRelInfo *mt_partitions; /* Per partition result relation */
982+
ResultRelInfo **mt_partitions; /* Per partition result relation pointers */
983983
TupleConversionMap **mt_partition_tupconv_maps;
984984
/* Per partition tuple conversion map */
985985
TupleTableSlot *mt_partition_tuple_slot;

0 commit comments

Comments
 (0)