aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/nodeHash.c
diff options
context:
space:
mode:
authorAlexander Korotkov <akorotkov@postgresql.org>2024-01-07 09:03:55 +0200
committerAlexander Korotkov <akorotkov@postgresql.org>2024-01-07 09:18:43 +0200
commit72d5b27763a86c1d9af79e3169aef885b79373a3 (patch)
tree099300b424a591f7b9c08e41ac126b22d5aa7976 /src/backend/executor/nodeHash.c
parent49fa18390cfbd03956a4820910777c3d6585d5a7 (diff)
downloadpostgresql-72d5b27763a86c1d9af79e3169aef885b79373a3.tar.gz
postgresql-72d5b27763a86c1d9af79e3169aef885b79373a3.zip
Fix oversized memory allocation in Parallel Hash Join
During the calculations of the maximum for the number of buckets, take into account that later we round that to the next power of 2. Reported-by: Karen Talarico Bug: #16925 Discussion: https://postgr.es/m/16925-ec96d83529d0d629%40postgresql.org Author: Thomas Munro, Andrei Lepikhov, Alexander Korotkov Reviewed-by: Alena Rybakina Backpatch-through: 12
Diffstat (limited to 'src/backend/executor/nodeHash.c')
-rw-r--r--src/backend/executor/nodeHash.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 462e81c7bc5..182aa9a216c 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -1141,6 +1141,7 @@ ExecParallelHashIncreaseNumBatches(HashJoinTable hashtable)
double dtuples;
double dbuckets;
int new_nbuckets;
+ uint32 max_buckets;
/*
* We probably also need a smaller bucket array. How many
@@ -1153,9 +1154,17 @@ ExecParallelHashIncreaseNumBatches(HashJoinTable hashtable)
* array.
*/
dtuples = (old_batch0->ntuples * 2.0) / new_nbatch;
+ /*
+ * We need to calculate the maximum number of buckets to
+ * stay within the MaxAllocSize boundary. Round the
+ * maximum number to the previous power of 2 given that
+ * later we round the number to the next power of 2.
+ */
+ max_buckets = MaxAllocSize / sizeof(dsa_pointer_atomic);
+ if ((max_buckets & (max_buckets - 1)) != 0)
+ max_buckets = 1 << (my_log2(max_buckets) - 1);
dbuckets = ceil(dtuples / NTUP_PER_BUCKET);
- dbuckets = Min(dbuckets,
- MaxAllocSize / sizeof(dsa_pointer_atomic));
+ dbuckets = Min(dbuckets, max_buckets);
new_nbuckets = (int) dbuckets;
new_nbuckets = Max(new_nbuckets, 1024);
new_nbuckets = 1 << my_log2(new_nbuckets);