partprune-check-zero-bounds-fix-v2.patch

text/plain

Filename: partprune-check-zero-bounds-fix-v2.patch
Type: text/plain
Part: 0
Message: Re: negative bitmapset member not allowed Error with partition pruning

Patch

Format: unified
Series: patch v2
File+
src/backend/executor/nodeAppend.c 5 0
src/backend/executor/nodeMergeAppend.c 5 0
src/backend/partitioning/partprune.c 16 1
diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c
index 5ce4fb43e1..86a68d3020 100644
--- a/src/backend/executor/nodeAppend.c
+++ b/src/backend/executor/nodeAppend.c
@@ -171,6 +171,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
 		{
 			/* We'll need to initialize all subplans */
 			nplans = list_length(node->appendplans);
+			Assert(nplans > 0);
 			validsubplans = bms_add_range(NULL, 0, nplans - 1);
 		}
 
@@ -179,7 +180,10 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
 		 * immediately, preventing later calls to ExecFindMatchingSubPlans.
 		 */
 		if (!prunestate->do_exec_prune)
+		{
+			Assert(nplans > 0);
 			appendstate->as_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
+		}
 	}
 	else
 	{
@@ -189,6 +193,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
 		 * When run-time partition pruning is not enabled we can just mark all
 		 * subplans as valid; they must also all be initialized.
 		 */
+		Assert(nplans > 0);
 		appendstate->as_valid_subplans = validsubplans =
 			bms_add_range(NULL, 0, nplans - 1);
 		appendstate->as_prune_state = NULL;
diff --git a/src/backend/executor/nodeMergeAppend.c b/src/backend/executor/nodeMergeAppend.c
index 64025733de..be43014cb8 100644
--- a/src/backend/executor/nodeMergeAppend.c
+++ b/src/backend/executor/nodeMergeAppend.c
@@ -131,6 +131,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
 		{
 			/* We'll need to initialize all subplans */
 			nplans = list_length(node->mergeplans);
+			Assert(nplans > 0);
 			validsubplans = bms_add_range(NULL, 0, nplans - 1);
 		}
 
@@ -139,7 +140,10 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
 		 * immediately, preventing later calls to ExecFindMatchingSubPlans.
 		 */
 		if (!prunestate->do_exec_prune)
+		{
+			Assert(nplans > 0);
 			mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
+		}
 	}
 	else
 	{
@@ -149,6 +153,7 @@ ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
 		 * When run-time partition pruning is not enabled we can just mark all
 		 * subplans as valid; they must also all be initialized.
 		 */
+		Assert(nplans > 0);
 		mergestate->ms_valid_subplans = validsubplans =
 			bms_add_range(NULL, 0, nplans - 1);
 		mergestate->ms_prune_state = NULL;
diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c
index 354eb0d4e6..630b6d58e9 100644
--- a/src/backend/partitioning/partprune.c
+++ b/src/backend/partitioning/partprune.c
@@ -486,7 +486,10 @@ get_matching_partitions(PartitionPruneContext *context, List *pruning_steps)
 
 	/* If there are no pruning steps then all partitions match. */
 	if (num_steps == 0)
+	{
+		Assert(context->nparts > 0);
 		return bms_add_range(NULL, 0, context->nparts - 1);
+	}
 
 	/*
 	 * Allocate space for individual pruning steps to store its result.  Each
@@ -2048,8 +2051,12 @@ get_matching_hash_bounds(PartitionPruneContext *context,
 				bms_make_singleton(rowHash % greatest_modulus);
 	}
 	else
+	{
+		/* Getting here means at least one hash partition exists. */
+		Assert(boundinfo->ndatums > 0);
 		result->bound_offsets = bms_add_range(NULL, 0,
 											  boundinfo->ndatums - 1);
+	}
 
 	/*
 	 * There is neither a special hash null partition or the default hash
@@ -2128,6 +2135,7 @@ get_matching_list_bounds(PartitionPruneContext *context,
 	 */
 	if (nvalues == 0)
 	{
+		Assert(boundinfo->ndatums > 0);
 		result->bound_offsets = bms_add_range(NULL, 0,
 											  boundinfo->ndatums - 1);
 		result->scan_default = partition_bound_has_default(boundinfo);
@@ -2140,6 +2148,7 @@ get_matching_list_bounds(PartitionPruneContext *context,
 		/*
 		 * First match to all bounds.  We'll remove any matching datums below.
 		 */
+		Assert(boundinfo->ndatums > 0);
 		result->bound_offsets = bms_add_range(NULL, 0,
 											  boundinfo->ndatums - 1);
 
@@ -2250,6 +2259,7 @@ get_matching_list_bounds(PartitionPruneContext *context,
 			break;
 	}
 
+	Assert(minoff >= 0 && maxoff >= 0);
 	result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
 	return result;
 }
@@ -2327,6 +2337,7 @@ get_matching_range_bounds(PartitionPruneContext *context,
 			maxoff--;
 
 		result->scan_default = partition_bound_has_default(boundinfo);
+		Assert(minoff >= 0 && maxoff >= 0);
 		result->bound_offsets = bms_add_range(NULL, minoff, maxoff);
 
 		return result;
@@ -2995,7 +3006,11 @@ perform_pruning_combine_step(PartitionPruneContext *context,
 	{
 		PartitionBoundInfo boundinfo = context->boundinfo;
 
-		result->bound_offsets = bms_add_range(NULL, 0, boundinfo->ndatums - 1);
+		if (boundinfo->ndatums > 0)
+			result->bound_offsets = bms_add_range(NULL, 0,
+												  boundinfo->ndatums - 1);
+		else
+			result->bound_offsets = NULL;
 		result->scan_default = partition_bound_has_default(boundinfo);
 		result->scan_null = partition_bound_accepts_nulls(boundinfo);
 		return result;