Fix bug in calculations of hash join buckets.
authorKevin Grittner <kgrittn@postgresql.org>
Wed, 19 Aug 2015 13:20:55 +0000 (08:20 -0500)
committerKevin Grittner <kgrittn@postgresql.org>
Wed, 19 Aug 2015 13:20:55 +0000 (08:20 -0500)
Commit 8cce08f168481c5fc5be4e7e29b968e314f1b41e used a left-shift
on a literal of 1 that could (in large allocations) be shifted by
31 or more bits.  This was assigned to a local variable that was
already declared to be a long to protect against overruns of int,
but the literal in this shift needs to be declared long to allow it
to work correctly in some compilers.

Backpatch to 9.5, where the bug was introduced.

Report and patch by KaiGai Kohei, slighly modified based on
discussion.

src/backend/executor/nodeHash.c

index ee9298a157f564c6f52b3bc3afdb1ccc224d93be..0b2c13917eec2b055df3df8ba57676ba63ab445b 100644 (file)
@@ -518,7 +518,7 @@ 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));
-       lbuckets = 1 << my_log2(hash_table_bytes / bucket_size);
+       lbuckets = 1L << my_log2(hash_table_bytes / bucket_size);
        lbuckets = Min(lbuckets, max_pointers);
        nbuckets = (int) lbuckets;
        bucket_bytes = nbuckets * sizeof(HashJoinTuple);