Postgresql 分区表 一
Posted 恩蒙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Postgresql 分区表 一相关的知识,希望对你有一定的参考价值。
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 分区表 一的主要内容,如果未能解决你的问题,请参考以下文章