diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index 8440a76fbd..430ae037fd 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -2010,27 +2010,26 @@ hash_choose_num_partitions(double input_groups, double hashentrysize,
 	/* make enough partitions so that each one is likely to fit in memory */
 	npartitions = 1 + (mem_wanted / (hash_mem * 1024L));
 
+	/* ceil(log2(npartitions)) */
+	partition_bits = my_log2(npartitions);
+
+	/* number of partitions will be a power of two */
+ 	npartitions = 1L << partition_bits;
+
+    /* make sure that number of partitions is valid */
 	if (npartitions > partition_limit)
 		npartitions = partition_limit;
-
 	if (npartitions < HASHAGG_MIN_PARTITIONS)
 		npartitions = HASHAGG_MIN_PARTITIONS;
 	if (npartitions > HASHAGG_MAX_PARTITIONS)
 		npartitions = HASHAGG_MAX_PARTITIONS;
 
-	/* ceil(log2(npartitions)) */
+	/* make sure that partition_bits match npartitions */
 	partition_bits = my_log2(npartitions);
 
-	/* make sure that we don't exhaust the hash bits */
-	if (partition_bits + used_bits >= 32)
-		partition_bits = 32 - used_bits;
-
 	if (log2_npartitions != NULL)
 		*log2_npartitions = partition_bits;
 
-	/* number of partitions will be a power of two */
-	npartitions = 1L << partition_bits;
-
 	return npartitions;
 }