創建一個father表的子表
CREATE TABLE test_1 () INHERITS (father_table);
分表之後第一件事情當然就是把原有父表的數據分發到各個子表中
首先創建原有數據對應的子表
DO $$
DECLARE
father RECORD;
BEGIN
FOR father IN SELECT item_type FROM father_table GROUP BY item_type LOOP
EXECUTE format('CREATE TABLE test_$%s () INHERITS (father_table);', father_table.item_type);
END LOOP;
END; $$
這時各個分表是空的,開始往各個分表分發數據
DO $$
DECLARE
father RECORD;
BEGIN
FOR father IN SELECT item_type FROM father_table GROUP BY item_type LOOP
EXECUTE format('INSERT INTO test_$%s SELECT * FROM father_table WHERE father_table.item_type = %s);', father_table.item_type);
END LOOP;
END; $$
然後再清除調父表的數據就ok了
DELETE FROM ONLY father_table;