Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc6337d

Browse files
committed
Simplify and encapsulate tuple routing support code.
Instead of having ExecSetupPartitionTupleRouting return multiple outparameters, have it return a pointer to a structure containing all ofthose different things. Also, provide and use a cleanup function,ExecCleanupTupleRouting, instead of cleaning up all of the resourcesallocated by ExecSetupPartitionTupleRouting individually.Amit Khandekar, reviewed by Amit Langote, David Rowley, and meDiscussion:http://postgr.es/m/CAJ3gD9fWfxgKC+PfJZF3hkgAcNOy-LpfPxVYitDEXKHjeieWQQ@mail.gmail.com
1 parentd3fb72e commitcc6337d

File tree

5 files changed

+154
-190
lines changed

5 files changed

+154
-190
lines changed

‎src/backend/commands/copy.c

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,9 @@ typedef struct CopyStateData
166166
boolvolatile_defexprs;/* is any of defexprs volatile? */
167167
List*range_table;
168168

169-
PartitionDispatch*partition_dispatch_info;
170-
intnum_dispatch;/* Number of entries in the above array */
171-
intnum_partitions;/* Number of members in the following arrays */
172-
ResultRelInfo**partitions;/* Per partition result relation pointers */
173-
TupleConversionMap**partition_tupconv_maps;
174-
TupleTableSlot*partition_tuple_slot;
169+
/* Tuple-routing support info */
170+
PartitionTupleRouting*partition_tuple_routing;
171+
175172
TransitionCaptureState*transition_capture;
176173
TupleConversionMap**transition_tupconv_maps;
177174

@@ -2472,28 +2469,10 @@ CopyFrom(CopyState cstate)
24722469
*/
24732470
if (cstate->rel->rd_rel->relkind==RELKIND_PARTITIONED_TABLE)
24742471
{
2475-
PartitionDispatch*partition_dispatch_info;
2476-
ResultRelInfo**partitions;
2477-
TupleConversionMap**partition_tupconv_maps;
2478-
TupleTableSlot*partition_tuple_slot;
2479-
intnum_parted,
2480-
num_partitions;
2481-
2482-
ExecSetupPartitionTupleRouting(NULL,
2483-
cstate->rel,
2484-
1,
2485-
estate,
2486-
&partition_dispatch_info,
2487-
&partitions,
2488-
&partition_tupconv_maps,
2489-
&partition_tuple_slot,
2490-
&num_parted,&num_partitions);
2491-
cstate->partition_dispatch_info=partition_dispatch_info;
2492-
cstate->num_dispatch=num_parted;
2493-
cstate->partitions=partitions;
2494-
cstate->num_partitions=num_partitions;
2495-
cstate->partition_tupconv_maps=partition_tupconv_maps;
2496-
cstate->partition_tuple_slot=partition_tuple_slot;
2472+
PartitionTupleRouting*proute;
2473+
2474+
proute=cstate->partition_tuple_routing=
2475+
ExecSetupPartitionTupleRouting(NULL,cstate->rel,1,estate);
24972476

24982477
/*
24992478
* If we are capturing transition tuples, they may need to be
@@ -2506,11 +2485,11 @@ CopyFrom(CopyState cstate)
25062485
inti;
25072486

25082487
cstate->transition_tupconv_maps= (TupleConversionMap**)
2509-
palloc0(sizeof(TupleConversionMap*)*cstate->num_partitions);
2510-
for (i=0;i<cstate->num_partitions;++i)
2488+
palloc0(sizeof(TupleConversionMap*)*proute->num_partitions);
2489+
for (i=0;i<proute->num_partitions;++i)
25112490
{
25122491
cstate->transition_tupconv_maps[i]=
2513-
convert_tuples_by_name(RelationGetDescr(cstate->partitions[i]->ri_RelationDesc),
2492+
convert_tuples_by_name(RelationGetDescr(proute->partitions[i]->ri_RelationDesc),
25142493
RelationGetDescr(cstate->rel),
25152494
gettext_noop("could not convert row type"));
25162495
}
@@ -2530,7 +2509,7 @@ CopyFrom(CopyState cstate)
25302509
if ((resultRelInfo->ri_TrigDesc!=NULL&&
25312510
(resultRelInfo->ri_TrigDesc->trig_insert_before_row||
25322511
resultRelInfo->ri_TrigDesc->trig_insert_instead_row))||
2533-
cstate->partition_dispatch_info!=NULL||
2512+
cstate->partition_tuple_routing!=NULL||
25342513
cstate->volatile_defexprs)
25352514
{
25362515
useHeapMultiInsert= false;
@@ -2605,10 +2584,11 @@ CopyFrom(CopyState cstate)
26052584
ExecStoreTuple(tuple,slot,InvalidBuffer, false);
26062585

26072586
/* Determine the partition to heap_insert the tuple into */
2608-
if (cstate->partition_dispatch_info)
2587+
if (cstate->partition_tuple_routing)
26092588
{
26102589
intleaf_part_index;
26112590
TupleConversionMap*map;
2591+
PartitionTupleRouting*proute=cstate->partition_tuple_routing;
26122592

26132593
/*
26142594
* Away we go ... If we end up not finding a partition after all,
@@ -2619,11 +2599,11 @@ CopyFrom(CopyState cstate)
26192599
* partition, respectively.
26202600
*/
26212601
leaf_part_index=ExecFindPartition(resultRelInfo,
2622-
cstate->partition_dispatch_info,
2602+
proute->partition_dispatch_info,
26232603
slot,
26242604
estate);
26252605
Assert(leaf_part_index >=0&&
2626-
leaf_part_index<cstate->num_partitions);
2606+
leaf_part_index<proute->num_partitions);
26272607

26282608
/*
26292609
* If this tuple is mapped to a partition that is not same as the
@@ -2641,7 +2621,7 @@ CopyFrom(CopyState cstate)
26412621
* to the selected partition.
26422622
*/
26432623
saved_resultRelInfo=resultRelInfo;
2644-
resultRelInfo=cstate->partitions[leaf_part_index];
2624+
resultRelInfo=proute->partitions[leaf_part_index];
26452625

26462626
/* We do not yet have a way to insert into a foreign partition */
26472627
if (resultRelInfo->ri_FdwRoutine)
@@ -2688,7 +2668,7 @@ CopyFrom(CopyState cstate)
26882668
* We might need to convert from the parent rowtype to the
26892669
* partition rowtype.
26902670
*/
2691-
map=cstate->partition_tupconv_maps[leaf_part_index];
2671+
map=proute->partition_tupconv_maps[leaf_part_index];
26922672
if (map)
26932673
{
26942674
Relationpartrel=resultRelInfo->ri_RelationDesc;
@@ -2700,7 +2680,7 @@ CopyFrom(CopyState cstate)
27002680
* point on. Use a dedicated slot from this point on until
27012681
* we're finished dealing with the partition.
27022682
*/
2703-
slot=cstate->partition_tuple_slot;
2683+
slot=proute->partition_tuple_slot;
27042684
Assert(slot!=NULL);
27052685
ExecSetSlotDescriptor(slot,RelationGetDescr(partrel));
27062686
ExecStoreTuple(tuple,slot,InvalidBuffer, true);
@@ -2852,34 +2832,8 @@ CopyFrom(CopyState cstate)
28522832
ExecCloseIndices(resultRelInfo);
28532833

28542834
/* Close all the partitioned tables, leaf partitions, and their indices */
2855-
if (cstate->partition_dispatch_info)
2856-
{
2857-
inti;
2858-
2859-
/*
2860-
* Remember cstate->partition_dispatch_info[0] corresponds to the root
2861-
* partitioned table, which we must not try to close, because it is
2862-
* the main target table of COPY that will be closed eventually by
2863-
* DoCopy(). Also, tupslot is NULL for the root partitioned table.
2864-
*/
2865-
for (i=1;i<cstate->num_dispatch;i++)
2866-
{
2867-
PartitionDispatchpd=cstate->partition_dispatch_info[i];
2868-
2869-
heap_close(pd->reldesc,NoLock);
2870-
ExecDropSingleTupleTableSlot(pd->tupslot);
2871-
}
2872-
for (i=0;i<cstate->num_partitions;i++)
2873-
{
2874-
ResultRelInfo*resultRelInfo=cstate->partitions[i];
2875-
2876-
ExecCloseIndices(resultRelInfo);
2877-
heap_close(resultRelInfo->ri_RelationDesc,NoLock);
2878-
}
2879-
2880-
/* Release the standalone partition tuple descriptor */
2881-
ExecDropSingleTupleTableSlot(cstate->partition_tuple_slot);
2882-
}
2835+
if (cstate->partition_tuple_routing)
2836+
ExecCleanupTupleRouting(cstate->partition_tuple_routing);
28832837

28842838
/* Close any trigger target relations */
28852839
ExecCleanUpTriggerState(estate);

‎src/backend/executor/execPartition.c

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,68 +38,50 @@ static char *ExecBuildSlotPartitionKeyDescription(Relation rel,
3838
intmaxfieldlen);
3939

4040
/*
41-
* ExecSetupPartitionTupleRouting - set up information needed during
42-
* tuple routing for partitioned tables
43-
*
44-
* Output arguments:
45-
* 'pd' receives an array of PartitionDispatch objects with one entry for
46-
*every partitioned table in the partition tree
47-
* 'partitions' receives an array of ResultRelInfo* objects with one entry for
48-
*every leaf partition in the partition tree
49-
* 'tup_conv_maps' receives an array of TupleConversionMap objects with one
50-
*entry for every leaf partition (required to convert input tuple based
51-
*on the root table's rowtype to a leaf partition's rowtype after tuple
52-
*routing is done)
53-
* 'partition_tuple_slot' receives a standalone TupleTableSlot to be used
54-
*to manipulate any given leaf partition's rowtype after that partition
55-
*is chosen by tuple-routing.
56-
* 'num_parted' receives the number of partitioned tables in the partition
57-
*tree (= the number of entries in the 'pd' output array)
58-
* 'num_partitions' receives the number of leaf partitions in the partition
59-
*tree (= the number of entries in the 'partitions' and 'tup_conv_maps'
60-
*output arrays
41+
* ExecSetupPartitionTupleRouting - sets up information needed during
42+
* tuple routing for partitioned tables, encapsulates it in
43+
* PartitionTupleRouting, and returns it.
6144
*
6245
* Note that all the relations in the partition tree are locked using the
6346
* RowExclusiveLock mode upon return from this function.
6447
*/
65-
void
48+
PartitionTupleRouting*
6649
ExecSetupPartitionTupleRouting(ModifyTableState*mtstate,
67-
Relationrel,
68-
IndexresultRTindex,
69-
EState*estate,
70-
PartitionDispatch**pd,
71-
ResultRelInfo***partitions,
72-
TupleConversionMap***tup_conv_maps,
73-
TupleTableSlot**partition_tuple_slot,
74-
int*num_parted,int*num_partitions)
50+
Relationrel,IndexresultRTindex,
51+
EState*estate)
7552
{
7653
TupleDesctupDesc=RelationGetDescr(rel);
7754
List*leaf_parts;
7855
ListCell*cell;
7956
inti;
8057
ResultRelInfo*leaf_part_rri;
58+
PartitionTupleRouting*proute;
8159

8260
/*
8361
* Get the information about the partition tree after locking all the
8462
* partitions.
8563
*/
8664
(void)find_all_inheritors(RelationGetRelid(rel),RowExclusiveLock,NULL);
87-
*pd=RelationGetPartitionDispatchInfo(rel,num_parted,&leaf_parts);
88-
*num_partitions=list_length(leaf_parts);
89-
*partitions= (ResultRelInfo**)palloc(*num_partitions*
90-
sizeof(ResultRelInfo*));
91-
*tup_conv_maps= (TupleConversionMap**)palloc0(*num_partitions*
92-
sizeof(TupleConversionMap*));
65+
proute= (PartitionTupleRouting*)palloc0(sizeof(PartitionTupleRouting));
66+
proute->partition_dispatch_info=
67+
RelationGetPartitionDispatchInfo(rel,&proute->num_dispatch,
68+
&leaf_parts);
69+
proute->num_partitions=list_length(leaf_parts);
70+
proute->partitions= (ResultRelInfo**)palloc(proute->num_partitions*
71+
sizeof(ResultRelInfo*));
72+
proute->partition_tupconv_maps=
73+
(TupleConversionMap**)palloc0(proute->num_partitions*
74+
sizeof(TupleConversionMap*));
9375

9476
/*
9577
* Initialize an empty slot that will be used to manipulate tuples of any
9678
* given partition's rowtype. It is attached to the caller-specified node
9779
* (such as ModifyTableState) and released when the node finishes
9880
* processing.
9981
*/
100-
*partition_tuple_slot=MakeTupleTableSlot();
82+
proute->partition_tuple_slot=MakeTupleTableSlot();
10183

102-
leaf_part_rri= (ResultRelInfo*)palloc0(*num_partitions*
84+
leaf_part_rri= (ResultRelInfo*)palloc0(proute->num_partitions*
10385
sizeof(ResultRelInfo));
10486
i=0;
10587
foreach(cell,leaf_parts)
@@ -109,8 +91,8 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
10991

11092
/*
11193
* We locked all the partitions above including the leaf partitions.
112-
* Note that each of the relations in*partitions are eventually
113-
* closed by the caller.
94+
* Note that each of the relations inproute->partitions are
95+
*eventuallyclosed by the caller.
11496
*/
11597
partrel=heap_open(lfirst_oid(cell),NoLock);
11698
part_tupdesc=RelationGetDescr(partrel);
@@ -119,8 +101,9 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
119101
* Save a tuple conversion map to convert a tuple routed to this
120102
* partition from the parent's type to the partition's.
121103
*/
122-
(*tup_conv_maps)[i]=convert_tuples_by_name(tupDesc,part_tupdesc,
123-
gettext_noop("could not convert row type"));
104+
proute->partition_tupconv_maps[i]=
105+
convert_tuples_by_name(tupDesc,part_tupdesc,
106+
gettext_noop("could not convert row type"));
124107

125108
InitResultRelInfo(leaf_part_rri,
126109
partrel,
@@ -149,9 +132,11 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
149132
estate->es_leaf_result_relations=
150133
lappend(estate->es_leaf_result_relations,leaf_part_rri);
151134

152-
(*partitions)[i]=leaf_part_rri++;
135+
proute->partitions[i]=leaf_part_rri++;
153136
i++;
154137
}
138+
139+
returnproute;
155140
}
156141

157142
/*
@@ -272,6 +257,45 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
272257
returnresult;
273258
}
274259

260+
/*
261+
* ExecCleanupTupleRouting -- Clean up objects allocated for partition tuple
262+
* routing.
263+
*
264+
* Close all the partitioned tables, leaf partitions, and their indices.
265+
*/
266+
void
267+
ExecCleanupTupleRouting(PartitionTupleRouting*proute)
268+
{
269+
inti;
270+
271+
/*
272+
* Remember, proute->partition_dispatch_info[0] corresponds to the root
273+
* partitioned table, which we must not try to close, because it is the
274+
* main target table of the query that will be closed by callers such as
275+
* ExecEndPlan() or DoCopy(). Also, tupslot is NULL for the root
276+
* partitioned table.
277+
*/
278+
for (i=1;i<proute->num_dispatch;i++)
279+
{
280+
PartitionDispatchpd=proute->partition_dispatch_info[i];
281+
282+
heap_close(pd->reldesc,NoLock);
283+
ExecDropSingleTupleTableSlot(pd->tupslot);
284+
}
285+
286+
for (i=0;i<proute->num_partitions;i++)
287+
{
288+
ResultRelInfo*resultRelInfo=proute->partitions[i];
289+
290+
ExecCloseIndices(resultRelInfo);
291+
heap_close(resultRelInfo->ri_RelationDesc,NoLock);
292+
}
293+
294+
/* Release the standalone partition tuple descriptor, if any */
295+
if (proute->partition_tuple_slot)
296+
ExecDropSingleTupleTableSlot(proute->partition_tuple_slot);
297+
}
298+
275299
/*
276300
* RelationGetPartitionDispatchInfo
277301
*Returns information necessary to route tuples down a partition tree

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp