前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Postgresql10分区表range实例

Postgresql10分区表range实例

作者头像
mingjie
发布2022-05-12 11:06:06
3140
发布2022-05-12 11:06:06
举报
文章被收录于专栏:Postgresql源码分析

注意点

代码语言:javascript
复制
1. PG10不能在父表上创建索引
2. 极限优化索引可以创建条件索引
3. 序列一定是要挂在父表上

创建规则 https://www.postgresql.org/docs/10/sql-createtable.html

代码语言:javascript
复制
PARTITION BY { RANGE | LIST } ( { column_name | ( expression ) } [ opclass ] [, ...] )
The optional PARTITION BY clause specifies a strategy of partitioning the table. The table thus created is called a partitioned table. The parenthesized list of columns or expressions forms the partition key for the table. When using range partitioning, the partition key can include multiple columns or expressions (up to 32, but this limit can be altered when building PostgreSQL), but for list partitioning, the partition key must consist of a single column or expression. If no B-tree operator class is specified when creating a partitioned table, the default B-tree operator class for the datatype will be used. If there is none, an error will be reported.

A partitioned table is divided into sub-tables (called partitions), which are created using separate CREATE TABLE commands. The partitioned table is itself empty. A data row inserted into the table is routed to a partition based on the value of columns or expressions in the partition key. If no existing partition matches the values in the new row, an error will be reported.

Partitioned tables do not support UNIQUE, PRIMARY KEY, EXCLUDE, or FOREIGN KEY constraints; however, you can define these constraints on individual partitions.

参考

代码语言:javascript
复制
create table t__root(id int, info text) partition by range(info);
create table t_0 partition of t__root for values from ('0') to ('1');
create table t_1 partition of t__root for values from ('1') to ('2');
create table t_2 partition of t__root for values from ('2') to ('3');
create table t_3 partition of t__root for values from ('3') to ('4');

alter table t_0 add constraint idx_t_0_id_pkey primary key(id);
alter table t_1 add constraint idx_t_1_id_pkey primary key(id);
alter table t_2 add constraint idx_t_2_id_pkey primary key(id);
alter table t_3 add constraint idx_t_3_id_pkey primary key(id);

alter table t_0 add constraint idx_t_0_id_unique unique(id);
alter table t_1 add constraint idx_t_1_id_unique unique(id);
alter table t_2 add constraint idx_t_2_id_unique unique(id);
alter table t_3 add constraint idx_t_3_id_unique unique(id);


CREATE SEQUENCE t__root_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
alter table t__root alter column id set default nextval('t__root_id_seq');
select setval('t__root_id_seq', 10000, true);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档