postgres的hash分表不停機擴容方案
原來我們hash分表之后阵翎,數(shù)據(jù)擴容采用的是rehash院领,這樣遷移全部的數(shù)據(jù)双絮,比較麻煩浴麻。
本次擴容利用hash環(huán)原理,并在此基礎(chǔ)上做一些適應(yīng)性的改動囤攀。
首先假定哈希環(huán)的范圍為0-1023软免,總共1024的數(shù)字,這個可以根據(jù)項目情況擴大或者減小焚挠。
然后假定表"test"中有一個字段"test_col"膏萧,我們根據(jù)這一個字段進行分表,因為hash環(huán)的范圍是0-1023蝌衔,所以hash之后取模將hash的范圍固定在0-1023之間榛泛,如abs(mod(hashtext(test_col::text), 1024)),abs是為了防止數(shù)據(jù)出現(xiàn)負(fù)值胚委。
初始化分表
假設(shè)我們初始化了4張表挟鸠,table_0叉信、table_1亩冬、table_2、table_3,hash取模之后的區(qū)間[0,1)的數(shù)據(jù)存在table_0硅急,[1,2)之間的存在table_1中覆享,[2,3)之間的存在table_2中,[3,1023]之間的存在table_3中营袜,但又因hash取模后的數(shù)是整數(shù)撒顿,因此0->table_0,1->table_1,2->table_2,[3,1023]->table_3荚板,很明顯出現(xiàn)了數(shù)據(jù)不均衡凤壁。
建立虛擬映射
這里我們采用取余的方式建立虛擬映射,即abs(mod(hashtext(test_col::text), 1024))%4跪另,余數(shù)是幾就插入到幾號表中拧抖,hash環(huán)上的table_0-n,table_1-n,table_2-n,table_3-n,就是虛擬映射用的表,每個實體表對應(yīng)了(1024-4)/4張?zhí)摂M表免绿,因為1024是4的整數(shù)倍唧席,所以在實際數(shù)據(jù)插入的時候是可以均勻插入的。
擴容
如上所示嘲驾,我們已經(jīng)可以利用hash環(huán)將我們的數(shù)據(jù)存在指定的分表中了淌哟,但是數(shù)據(jù)量增加的時候,還是有擴容的需求的辽故,所以重點來了徒仓,如下圖所示,我們將4張表擴容成8張表了榕暇,映射關(guān)系變更為abs(mod(hashtext(test_col::text), 1024))%8蓬衡,即原先4%4=0(12,20...彤枢,1020)狰晚,這個表中的數(shù)據(jù)遷移到表4中,原先5%4=1(13缴啡,21...壁晒,1021),中的數(shù)據(jù)遷移到表5中业栅,原先6%4=2(14秒咐,22...,1022)碘裕,中的數(shù)據(jù)遷移到表6中携取,原先7%4=3(15,23...帮孔,1023)雷滋,中的數(shù)據(jù)遷移到表7中不撑,其中有(0,8,...,1016),(1,9,...,1017),(2,10,...,1018),(3,11,...,1019)這些映射上的數(shù)據(jù)是不需要遷移的,因此只需要遷移一半的數(shù)據(jù)即可晤斩。
腳本語句
(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;
$$;
## 觸發(fā)器函數(shù)
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;
$$;
## 觸發(fā)器函數(shù)
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)數(shù)據(jù)轉(zhuǎn)移
如要將表test0中的要轉(zhuǎn)移的數(shù)據(jù)查出來,然后存在新表中澳泵。
SELECT * FROM test0 where (abs(mod(hashtext(test_col::text), 1024))%8)=4
注意點
(1)這里采用的數(shù)據(jù)擴容方式是成倍擴容实愚,而1024剛好符合我們的成倍擴容方式,不會造成數(shù)據(jù)傾斜兔辅。
(2)表數(shù)量是會動態(tài)變更的腊敲,如果服務(wù)需要動態(tài)擴容的話,這個值不應(yīng)該寫死在代碼里面维苔,應(yīng)該支持動態(tài)變更兔仰。
(3)擴容的時候還做不到對整體系統(tǒng)無影響,因此只能選在夜深人靜時蕉鸳。
如果各位有更好的方法乎赴,請多多指教,謝謝潮尝。
如果各位有更好的方法榕吼,請多多指教,謝謝勉失。
如果各位有更好的方法羹蚣,請多多指教,謝謝乱凿。