From 395d943f9fe86289d0831f11ef652f7342bfd792 Mon Sep 17 00:00:00 2001
From: Gregory Hennessy <greg.hennessy@gmail.com>
Date: Wed, 16 Jul 2025 20:22:58 -0400
Subject: [PATCH] Change algorithm to use sqrt of buffer size rather than log3

---
 src/backend/optimizer/path/allpaths.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 6cc6966b060..19e7b30453f 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -4273,18 +4273,21 @@ compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages,
 			int			heap_parallel_workers = 1;
 
 			/*
-			 * Select the number of workers based on the log of the size of
-			 * the relation.  This probably needs to be a good deal more
+			 * Select the number of workers based on the sqrt of the size of
+			 * the ratio of the heap_pages and the min table scan size.
+			 * This allocates more workers than the previous method based on log3
+			 * of the ratio. It is easier to make the number of workers smaller with a GUC
+			 * than to make the number larger.
+			 * This probably needs to be a good deal more
 			 * sophisticated, but we need something here for now.  Note that
 			 * the upper limit of the min_parallel_table_scan_size GUC is
 			 * chosen to prevent overflow here.
 			 */
 			heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
-			while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
+			while (heap_pages >= (BlockNumber) (heap_parallel_threshold*heap_parallel_workers*heap_parallel_workers))
 			{
 				heap_parallel_workers++;
-				heap_parallel_threshold *= 3;
-				if (heap_parallel_threshold > INT_MAX / 3)
+				if (heap_parallel_threshold *heap_parallel_workers*heap_parallel_workers> INT_MAX )
 					break;		/* avoid overflow */
 			}
 
@@ -4298,11 +4301,10 @@ compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages,
 
 			/* same calculation as for heap_pages above */
 			index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
-			while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
+			while (index_pages >= (BlockNumber) (index_parallel_threshold * index_parallel_workers*index_parallel_workers))
 			{
 				index_parallel_workers++;
-				index_parallel_threshold *= 3;
-				if (index_parallel_threshold > INT_MAX / 3)
+				if (index_parallel_threshold*index_parallel_workers*index_parallel_workers > INT_MAX)
 					break;		/* avoid overflow */
 			}
 
-- 
2.50.1

