Skip to content

Commit f2fc98f

Browse files
committed
Further twiddling of nodeHash.c hashtable sizing calculation.
On reflection, the submitted patch didn't really work to prevent the request size from exceeding MaxAllocSize, because of the fact that we'd happily round nbuckets up to the next power of 2 after we'd limited it to max_pointers. The simplest way to enforce the limit correctly is to round max_pointers down to a power of 2 when it isn't one already. (Note that the constraint to INT_MAX / 2, if it were doing anything useful at all, is properly applied after that.)
1 parent a31e64d commit f2fc98f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Diff for: src/backend/executor/nodeHash.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
415415
long hash_table_bytes;
416416
long skew_table_bytes;
417417
long max_pointers;
418+
long mppow2;
418419
int nbatch = 1;
419420
int nbuckets;
420421
double dbuckets;
@@ -485,14 +486,20 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
485486
*/
486487
max_pointers = (work_mem * 1024L) / sizeof(HashJoinTuple);
487488
max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
488-
/* also ensure we avoid integer overflow in nbatch and nbuckets */
489+
/* If max_pointers isn't a power of 2, must round it down to one */
490+
mppow2 = 1L << my_log2(max_pointers);
491+
if (max_pointers != mppow2)
492+
max_pointers = mppow2 / 2;
493+
494+
/* Also ensure we avoid integer overflow in nbatch and nbuckets */
489495
/* (this step is redundant given the current value of MaxAllocSize) */
490496
max_pointers = Min(max_pointers, INT_MAX / 2);
491497

492498
dbuckets = ceil(ntuples / NTUP_PER_BUCKET);
493499
dbuckets = Min(dbuckets, max_pointers);
500+
nbuckets = (int) dbuckets;
494501
/* don't let nbuckets be really small, though ... */
495-
nbuckets = Max((int) dbuckets, 1024);
502+
nbuckets = Max(nbuckets, 1024);
496503
/* ... and force it to be a power of 2. */
497504
nbuckets = 1 << my_log2(nbuckets);
498505

0 commit comments

Comments
 (0)