Avoid misbehavior when hash_table_bytes < bucket_size.
authorTom Lane <[email protected]>
Sat, 13 Aug 2022 20:59:58 +0000 (16:59 -0400)
committerTom Lane <[email protected]>
Sat, 13 Aug 2022 21:00:32 +0000 (17:00 -0400)
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

src/backend/executor/nodeHash.c

index 123079c16ccabedb5143782cf8bce29e89293bcc..c48d92259f9725cadba438089772b7dc80d26d74 100644 (file)
@@ -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);