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

Commit 1fc5c49

Browse files
committed
Refactor partition tuple routing code to reduce duplication.
Amit Langote
1 parent 3b790d2 commit 1fc5c49

File tree

4 files changed

+127
-118
lines changed

4 files changed

+127
-118
lines changed

src/backend/commands/copy.c

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,64 +1406,22 @@ BeginCopy(ParseState *pstate,
14061406
/* Initialize state for CopyFrom tuple routing. */
14071407
if (is_from && rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
14081408
{
1409-
List *leaf_parts;
1410-
ListCell *cell;
1411-
int i,
1412-
num_parted;
1413-
ResultRelInfo *leaf_part_rri;
1414-
1415-
/* Get the tuple-routing information and lock partitions */
1416-
cstate->partition_dispatch_info =
1417-
RelationGetPartitionDispatchInfo(rel, RowExclusiveLock,
1418-
&num_parted,
1419-
&leaf_parts);
1409+
PartitionDispatch *partition_dispatch_info;
1410+
ResultRelInfo *partitions;
1411+
TupleConversionMap **partition_tupconv_maps;
1412+
int num_parted,
1413+
num_partitions;
1414+
1415+
ExecSetupPartitionTupleRouting(rel,
1416+
&partition_dispatch_info,
1417+
&partitions,
1418+
&partition_tupconv_maps,
1419+
&num_parted, &num_partitions);
1420+
cstate->partition_dispatch_info = partition_dispatch_info;
14201421
cstate->num_dispatch = num_parted;
1421-
cstate->num_partitions = list_length(leaf_parts);
1422-
cstate->partitions = (ResultRelInfo *)
1423-
palloc(cstate->num_partitions *
1424-
sizeof(ResultRelInfo));
1425-
cstate->partition_tupconv_maps = (TupleConversionMap **)
1426-
palloc0(cstate->num_partitions *
1427-
sizeof(TupleConversionMap *));
1428-
1429-
leaf_part_rri = cstate->partitions;
1430-
i = 0;
1431-
foreach(cell, leaf_parts)
1432-
{
1433-
Relation partrel;
1434-
1435-
/*
1436-
* We locked all the partitions above including the leaf
1437-
* partitions. Note that each of the relations in
1438-
* cstate->partitions will be closed by CopyFrom() after it's
1439-
* finished with its processing.
1440-
*/
1441-
partrel = heap_open(lfirst_oid(cell), NoLock);
1442-
1443-
/*
1444-
* Verify result relation is a valid target for the current
1445-
* operation.
1446-
*/
1447-
CheckValidResultRel(partrel, CMD_INSERT);
1448-
1449-
InitResultRelInfo(leaf_part_rri,
1450-
partrel,
1451-
1, /* dummy */
1452-
false, /* no partition constraint
1453-
* check */
1454-
0);
1455-
1456-
/* Open partition indices */
1457-
ExecOpenIndices(leaf_part_rri, false);
1458-
1459-
if (!equalTupleDescs(tupDesc, RelationGetDescr(partrel)))
1460-
cstate->partition_tupconv_maps[i] =
1461-
convert_tuples_by_name(tupDesc,
1462-
RelationGetDescr(partrel),
1463-
gettext_noop("could not convert row type"));
1464-
leaf_part_rri++;
1465-
i++;
1466-
}
1422+
cstate->partitions = partitions;
1423+
cstate->num_partitions = num_partitions;
1424+
cstate->partition_tupconv_maps = partition_tupconv_maps;
14671425
}
14681426
}
14691427
else

src/backend/executor/execMain.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "miscadmin.h"
5252
#include "optimizer/clauses.h"
5353
#include "parser/parsetree.h"
54+
#include "rewrite/rewriteManip.h"
5455
#include "storage/bufmgr.h"
5556
#include "storage/lmgr.h"
5657
#include "tcop/utility.h"
@@ -2998,6 +2999,97 @@ EvalPlanQualEnd(EPQState *epqstate)
29982999
epqstate->origslot = NULL;
29993000
}
30003001

3002+
/*
3003+
* ExecSetupPartitionTupleRouting - set up information needed during
3004+
* tuple routing for partitioned tables
3005+
*
3006+
* Output arguments:
3007+
* 'pd' receives an array of PartitionDispatch objects with one entry for
3008+
* every partitioned table in the partition tree
3009+
* 'partitions' receives an array of ResultRelInfo objects with one entry for
3010+
* every leaf partition in the partition tree
3011+
* 'tup_conv_maps' receives an array of TupleConversionMap objects with one
3012+
* entry for every leaf partition (required to convert input tuple based
3013+
* on the root table's rowtype to a leaf partition's rowtype after tuple
3014+
* routing is done
3015+
* 'num_parted' receives the number of partitioned tables in the partition
3016+
* tree (= the number of entries in the 'pd' output array)
3017+
* 'num_partitions' receives the number of leaf partitions in the partition
3018+
* tree (= the number of entries in the 'partitions' and 'tup_conv_maps'
3019+
* output arrays
3020+
*
3021+
* Note that all the relations in the partition tree are locked using the
3022+
* RowExclusiveLock mode upon return from this function.
3023+
*/
3024+
void
3025+
ExecSetupPartitionTupleRouting(Relation rel,
3026+
PartitionDispatch **pd,
3027+
ResultRelInfo **partitions,
3028+
TupleConversionMap ***tup_conv_maps,
3029+
int *num_parted, int *num_partitions)
3030+
{
3031+
TupleDesc tupDesc = RelationGetDescr(rel);
3032+
List *leaf_parts;
3033+
ListCell *cell;
3034+
int i;
3035+
ResultRelInfo *leaf_part_rri;
3036+
3037+
/* Get the tuple-routing information and lock partitions */
3038+
*pd = RelationGetPartitionDispatchInfo(rel, RowExclusiveLock, num_parted,
3039+
&leaf_parts);
3040+
*num_partitions = list_length(leaf_parts);
3041+
*partitions = (ResultRelInfo *) palloc(*num_partitions *
3042+
sizeof(ResultRelInfo));
3043+
*tup_conv_maps = (TupleConversionMap **) palloc0(*num_partitions *
3044+
sizeof(TupleConversionMap *));
3045+
3046+
leaf_part_rri = *partitions;
3047+
i = 0;
3048+
foreach(cell, leaf_parts)
3049+
{
3050+
Relation partrel;
3051+
TupleDesc part_tupdesc;
3052+
3053+
/*
3054+
* We locked all the partitions above including the leaf partitions.
3055+
* Note that each of the relations in *partitions are eventually
3056+
* closed by the caller.
3057+
*/
3058+
partrel = heap_open(lfirst_oid(cell), NoLock);
3059+
part_tupdesc = RelationGetDescr(partrel);
3060+
3061+
/*
3062+
* Verify result relation is a valid target for the current operation.
3063+
*/
3064+
CheckValidResultRel(partrel, CMD_INSERT);
3065+
3066+
/*
3067+
* Save a tuple conversion map to convert a tuple routed to this
3068+
* partition from the parent's type to the partition's.
3069+
*/
3070+
(*tup_conv_maps)[i] = convert_tuples_by_name(tupDesc, part_tupdesc,
3071+
gettext_noop("could not convert row type"));
3072+
3073+
InitResultRelInfo(leaf_part_rri,
3074+
partrel,
3075+
1, /* dummy */
3076+
false,
3077+
0);
3078+
3079+
/*
3080+
* Open partition indices (remember we do not support ON CONFLICT in
3081+
* case of partitioned tables, so we do not need support information
3082+
* for speculative insertion)
3083+
*/
3084+
if (leaf_part_rri->ri_RelationDesc->rd_rel->relhasindex &&
3085+
leaf_part_rri->ri_IndexRelationDescs == NULL)
3086+
ExecOpenIndices(leaf_part_rri, false);
3087+
3088+
leaf_part_rri++;
3089+
i++;
3090+
}
3091+
}
3092+
30013093
/*
30023094
* ExecFindPartition -- Find a leaf partition in the partition tree rooted
30033095
* at parent, for the heap tuple contained in *slot

src/backend/executor/nodeModifyTable.c

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,68 +1718,22 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
17181718
if (operation == CMD_INSERT &&
17191719
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
17201720
{
1721-
int i,
1722-
j,
1723-
num_parted;
1724-
List *leaf_parts;
1725-
ListCell *cell;
1726-
ResultRelInfo *leaf_part_rri;
1727-
1728-
/* Get the tuple-routing information and lock partitions */
1729-
mtstate->mt_partition_dispatch_info =
1730-
RelationGetPartitionDispatchInfo(rel, RowExclusiveLock,
1731-
&num_parted,
1732-
&leaf_parts);
1721+
PartitionDispatch *partition_dispatch_info;
1722+
ResultRelInfo *partitions;
1723+
TupleConversionMap **partition_tupconv_maps;
1724+
int num_parted,
1725+
num_partitions;
1726+
1727+
ExecSetupPartitionTupleRouting(rel,
1728+
&partition_dispatch_info,
1729+
&partitions,
1730+
&partition_tupconv_maps,
1731+
&num_parted, &num_partitions);
1732+
mtstate->mt_partition_dispatch_info = partition_dispatch_info;
17331733
mtstate->mt_num_dispatch = num_parted;
1734-
mtstate->mt_num_partitions = list_length(leaf_parts);
1735-
mtstate->mt_partitions = (ResultRelInfo *)
1736-
palloc0(mtstate->mt_num_partitions *
1737-
sizeof(ResultRelInfo));
1738-
mtstate->mt_partition_tupconv_maps = (TupleConversionMap **)
1739-
palloc0(mtstate->mt_num_partitions *
1740-
sizeof(TupleConversionMap *));
1741-
1742-
leaf_part_rri = mtstate->mt_partitions;
1743-
i = j = 0;
1744-
foreach(cell, leaf_parts)
1745-
{
1746-
Oid partrelid = lfirst_oid(cell);
1747-
Relation partrel;
1748-
1749-
/*
1750-
* We locked all the partitions above including the leaf
1751-
* partitions. Note that each of the relations in
1752-
* mtstate->mt_partitions will be closed by ExecEndModifyTable().
1753-
*/
1754-
partrel = heap_open(partrelid, NoLock);
1755-
1756-
/*
1757-
* Verify result relation is a valid target for the current
1758-
* operation
1759-
*/
1760-
CheckValidResultRel(partrel, CMD_INSERT);
1761-
1762-
InitResultRelInfo(leaf_part_rri,
1763-
partrel,
1764-
1, /* dummy */
1765-
false, /* no partition constraint checks */
1766-
eflags);
1767-
1768-
/* Open partition indices (note: ON CONFLICT unsupported)*/
1769-
if (partrel->rd_rel->relhasindex && operation != CMD_DELETE &&
1770-
leaf_part_rri->ri_IndexRelationDescs == NULL)
1771-
ExecOpenIndices(leaf_part_rri, false);
1772-
1773-
if (!equalTupleDescs(RelationGetDescr(rel),
1774-
RelationGetDescr(partrel)))
1775-
mtstate->mt_partition_tupconv_maps[i] =
1776-
convert_tuples_by_name(RelationGetDescr(rel),
1777-
RelationGetDescr(partrel),
1778-
gettext_noop("could not convert row type"));
1779-
1780-
leaf_part_rri++;
1781-
i++;
1782-
}
1734+
mtstate->mt_partitions = partitions;
1735+
mtstate->mt_num_partitions = num_partitions;
1736+
mtstate->mt_partition_tupconv_maps = partition_tupconv_maps;
17831737
}
17841738

17851739
/*

src/include/executor/executor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ extern void EvalPlanQualSetPlan(EPQState *epqstate,
213213
extern void EvalPlanQualSetTuple(EPQState *epqstate, Index rti,
214214
HeapTuple tuple);
215215
extern HeapTuple EvalPlanQualGetTuple(EPQState *epqstate, Index rti);
216+
extern void ExecSetupPartitionTupleRouting(Relation rel,
217+
PartitionDispatch **pd,
218+
ResultRelInfo **partitions,
219+
TupleConversionMap ***tup_conv_maps,
220+
int *num_parted, int *num_partitions);
216221
extern int ExecFindPartition(ResultRelInfo *resultRelInfo,
217222
PartitionDispatch *pd,
218223
TupleTableSlot *slot,

0 commit comments

Comments
 (0)