From e072a543f8ed3c53013d3c8df4d0f3cca03f13a0 Mon Sep 17 00:00:00 2001
From: Amul Sul <sulamul@gmail.com>
Date: Mon, 3 Jul 2017 15:07:33 +0530
Subject: [PATCH 1/2] Cleanup_v6

Code refactoring required for hash partitioning patch v14.
---
 src/backend/catalog/partition.c | 103 ++++++++++++++++++++++++----------------
 1 file changed, 61 insertions(+), 42 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 7da2058..a7efe7e 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -1942,9 +1942,7 @@ get_partition_for_tuple(PartitionDispatch *pd,
 	PartitionDispatch parent;
 	Datum		values[PARTITION_MAX_KEYS];
 	bool		isnull[PARTITION_MAX_KEYS];
-	int			cur_offset,
-				cur_index;
-	int			i,
+	int			cur_index,
 				result;
 	ExprContext *ecxt = GetPerTupleExprContext(estate);
 	TupleTableSlot *ecxt_scantuple_old = ecxt->ecxt_scantuple;
@@ -1988,48 +1986,69 @@ get_partition_for_tuple(PartitionDispatch *pd,
 		ecxt->ecxt_scantuple = slot;
 		FormPartitionKeyDatum(parent, slot, estate, values, isnull);
 
-		if (key->strategy == PARTITION_STRATEGY_RANGE)
-		{
-			/*
-			 * Since we cannot route tuples with NULL partition keys through a
-			 * range-partitioned table, simply return that no partition exists
-			 */
-			for (i = 0; i < key->partnatts; i++)
-			{
-				if (isnull[i])
-				{
-					*failed_at = parent;
-					*failed_slot = slot;
-					result = -1;
-					goto error_exit;
-				}
-			}
-		}
-
 		/*
-		 * A null partition key is only acceptable if null-accepting list
-		 * partition exists.
+		 * cur_index should be reset at every iteration here.  Otherwise, we
+		 * might have hazardous array subscript.
 		 */
 		cur_index = -1;
-		if (isnull[0] && partition_bound_accepts_nulls(partdesc->boundinfo))
-			cur_index = partdesc->boundinfo->null_index;
-		else if (!isnull[0])
+
+		switch (key->strategy)
 		{
-			/* Else bsearch in partdesc->boundinfo */
-			bool		equal = false;
+			case PARTITION_STRATEGY_LIST:
 
-			cur_offset = partition_bound_bsearch(key, partdesc->boundinfo,
-												 values, false, &equal);
-			switch (key->strategy)
-			{
-				case PARTITION_STRATEGY_LIST:
+				/*
+				 * A null partition key is only acceptable if null-accepting
+				 * list partition exists.
+				 */
+				if (isnull[0])
+				{
+					if (partition_bound_accepts_nulls(partdesc->boundinfo))
+						cur_index = partdesc->boundinfo->null_index;
+				}
+				else
+				{
+					bool		equal = false;
+					int			cur_offset;
+
+					/* bsearch in partdesc->boundinfo */
+					cur_offset = partition_bound_bsearch(key,
+														 partdesc->boundinfo,
+														 values,
+														 false,
+														 &equal);
 					if (cur_offset >= 0 && equal)
 						cur_index = partdesc->boundinfo->indexes[cur_offset];
-					else
-						cur_index = -1;
-					break;
+				}
+				break;
 
-				case PARTITION_STRATEGY_RANGE:
+			case PARTITION_STRATEGY_RANGE:
+				{
+					bool		equal = false;
+					int			cur_offset;
+					int			i;
+
+					/*
+					 * Since we cannot route tuples with NULL partition keys
+					 * through a range-partitioned table, simply return that
+					 * no partition exists
+					 */
+					for (i = 0; i < key->partnatts; i++)
+					{
+						if (isnull[i])
+						{
+							*failed_at = parent;
+							*failed_slot = slot;
+							result = -1;
+							goto error_exit;
+						}
+					}
+
+					/* bsearch in partdesc->boundinfo */
+					cur_offset = partition_bound_bsearch(key,
+														 partdesc->boundinfo,
+														 values,
+														 false,
+														 &equal);
 
 					/*
 					 * Offset returned is such that the bound at offset is
@@ -2037,12 +2056,12 @@ get_partition_for_tuple(PartitionDispatch *pd,
 					 * at offset+1 would be the upper bound.
 					 */
 					cur_index = partdesc->boundinfo->indexes[cur_offset + 1];
-					break;
+				}
+				break;
 
-				default:
-					elog(ERROR, "unexpected partition strategy: %d",
-						 (int) key->strategy);
-			}
+			default:
+				elog(ERROR, "unexpected partition strategy: %d",
+					 (int) key->strategy);
 		}
 
 		/*
-- 
2.6.2

