我想手动计算提醒,以便在分区表中使用upsert。 我尝试了以下代码:
create table test_partitioning
(
test bigint not null
) partition by hash (test);
DO $$
declare
_remainder integer ;
begin
FOR _remainder IN (select generate_series(1, 10)) LOOP
EXECUTE 'CREATE TABLE IF NOT EXISTS test_partitioning_' || _remainder ||
' PARTITION OF test_partitioning FOR VALUES WITH (modulus 10, remainder ' || (_remainder - 1) || ')';
END LOOP;
END $$;
do
$$
declare
value bigint ;
calculated_reminder int;
begin
FOR value IN (select generate_series(1, 10)) LOOP
calculated_reminder = (((hashint8extended(value, 8816678312871386365)::numeric + 5305509591434766563) % 10)::int + 10) % 10;
if not satisfies_hash_partition('test_partitioning'::regclass, 10, calculated_reminder, value) THEN
RAISE 'check failed, value=%, reminder=%', value, calculated_reminder;
end if;
end LOOP ;
end
$$;
在值 2 和 3 上失败。 我的计算有什么问题?
这里是解决方案:
CREATE OR REPLACE FUNCTION unsigned2signed(signed bigint)
RETURNS numeric AS $$
SELECT CASE
WHEN signed >= 0 THEN signed::numeric
ELSE 18446744073709551616 + signed
END
$$ LANGUAGE SQL;
calculated_reminder = ((unsigned2signed(hashint8extended(value, 8816678312871386365)) + 5305509591434766563) % 18446744073709551616) % 10;
顺便说一句,原始解决方案在分区数 = x^2 (2, 4, 8, 16, ...) 的测试中也有效