Skip to content

Commit 7149b12

Browse files
committed
Improve hash_array() logic for combining hash values.
The new logic is less vulnerable to transpositions. This invalidates the contents of hash indexes built with the old functions; hence, bump catversion. Dean Rasheed
1 parent c58b945 commit 7149b12

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/backend/utils/adt/arrayfuncs.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,7 +3533,7 @@ hash_array(PG_FUNCTION_ARGS)
35333533
int ndims = ARR_NDIM(array);
35343534
int *dims = ARR_DIMS(array);
35353535
Oid element_type = ARR_ELEMTYPE(array);
3536-
uint32 result = 0;
3536+
uint32 result = 1;
35373537
int nitems;
35383538
TypeCacheEntry *typentry;
35393539
int typlen;
@@ -3617,11 +3617,17 @@ hash_array(PG_FUNCTION_ARGS)
36173617
}
36183618

36193619
/*
3620-
* Combine hash values of successive elements by rotating the previous
3621-
* value left 1 bit, then XOR'ing in the new element's hash value.
3620+
* Combine hash values of successive elements by multiplying the
3621+
* current value by 31 and adding on the new element's hash value.
3622+
*
3623+
* The result is a sum in which each element's hash value is
3624+
* multiplied by a different power of 31. This is modulo 2^32
3625+
* arithmetic, and the powers of 31 modulo 2^32 form a cyclic group of
3626+
* order 2^27. So for arrays of up to 2^27 elements, each element's
3627+
* hash value is multiplied by a different (odd) number, resulting in
3628+
* a good mixing of all the elements' hash values.
36223629
*/
3623-
result = (result << 1) | (result >> 31);
3624-
result ^= elthash;
3630+
result = (result << 5) - result + elthash;
36253631
}
36263632

36273633
/* Avoid leaking memory when handed toasted input. */

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201105131
56+
#define CATALOG_VERSION_NO 201105231
5757

5858
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy