Skip to content

Commit 4075fc4

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 ff4cbc1 commit 4075fc4

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/backend/executor/nodeHash.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
397397
long hash_table_bytes;
398398
long skew_table_bytes;
399399
long max_pointers;
400+
long mppow2;
400401
int nbatch;
401402
int nbuckets;
402403
int i;
@@ -464,7 +465,12 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
464465
*/
465466
max_pointers = (work_mem * 1024L) / sizeof(HashJoinTuple);
466467
max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
467-
/* also ensure we avoid integer overflow in nbatch and nbuckets */
468+
/* If max_pointers isn't a power of 2, must round it down to one */
469+
mppow2 = 1L << my_log2(max_pointers);
470+
if (max_pointers != mppow2)
471+
max_pointers = mppow2 / 2;
472+
473+
/* Also ensure we avoid integer overflow in nbatch and nbuckets */
468474
/* (this step is redundant given the current value of MaxAllocSize) */
469475
max_pointers = Min(max_pointers, INT_MAX / 2);
470476

0 commit comments

Comments
 (0)