For sharding I need good hash algorithm. I need to generate hash integer value for string within 0-2^31 range. Is this possible ?
Following answer give a way to generate integer from md5 algorithm.
Hashing a String to a Numeric Value in PostgresSQL
But is there a way to generate this value within a range ?
In the function suggested in Hashing a String to a Numeric Value in PostgresSQL, the 32 bits range is expressed as the width of the
bit(N)
cast.It cannot be passed as a parameter because a type modifier must be constant, but to get a fixed 31 bits, you may just adjust the function like this:
Then the result will be always 31 bits wide and positive, which is the same thing.