From 7ae7551bb3de843d2b9b34b04751c5974f56b52e Mon Sep 17 00:00:00 2001
From: David Rowley <dgrowley@gmail.com>
Date: Thu, 1 Mar 2018 22:59:58 +1300
Subject: [PATCH v14 2/5] Extract code to build partition expressions into a
 reusable function

---
 src/backend/catalog/partition.c      | 50 ++++++++++++++++++++++++++++++++++++
 src/backend/optimizer/util/plancat.c | 36 +-------------------------
 src/include/catalog/partition.h      |  2 ++
 3 files changed, 53 insertions(+), 35 deletions(-)

diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 021170a..311d0d3 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -4200,3 +4200,53 @@ satisfies_hash_partition(PG_FUNCTION_ARGS)
 
 	PG_RETURN_BOOL(rowHash % modulus == remainder);
 }
+
+/*
+ * build_partition_expressions
+ *		Build a list of partition key expressions.  Plain attibute keys will
+ *		be build as Vars with the varno set to 'varno'.
+ */
+List **
+build_partition_expressions(PartitionKey partkey, Index varno)
+{
+	int			partnatts;
+	int			cnt;
+	List	  **partexprs;
+	ListCell   *lc;
+
+	partnatts = partkey->partnatts;
+	partexprs = (List **) palloc(sizeof(List *) * partnatts);
+	lc = list_head(partkey->partexprs);
+
+	for (cnt = 0; cnt < partnatts; cnt++)
+	{
+		Expr	   *partexpr;
+		AttrNumber	attno = partkey->partattrs[cnt];
+
+		if (attno != InvalidAttrNumber)
+		{
+			/* Single column partition key is stored as a Var node. */
+			Assert(attno > 0);
+
+			partexpr = (Expr *) makeVar(varno, attno,
+										partkey->parttypid[cnt],
+										partkey->parttypmod[cnt],
+										partkey->parttypcoll[cnt], 0);
+		}
+		else
+		{
+			if (lc == NULL)
+				elog(ERROR, "wrong number of partition key expressions");
+
+			/* Re-stamp the expression with given varno. */
+			partexpr = (Expr *) copyObject(lfirst(lc));
+			if (varno != 1)
+				ChangeVarNodes((Node *) partexpr, 1, varno, 0);
+			lc = lnext(lc);
+		}
+
+		partexprs[cnt] = list_make1(partexpr);
+	}
+
+	return partexprs;
+}
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index 4dcca8b..03c1c35 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -1993,9 +1993,6 @@ set_baserel_partition_key_exprs(Relation relation,
 {
 	PartitionKey partkey = RelationGetPartitionKey(relation);
 	int			partnatts;
-	int			cnt;
-	List	  **partexprs;
-	ListCell   *lc;
 	Index		varno = rel->relid;
 
 	Assert(IS_SIMPLE_REL(rel) && rel->relid > 0);
@@ -2004,39 +2001,8 @@ set_baserel_partition_key_exprs(Relation relation,
 	Assert(partkey != NULL);
 
 	partnatts = partkey->partnatts;
-	partexprs = (List **) palloc(sizeof(List *) * partnatts);
-	lc = list_head(partkey->partexprs);
-
-	for (cnt = 0; cnt < partnatts; cnt++)
-	{
-		Expr	   *partexpr;
-		AttrNumber	attno = partkey->partattrs[cnt];
-
-		if (attno != InvalidAttrNumber)
-		{
-			/* Single column partition key is stored as a Var node. */
-			Assert(attno > 0);
-
-			partexpr = (Expr *) makeVar(varno, attno,
-										partkey->parttypid[cnt],
-										partkey->parttypmod[cnt],
-										partkey->parttypcoll[cnt], 0);
-		}
-		else
-		{
-			if (lc == NULL)
-				elog(ERROR, "wrong number of partition key expressions");
-
-			/* Re-stamp the expression with given varno. */
-			partexpr = (Expr *) copyObject(lfirst(lc));
-			ChangeVarNodes((Node *) partexpr, 1, varno, 0);
-			lc = lnext(lc);
-		}
-
-		partexprs[cnt] = list_make1(partexpr);
-	}
 
-	rel->partexprs = partexprs;
+	rel->partexprs = build_partition_expressions(partkey, varno);
 
 	/*
 	 * A base relation can not have nullable partition key expressions. We
diff --git a/src/include/catalog/partition.h b/src/include/catalog/partition.h
index 4e9281d..8a487e0 100644
--- a/src/include/catalog/partition.h
+++ b/src/include/catalog/partition.h
@@ -162,4 +162,6 @@ extern int get_partition_for_tuple(Relation relation, Datum *values,
 extern Bitmapset *get_partitions_for_keys(PartitionPruneContext *context,
 						PartScanKeyInfo *keys);
 
+extern List **build_partition_expressions(PartitionKey partkey, Index varno);
+
 #endif							/* PARTITION_H */
-- 
1.9.5.msysgit.1

