From 5dc7b848b7a1f259591492797913ac506e53d54c Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Thu, 8 Aug 2019 14:30:24 +0900
Subject: [PATCH 2/2] Improve constraint exclusion usage in partprune.c a
 little

---
 src/backend/partitioning/partprune.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index d399df6cf5..835740e546 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -858,6 +858,25 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
 	List	   *result = NIL;
 	ListCell   *lc;
 
+	/*
+	 * If the relation has the default partition, we should first check if
+	 * the clauses contradict its partition constraint, that is, if it's
+	 * sub-partitioned.  If it does, there's no need to generate any steps
+	 * as it'd already be proven that no partitions need to be scanned,
+	 * including the default partition.  When there's no default partition,
+	 * we can go the slightly cheaper route of generating the steps only to
+	 * find that none of the partitions match when they are executed.  Using
+	 * constraint exclusion here is a measure of last resort for when the
+	 * default partition is present, because it can't be pruned using the
+	 * steps generated from such clauses.
+	 */
+	if (partition_bound_has_default(context->rel->boundinfo) &&
+		predicate_refuted_by(context->rel->partition_qual, clauses, false))
+	{
+		context->contradictory = true;
+		return NIL;
+	}
+
 	memset(keyclauses, 0, sizeof(keyclauses));
 	foreach(lc, clauses)
 	{
@@ -1011,18 +1030,6 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context,
 		}
 
 		/*
-		 * If the clause contradicts the partition constraint, mark the clause
-		 * as contradictory and we're done.  This is particularly helpful to
-		 * prune the default partition.
-		 */
-		if (predicate_refuted_by(context->rel->partition_qual,
-								 list_make1(clause), false))
-		{
-			context->contradictory = true;
-			return NIL;
-		}
-
-		/*
 		 * See if we can match this clause to any of the partition keys.
 		 */
 		for (i = 0; i < part_scheme->partnatts; i++)
-- 
2.11.0

