postgresql partition分表的总结
Posted Agent-C
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postgresql partition分表的总结相关的知识,希望对你有一定的参考价值。
postgre分表有两种方式
10版本以前仅继承分表
10版本以后支持内置分区分表
内置分表
主表创建的索引会传递到子表
内置分表有三种情况
1.hash分表
-- 1.创建主表
drop table if exists dept;
create table dept (id int primary key) partition by hash(id) ;
--2.创建子表
create table dept_1 partition of dept FOR VALUES WITH (MODULUS 5, REMAINDER 0);
create table dept_2 partition of dept FOR VALUES WITH (MODULUS 5, REMAINDER 1);
create table dept_3 partition of dept FOR VALUES WITH (MODULUS 5, REMAINDER 2);
create table dept_4 partition of dept FOR VALUES WITH (MODULUS 5, REMAINDER 3);
create table dept_5 partition of dept FOR VALUES WITH (MODULUS 5, REMAINDER 4);
--3.插入数据
insert into dept (select generate_series(0,200000));
2.range分表
drop table if exists list_parted;
CREATE TABLE list_parted ( a int ) PARTITION BY range (a);
CREATE TABLE part_1 PARTITION OF list_parted FOR VALUES from (1) to (10);
CREATE TABLE part_2 PARTITION OF list_parted FOR VALUES from (10) to (20);
CREATE TABLE part_3 PARTITION OF list_parted FOR VALUES from (20) to (30);
CREATE TABLE part_4 PARTITION OF list_parted FOR VALUES from (30) to (40);
CREATE TABLE part_5 PARTITION OF list_parted FOR VALUES from (40) to (50);
3.list分表
方式2-list分表
drop table if exists list_parted;
CREATE TABLE list_parted ( a int ) PARTITION BY LIST (a);
CREATE TABLE part_1 PARTITION OF list_parted FOR VALUES IN (1);
CREATE TABLE part_2 PARTITION OF list_parted FOR VALUES IN (2);
CREATE TABLE part_3 PARTITION OF list_parted FOR VALUES IN (3);
CREATE TABLE part_4 PARTITION OF list_parted FOR VALUES IN (4);
CREATE TABLE part_5 PARTITION OF list_parted FOR VALUES IN (5);
以上是关于postgresql partition分表的总结的主要内容,如果未能解决你的问题,请参考以下文章
PostgreSQL分区表(partitioning)应用实例
cannot perform an INSERT without a partition column value