diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 6e5efe9..2bb3641 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -846,50 +846,28 @@ set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 	rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
 }
 
-/*
- * get_rel_partitions
- *		Return the list of partitions of rel that pass the query clauses
- *
- * Returned list contains the AppendInfos of the chosen partitions.
- */
-static List *
-get_rel_partitions(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
+static void
+get_matching_clause(RelOptInfo *rel, List *clauses, List **matchedclauses,
+					NullTestType *keynullness)
 {
-	Relation		parent = heap_open(rte->relid, NoLock);
-	PartitionDesc	partdesc = RelationGetPartitionDesc(parent);
-	List   *indexes;
-	List   *result = NIL;
-	ListCell   *lc1,
-			   *lc2;
+	ListCell   *lc;
 	int		keyPos;
-	List   *matchedclauses[PARTITION_MAX_KEYS];
-	NullTestType keynullness[PARTITION_MAX_KEYS];
-	Datum	minkeys[PARTITION_MAX_KEYS],
-			maxkeys[PARTITION_MAX_KEYS];
-	bool	need_next_min,
-			need_next_max,
-			minkey_set[PARTITION_MAX_KEYS],
-			maxkey_set[PARTITION_MAX_KEYS],
-			min_incl,
-			max_incl;
-	int		n_minkeys = 0,
-			n_maxkeys = 0,
-			i;
+	int		i;
 
 	/*
 	 * Match individual OpExprs in the query's restriction with individual
 	 * partition key columns.  There is one list per key.
 	 */
-	memset(keynullness, -1, sizeof(keynullness));
-	memset(matchedclauses, 0, sizeof(matchedclauses));
+	memset(keynullness, -1, PARTITION_MAX_KEYS * sizeof(NullTestType));
+	memset(matchedclauses, 0, PARTITION_MAX_KEYS * sizeof(List*));
 	keyPos = 0;
 	for (i = 0; i < rel->part_scheme->partnatts; i++)
 	{
 		Node   *partkey = linitial(rel->partexprs[i]);
 
-		foreach(lc2, rel->baserestrictinfo)
+		foreach(lc, clauses)
 		{
-			RestrictInfo   *rinfo = lfirst(lc2);
+			RestrictInfo   *rinfo = lfirst(lc);
 			Expr		   *clause = rinfo->clause;
 
 			if (is_opclause(clause))
@@ -948,6 +926,37 @@ get_rel_partitions(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
 		/* Onto finding clauses matching the next partition key. */
 		keyPos++;
 	}
+}
+
+/*
+ * get_rel_partitions
+ *		Return the list of partitions of rel that pass the query clauses
+ *
+ * Returned list contains the AppendInfos of the chosen partitions.
+ */
+static List *
+get_rel_partitions(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
+{
+	Relation		parent = heap_open(rte->relid, NoLock);
+	List   *indexes;
+	List   *result = NIL;
+	ListCell   *lc1;
+	List   *matchedclauses[PARTITION_MAX_KEYS];
+	NullTestType keynullness[PARTITION_MAX_KEYS];
+	Datum	minkeys[PARTITION_MAX_KEYS],
+			maxkeys[PARTITION_MAX_KEYS];
+	bool	need_next_min,
+			need_next_max,
+			minkey_set[PARTITION_MAX_KEYS],
+			maxkey_set[PARTITION_MAX_KEYS],
+			min_incl,
+			max_incl;
+	int		n_minkeys = 0,
+			n_maxkeys = 0,
+			i;
+
+	get_matching_clause(rel, rel->baserestrictinfo, matchedclauses,
+						keynullness);
 
 	/*
 	 * Determine the min keys and the max keys using btree semantics-based
