aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-10-04 15:55:07 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2015-10-04 15:55:07 -0400
commitd84cc40e4bbaa3e708029e0744cba1566ebff324 (patch)
treee5a6f76b7487519c0cb9449bcb388de6501199a4
parenta8168fbcbbb3ed88c19ca2b648d737e8d90f37b1 (diff)
downloadpostgresql-d84cc40e4bbaa3e708029e0744cba1566ebff324.tar.gz
postgresql-d84cc40e4bbaa3e708029e0744cba1566ebff324.zip
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.)
-rw-r--r--src/backend/executor/nodeHash.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 4bd332942d3..d2fae348466 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -398,6 +398,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;
@@ -465,7 +466,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);