From 761e7cde7e93da0030a879925e75c89ff1099e9c Mon Sep 17 00:00:00 2001
From: Jeevan Ladhe <jeevan.ladhe@enterprisedb.com>
Date: Tue, 22 Aug 2017 07:40:55 +0530
Subject: [PATCH 1/2] Refactor RelationBuildPartitionDesc().

This patch simplifies RelationBuildPartitionDesc() to optimize the
way we find out the disctinct bounds in case of range partitioned
table.

Beena Emerson, revised by Jeevan Ladhe.
---
 src/backend/catalog/partition.c | 53 +++++++++++++----------------------------
 1 file changed, 16 insertions(+), 37 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 06444f4931..0695f86f88 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -335,21 +335,18 @@ RelationBuildPartitionDesc(Relation rel)
 		}
 		else if (key->strategy == PARTITION_STRATEGY_RANGE)
 		{
-			int			j,
-						k;
+			int			k;
 			PartitionRangeBound **all_bounds,
 					   *prev;
-			bool	   *distinct_indexes;
 
 			all_bounds = (PartitionRangeBound **) palloc0(2 * nparts *
 														  sizeof(PartitionRangeBound *));
-			distinct_indexes = (bool *) palloc(2 * nparts * sizeof(bool));
 
 			/*
 			 * Create a unified list of range bounds across all the
 			 * partitions.
 			 */
-			i = j = 0;
+			i = ndatums = 0;
 			foreach(cell, boundspecs)
 			{
 				PartitionBoundSpec *spec = castNode(PartitionBoundSpec,
@@ -364,12 +361,12 @@ RelationBuildPartitionDesc(Relation rel)
 											 true);
 				upper = make_one_range_bound(key, i, spec->upperdatums,
 											 false);
-				all_bounds[j] = lower;
-				all_bounds[j + 1] = upper;
-				j += 2;
+				all_bounds[ndatums++] = lower;
+				all_bounds[ndatums++] = upper;
 				i++;
 			}
-			Assert(j == 2 * nparts);
+
+			Assert(ndatums == nparts * 2);
 
 			/* Sort all the bounds in ascending order */
 			qsort_arg(all_bounds, 2 * nparts,
@@ -377,13 +374,11 @@ RelationBuildPartitionDesc(Relation rel)
 					  qsort_partition_rbound_cmp,
 					  (void *) key);
 
-			/*
-			 * Count the number of distinct bounds to allocate an array of
-			 * that size.
-			 */
-			ndatums = 0;
+			rbounds = (PartitionRangeBound **) palloc(ndatums *
+													  sizeof(PartitionRangeBound *));
+			k = 0;
 			prev = NULL;
-			for (i = 0; i < 2 * nparts; i++)
+			for (i = 0; i < ndatums; i++)
 			{
 				PartitionRangeBound *cur = all_bounds[i];
 				bool		is_distinct = false;
@@ -420,34 +415,18 @@ RelationBuildPartitionDesc(Relation rel)
 				}
 
 				/*
-				 * Count the current bound if it is distinct from the previous
-				 * one.  Also, store if the index i contains a distinct bound
-				 * that we'd like put in the relcache array.
+				 * Only if the bound is distinct save it into a temporary
+				 * array i.e. rbounds which is later copied into boundinfo
+				 * datums array.
 				 */
 				if (is_distinct)
-				{
-					distinct_indexes[i] = true;
-					ndatums++;
-				}
-				else
-					distinct_indexes[i] = false;
+					rbounds[k++] = all_bounds[i];
 
 				prev = cur;
 			}
 
-			/*
-			 * Finally save them in an array from where they will be copied
-			 * into the relcache.
-			 */
-			rbounds = (PartitionRangeBound **) palloc(ndatums *
-													  sizeof(PartitionRangeBound *));
-			k = 0;
-			for (i = 0; i < 2 * nparts; i++)
-			{
-				if (distinct_indexes[i])
-					rbounds[k++] = all_bounds[i];
-			}
-			Assert(k == ndatums);
+			/* Update ndatums to hold the count of distinct datums. */
+			ndatums = k;
 		}
 		else
 			elog(ERROR, "unexpected partition strategy: %d",
-- 
2.13.3

