From 64494e6be2d5e365e2161684791ae291a2751fcc Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Tue, 10 Oct 2017 14:36:24 +0530
Subject: [PATCH 1/3] partition_bounds_copy code refactoring v1

---
 src/backend/catalog/partition.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index ebda85e4ef..b82ceb903d 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -149,6 +149,7 @@ static int partition_bound_bsearch(PartitionKey key,
 						void *probe, bool probe_is_bound, bool *is_equal);
 static void get_partition_dispatch_recurse(Relation rel, Relation parent,
 							   List **pds, List **leaf_part_oids);
+static int get_partition_bound_num_indexes(PartitionBoundInfo b);
 
 /*
  * RelationBuildPartitionDesc
@@ -721,8 +722,7 @@ partition_bounds_copy(PartitionBoundInfo src,
 	ndatums = dest->ndatums = src->ndatums;
 	partnatts = key->partnatts;
 
-	/* Range partitioned table has an extra index. */
-	num_indexes = key->strategy == PARTITION_STRATEGY_RANGE ? ndatums + 1 : ndatums;
+	num_indexes = get_partition_bound_num_indexes(src);
 
 	/* List partitioned tables have only a single partition key. */
 	Assert(key->strategy != PARTITION_STRATEGY_LIST || partnatts == 1);
@@ -2894,3 +2894,34 @@ get_proposed_default_constraint(List *new_part_constraints)
 
 	return list_make1(defPartConstraint);
 }
+
+/*
+ * get_partition_bound_num_indexes
+ *
+ * Returns the number of the entries in the partition bound indexes array.
+ */
+static int
+get_partition_bound_num_indexes(PartitionBoundInfo bound)
+{
+	int num_indexes;
+
+	Assert(bound);
+
+	switch (bound->strategy)
+	{
+		case PARTITION_STRATEGY_LIST:
+			num_indexes = bound->ndatums;
+			break;
+
+		case PARTITION_STRATEGY_RANGE:
+			/* Range partitioned table has an extra index. */
+			num_indexes = bound->ndatums + 1;
+			break;
+
+		default:
+			elog(ERROR, "unexpected partition strategy: %d",
+				 (int) bound->strategy);
+	}
+
+	return num_indexes;
+}
-- 
2.14.1

