diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-08-13 16:59:58 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-08-13 16:59:58 -0400 |
commit | 4878ea717c7166e3453cb94796a2044837bf1d2b (patch) | |
tree | d0f25115375f52501541d504bb6259f6c063f226 /src/backend/executor/nodeHash.c | |
parent | 60f876317efc7b9ad624b11ae2f4b8208e408ef4 (diff) | |
download | postgresql-4878ea717c7166e3453cb94796a2044837bf1d2b.tar.gz postgresql-4878ea717c7166e3453cb94796a2044837bf1d2b.zip |
Avoid misbehavior when hash_table_bytes < bucket_size.
It's possible to reach this case when work_mem is very small and tupsize
is (relatively) very large. In that case ExecChooseHashTableSize would
get an assertion failure, or with asserts off it'd compute nbuckets = 0,
which'd likely cause misbehavior later (I've not checked). To fix,
clamp the number of buckets to be at least 1.
This is due to faulty conversion of old my_log2() coding in 28d936031.
Back-patch to v13, as that was.
Zhang Mingli
Discussion: https://postgr.es/m/beb64ca0-91e2-44ac-bf4a-7ea36275ec02@Spark
Diffstat (limited to 'src/backend/executor/nodeHash.c')
-rw-r--r-- | src/backend/executor/nodeHash.c | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c index ac274bbe497..f5f6b2a62e4 100644 --- a/src/backend/executor/nodeHash.c +++ b/src/backend/executor/nodeHash.c @@ -832,7 +832,10 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew, * overhead for the hash code, pointer to the next tuple, etc. */ bucket_size = (tupsize * NTUP_PER_BUCKET + sizeof(HashJoinTuple)); - sbuckets = pg_nextpower2_size_t(hash_table_bytes / bucket_size); + if (hash_table_bytes <= bucket_size) + sbuckets = 1; /* avoid pg_nextpower2_size_t(0) */ + else + sbuckets = pg_nextpower2_size_t(hash_table_bytes / bucket_size); sbuckets = Min(sbuckets, max_pointers); nbuckets = (int) sbuckets; nbuckets = pg_nextpower2_32(nbuckets); |