標籤:and http postgre 特性 res pos path check creat
Postgres 10 新特性 分區表
http://francs3.blog.163.com/blog/static/40576727201742103158135/
Postgres 10 之前分區表外掛程式實現 pg_pathman
https://yq.aliyun.com/articles/62314#25
這裡簡單記錄基於觸發器或規則實現
建立分區表
CREATE TABLE RECORD ( --主表
record_id int NOT NULL,
record_pro text NOT NULL,
city_id int,
city_name text
);
CREATE TABLE RECORD_DEFALUT (CHECK(city_id = 0)) INHERITS (RECORD); --DEFAULT
CREATE TABLE RECORD_BJ (CHECK(city_id = 1)) INHERITS (RECORD); --北京
CREATE TABLE RECORD_SH (CHECK(city_id = 2)) INHERITS (RECORD); --上海
CREATE TABLE RECORD_GZ (CHECK(city_id = 3)) INHERITS (RECORD); --廣州
CREATE TABLE RECORD_SZ (CHECK(city_id = 4)) INHERITS (RECORD); --深圳
建立規則
CREATE RULE RECORD_DEFAULT_RULE_INSERT AS
ON INSERT TO RECORD WHERE city_id = 0
DO INSTEAD
INSERT INTO RECORD_DEFALUT VALUES (NEW.*);
CREATE RULE RECORD_BJ_RULE_INSERT AS
ON INSERT TO RECORD WHERE city_id = 1
DO INSTEAD
INSERT INTO RECORD_BJ VALUES (NEW.*);
CREATE RULE RECORD_SH_RULE_INSERT AS
ON INSERT TO RECORD WHERE city_id = 2
DO INSTEAD
INSERT INTO RECORD_SH VALUES (NEW.*);
CREATE RULE RECORD_GZ_RULE_INSERT AS
ON INSERT TO RECORD WHERE city_id = 3
DO INSTEAD
INSERT INTO RECORD_GZ VALUES (NEW.*);
CREATE RULE RECORD_SZ_RULE_INSERT AS
ON INSERT TO RECORD WHERE city_id = 4
DO INSTEAD
INSERT INTO RECORD_SZ VALUES (NEW.*);
輸入類比資料
truncate RECORD;
INSERT INTO RECORD
SELECT generate_series(1, 100),‘recode‘, (int4(random()*10))%5,‘city‘;
INSERT INTO RECORD VALUES(1 , ‘recode‘, 3 , ‘city‘);
INSERT INTO RECORD VALUES(2 , ‘recode‘ , 3 , ‘city‘);
INSERT INTO RECORD VALUES(3 , ‘recode‘ , 0 , ‘city‘);
INSERT INTO RECORD VALUES(4 , ‘recode‘ , 0 , ‘city‘);
INSERT INTO RECORD VALUES(5 , ‘recode‘ , 4 , ‘city‘);
INSERT INTO RECORD VALUES(6 , ‘recode‘ , 3 , ‘city‘);
INSERT INTO RECORD VALUES(7 , ‘recode‘ , 3 , ‘city‘);
INSERT INTO RECORD VALUES(8 , ‘recode‘ , 0 , ‘city‘);
INSERT INTO RECORD VALUES(9 , ‘recode‘ , 0 , ‘city‘);
INSERT INTO RECORD VALUES(10 , ‘recode‘, 2, ‘city‘);
Postgresql 分區表 一