postgres的hash分表不停機擴容方案
原來我們hash分表之后,數據擴容采用的是rehash,這樣遷移全部的數據,比較麻煩。
本次擴容利用hash環原理,并在此基礎上做一些適應性的改動。
首先假定哈希環的范圍為0-1023,總共1024的數字,這個可以根據項目情況擴大或者減小。
然后假定表"test"中有一個字段"test_col",我們根據這一個字段進行分表,因為hash環的范圍是0-1023,所以hash之后取模將hash的范圍固定在0-1023之間,如abs(mod(hashtext(test_col::text), 1024)),abs是為了防止數據出現負值。
初始化分表
數據不均衡.JPG
假設我們初始化了4張表,table_0、table_1、table_2、table_3,hash取模之后的區間[0,1)的數據存在table_0,[1,2)之間的存在table_1中,[2,3)之間的存在table_2中,[3,1023]之間的存在table_3中,但又因hash取模后的數是整數,因此0->table_0,1->table_1,2->table_2,[3,1023]->table_3,很明顯出現了數據不均衡。
建立虛擬映射
初始化.JPG
這里我們采用取余的方式建立虛擬映射,即abs(mod(hashtext(test_col::text), 1024))%4,余數是幾就插入到幾號表中,hash環上的table_0-n,table_1-n,table_2-n,table_3-n,就是虛擬映射用的表,每個實體表對應了(1024-4)/4張虛擬表,因為1024是4的整數倍,所以在實際數據插入的時候是可以均勻插入的。
擴容
如上所示,我們已經可以利用hash環將我們的數據存在指定的分表中了,但是數據量增加的時候,還是有擴容的需求的,所以重點來了,如下圖所示,我們將4張表擴容成8張表了,映射關系變更為abs(mod(hashtext(test_col::text), 1024))%8,即原先4%4=0(12,20...,1020),這個表中的數據遷移到表4中,原先5%4=1(13,21...,1021),中的數據遷移到表5中,原先6%4=2(14,22...,1022),中的數據遷移到表6中,原先7%4=3(15,23...,1023),中的數據遷移到表7中,其中有(0,8,...,1016),(1,9,...,1017),(2,10,...,1018),(3,11,...,1019)這些映射上的數據是不需要遷移的,因此只需要遷移一半的數據即可。
擴容.JPG
腳本語句
(1)初始化分表語句
## 分表語句
do language plpgsql
$$
declare
parts int := 4;
begin
for i in 0..parts-1 loop
execute format('create table test%s (like test including all) inherits (test)', i);
execute format('alter table test%s add constraint ck check((abs(mod(hashtext(test_col),1024))%%4)=%s)', i, i);
end loop;
end;
$$;
## 觸發器函數
create or replace function ins_test() returns trigger as
$$
declare begin
case (abs(mod(hashtext(NEW.test_col),1024))%4)
when 0 then
insert into test0 values (NEW.*);
when 1 then
insert into test1 values (NEW.*);
when 2 then
insert into test2 values (NEW.*);
when 3 then
insert into test3 values (NEW.*);
else
return NEW;
end case;
return null;
end;
$$
language plpgsql strict;
## 為空保護
create trigger test_ins_tg before insert on test for each row when (NEW.test_col is not null) execute procedure ins_test();
## 查詢時拼接
and (abs(mod(hashtext(test_col::text), 1024))%4)=(abs(mod(hashtext(#{testCol}::text), 1024))%4)
(2)擴容腳本-1
do language plpgsql
$$
declare
parts int := 8;
begin
for i in 4..parts-1 loop
execute format('create table test%s (like test including all) inherits (test)', i);
execute format('alter table test%s add constraint ck check((abs(mod(hashtext(test_col),1024))%%8)=%s)', i, i);
end loop;
end;
$$;
## 觸發器函數
create or replace function ins_test() returns trigger as
$$
declare begin
case (abs(mod(hashtext(NEW.test_col),1024))%8)
when 0 then
insert into test0 values (NEW.*);
when 1 then
insert into test1 values (NEW.*);
when 2 then
insert into test2 values (NEW.*);
when 3 then
insert into test3 values (NEW.*);
when 4 then
insert into test0 values (NEW.*);
when 5 then
insert into test1 values (NEW.*);
when 6 then
insert into test2 values (NEW.*);
when 7 then
insert into test3 values (NEW.*);
else
return NEW;
end case;
return null;
end;
$$
language plpgsql strict;
## 查詢時拼接
and (abs(mod(hashtext(test_col::text), 1024))%8)=(abs(mod(hashtext(#{testCol}::text), 1024))%8)
(3)擴容腳本-2
因為原先的分表中constraint定義的是%4,這里需要改成%8
alter table test0 add constraint ck check((abs(mod(hashtext(test_col),1024))%%8)=0)
alter table test0 add constraint ck check((abs(mod(hashtext(test_col),1024))%%8)=1)
alter table test0 add constraint ck check((abs(mod(hashtext(test_col),1024))%%8)=2)
alter table test0 add constraint ck check((abs(mod(hashtext(test_col),1024))%%8)=3)
(4)數據轉移
如要將表test0中的要轉移的數據查出來,然后存在新表中。
SELECT * FROM test0 where (abs(mod(hashtext(test_col::text), 1024))%8)=4
注意點
(1)這里采用的數據擴容方式是成倍擴容,而1024剛好符合我們的成倍擴容方式,不會造成數據傾斜。
(2)表數量是會動態變更的,如果服務需要動態擴容的話,這個值不應該寫死在代碼里面,應該支持動態變更。
(3)擴容的時候還做不到對整體系統無影響,因此只能選在夜深人靜時。
如果各位有更好的方法,請多多指教,謝謝。
如果各位有更好的方法,請多多指教,謝謝。
如果各位有更好的方法,請多多指教,謝謝。