diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c index 62724325c4b8504fa2a890f8fc3e84f611b18eb7..b10a58b42488925f718b044f13c66311d0ddea95 100644 --- a/src/backend/executor/nodeHash.c +++ b/src/backend/executor/nodeHash.c @@ -396,6 +396,7 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, long hash_table_bytes; long skew_table_bytes; long max_pointers; + long mppow2; int nbatch; int nbuckets; int i; @@ -463,7 +464,12 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, */ max_pointers = (work_mem * 1024L) / sizeof(HashJoinTuple); max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple)); - /* also ensure we avoid integer overflow in nbatch and nbuckets */ + /* If max_pointers isn't a power of 2, must round it down to one */ + mppow2 = 1L << my_log2(max_pointers); + if (max_pointers != mppow2) + max_pointers = mppow2 / 2; + + /* Also ensure we avoid integer overflow in nbatch and nbuckets */ /* (this step is redundant given the current value of MaxAllocSize) */ max_pointers = Min(max_pointers, INT_MAX / 2);