79554907

Date: 2025-04-04 09:13:55
Score: 1
Natty:
Report link

Thanks @Laurenz Albe, I think I've managed to handle the negative values LHJ was experiencing (and me too). Wrapped it up in a function to take into account the unsigned-ness. Tested locally on PG16 and seems to work, with the warning that this is only for hashing on tables partitioned by a text column.

CREATE OR REPLACE FUNCTION compute_text_hash_partition(val TEXT, partition_count INT)
RETURNS INT AS $$
DECLARE
    seed CONSTANT BIGINT := 8816678312871386365;
    hash_offset CONSTANT BIGINT := 5305509591434766563;
    max_uint64 CONSTANT NUMERIC := 18446744073709551616;
    hashval NUMERIC;
BEGIN
    hashval := (hashtextextended(val, seed)::numeric + hash_offset + max_uint64) % max_uint64;
    RETURN MOD(hashval, partition_count);
END;
$$ LANGUAGE plpgsql IMMUTABLE;
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Looks like a comment (1):
  • Low reputation (0.5):
Posted by: LTMullineux