postgres.git / summary / log / commit / refs
commit 4075fc4b97f9b4988250ede0288849bf78cab90c
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun Oct 04 19:55:07 2015 +0000
First released in: 9.6.0, 9.0.23, 9.1.19, 9.2.14, 9.3.10, 9.4.5, 9.5.0
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.)
src/backend/executor/nodeHash.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 5ea353afe38..ea9e88423e0 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -397,6 +397,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;
@@ -464,7 +465,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);
[parent: ff4cbc1ff3d2]