0003-ExecSetupPartitionTupleRouting-refactoring.patch

text/plain

Filename: 0003-ExecSetupPartitionTupleRouting-refactoring.patch
Type: text/plain
Part: 2
Message: non-bulk inserts and tuple routing

Patch

Format: format-patch
Series: patch 0003
Subject: ExecSetupPartitionTupleRouting refactoring
File+
src/backend/commands/copy.c 1 21
src/backend/executor/execPartition.c 30 39
src/backend/executor/nodeModifyTable.c 1 24
src/include/executor/execPartition.h 1 8
From 6ea3100c3df46ee131ea3d7590eaba378536c320 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Tue, 19 Dec 2017 16:20:09 +0900
Subject: [PATCH 3/4] ExecSetupPartitionTupleRouting refactoring

---
 src/backend/commands/copy.c            | 22 +----------
 src/backend/executor/execPartition.c   | 69 +++++++++++++++-------------------
 src/backend/executor/nodeModifyTable.c | 25 +-----------
 src/include/executor/execPartition.h   |  9 +----
 4 files changed, 33 insertions(+), 92 deletions(-)

diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 280d449dec..e7fe020fa7 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -2466,12 +2466,6 @@ CopyFrom(CopyState cstate)
 	 */
 	if (cstate->rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
 	{
-		PartitionDispatch *partition_dispatch_info;
-		ResultRelInfo **partitions;
-		TupleConversionMap **partition_tupconv_maps;
-		TupleTableSlot *partition_tuple_slot;
-		int			num_parted,
-					num_partitions;
 		ModifyTable *node = makeNode(ModifyTable);
 
 		/* Just need make this field appear valid. */
@@ -2479,21 +2473,7 @@ CopyFrom(CopyState cstate)
 		mtstate->ps.plan = (Plan *) node;
 		mtstate->ps.state = estate;
 		mtstate->resultRelInfo = resultRelInfo;
-		ExecSetupPartitionTupleRouting(mtstate,
-									   cstate->rel,
-									   node->nominalRelation,
-									   estate,
-									   &partition_dispatch_info,
-									   &partitions,
-									   &partition_tupconv_maps,
-									   &partition_tuple_slot,
-									   &num_parted, &num_partitions);
-		mtstate->mt_partition_dispatch_info = partition_dispatch_info;
-		mtstate->mt_num_dispatch = num_parted;
-		mtstate->mt_partitions = partitions;
-		mtstate->mt_num_partitions = num_partitions;
-		mtstate->mt_partition_tupconv_maps = partition_tupconv_maps;
-		mtstate->mt_partition_tuple_slot = partition_tuple_slot;
+		ExecSetupPartitionTupleRouting(mtstate, cstate->rel);
 
 		/*
 		 * If we are capturing transition tuples, they may need to be
diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index a40c174230..a495b165bd 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -41,42 +41,19 @@ static char *ExecBuildSlotPartitionKeyDescription(Relation rel,
  * ExecSetupPartitionTupleRouting - set up information needed during
  * tuple routing for partitioned tables
  *
- * Output arguments:
- * 'pd' receives an array of PartitionDispatch objects with one entry for
- *		every partitioned table in the partition tree
- * 'partitions' receives an array of ResultRelInfo* objects with one entry for
- *		every leaf partition in the partition tree
- * 'tup_conv_maps' receives an array of TupleConversionMap objects with one
- *		entry for every leaf partition (required to convert input tuple based
- *		on the root table's rowtype to a leaf partition's rowtype after tuple
- *		routing is done)
- * 'partition_tuple_slot' receives a standalone TupleTableSlot to be used
- *		to manipulate any given leaf partition's rowtype after that partition
- *		is chosen by tuple-routing.
- * 'num_parted' receives the number of partitioned tables in the partition
- *		tree (= the number of entries in the 'pd' output array)
- * 'num_partitions' receives the number of leaf partitions in the partition
- *		tree (= the number of entries in the 'partitions' and 'tup_conv_maps'
- *		output arrays
- *
  * Note that all the relations in the partition tree are locked using the
  * RowExclusiveLock mode upon return from this function.
  */
 void
-ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
-							   Relation rel,
-							   Index resultRTindex,
-							   EState *estate,
-							   PartitionDispatch **pd,
-							   ResultRelInfo ***partitions,
-							   TupleConversionMap ***tup_conv_maps,
-							   TupleTableSlot **partition_tuple_slot,
-							   int *num_parted, int *num_partitions)
+ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
 {
 	TupleDesc	tupDesc = RelationGetDescr(rel);
 	List	   *leaf_parts;
 	ListCell   *cell;
 	int			i;
+	EState	   *estate = mtstate->ps.state;
+	ModifyTable *node = (ModifyTable *) mtstate->ps.plan;
+	Index		resultRTindex = node->nominalRelation;
 	ResultRelInfo *leaf_part_rri;
 
 	/*
@@ -84,23 +61,35 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
 	 * partitions.
 	 */
 	(void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL);
-	*pd = RelationGetPartitionDispatchInfo(rel, num_parted, &leaf_parts);
-	*num_partitions = list_length(leaf_parts);
-	*partitions = (ResultRelInfo **) palloc(*num_partitions *
-											sizeof(ResultRelInfo *));
-	*tup_conv_maps = (TupleConversionMap **) palloc0(*num_partitions *
-													 sizeof(TupleConversionMap *));
+	mtstate->mt_partition_dispatch_info =
+				RelationGetPartitionDispatchInfo(rel,
+												 &mtstate->mt_num_dispatch,
+												 &leaf_parts);
+	mtstate->mt_num_partitions = list_length(leaf_parts);
 
 	/*
+	 * Allocate an array of ResultRelInfo pointers, but actual
+	 * ResultRelInfo's will be allocated if and when needed.  See
+	 * ExecFindPartition where it's done.
+	 */
+	mtstate->mt_partitions = (ResultRelInfo **)
+										 palloc0(sizeof(ResultRelInfo *) *
+												 mtstate->mt_num_partitions);
+	/* Ditto. */
+	mtstate->mt_partition_tupconv_maps =
+							(TupleConversionMap **)
+										palloc0(sizeof(TupleConversionMap *) *
+												mtstate->mt_num_partitions);
+	/*
 	 * 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.
 	 */
-	*partition_tuple_slot = MakeTupleTableSlot();
+	mtstate->mt_partition_tuple_slot = MakeTupleTableSlot();
 
-	leaf_part_rri = (ResultRelInfo *) palloc0(*num_partitions *
-											  sizeof(ResultRelInfo));
+	leaf_part_rri = (ResultRelInfo *) palloc0(sizeof(ResultRelInfo) *
+											  mtstate->mt_num_partitions);
 	i = 0;
 	foreach(cell, leaf_parts)
 	{
@@ -119,8 +108,10 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
 		 * Save a tuple conversion map to convert a tuple routed to this
 		 * partition from the parent's type to the partition's.
 		 */
-		(*tup_conv_maps)[i] = convert_tuples_by_name(tupDesc, part_tupdesc,
-													 gettext_noop("could not convert row type"));
+		mtstate->mt_partition_tupconv_maps[i] =
+								convert_tuples_by_name(tupDesc,
+													   part_tupdesc,
+									gettext_noop("could not convert row type"));
 
 		InitResultRelInfo(leaf_part_rri,
 						  partrel,
@@ -149,7 +140,7 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
 		estate->es_leaf_result_relations =
 			lappend(estate->es_leaf_result_relations, leaf_part_rri);
 
-		(*partitions)[i] = leaf_part_rri++;
+		mtstate->mt_partitions[i] = leaf_part_rri++;
 		i++;
 	}
 }
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index f836dd3703..6a3b171587 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -1942,30 +1942,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
 	/* Build state for INSERT tuple routing */
 	if (operation == CMD_INSERT &&
 		rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
-	{
-		PartitionDispatch *partition_dispatch_info;
-		ResultRelInfo **partitions;
-		TupleConversionMap **partition_tupconv_maps;
-		TupleTableSlot *partition_tuple_slot;
-		int			num_parted,
-					num_partitions;
-
-		ExecSetupPartitionTupleRouting(mtstate,
-									   rel,
-									   node->nominalRelation,
-									   estate,
-									   &partition_dispatch_info,
-									   &partitions,
-									   &partition_tupconv_maps,
-									   &partition_tuple_slot,
-									   &num_parted, &num_partitions);
-		mtstate->mt_partition_dispatch_info = partition_dispatch_info;
-		mtstate->mt_num_dispatch = num_parted;
-		mtstate->mt_partitions = partitions;
-		mtstate->mt_num_partitions = num_partitions;
-		mtstate->mt_partition_tupconv_maps = partition_tupconv_maps;
-		mtstate->mt_partition_tuple_slot = partition_tuple_slot;
-	}
+		ExecSetupPartitionTupleRouting(mtstate, rel);
 
 	/*
 	 * Build state for collecting transition tuples.  This requires having a
diff --git a/src/include/executor/execPartition.h b/src/include/executor/execPartition.h
index 19e3b9d233..c3ddf879b9 100644
--- a/src/include/executor/execPartition.h
+++ b/src/include/executor/execPartition.h
@@ -50,14 +50,7 @@ typedef struct PartitionDispatchData
 typedef struct PartitionDispatchData *PartitionDispatch;
 
 extern void ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
-							   Relation rel,
-							   Index resultRTindex,
-							   EState *estate,
-							   PartitionDispatch **pd,
-							   ResultRelInfo ***partitions,
-							   TupleConversionMap ***tup_conv_maps,
-							   TupleTableSlot **partition_tuple_slot,
-							   int *num_parted, int *num_partitions);
+							   Relation rel);
 extern int ExecFindPartition(ModifyTableState *mtstate, TupleTableSlot *slot);
 
 #endif							/* EXECPARTITION_H */
-- 
2.11.0