想要在主表上创建一个自增的唯一建,保证这个字段在全局唯一(包括所有子表)。
而又想用 PostgreSQL 10版本的分区表的功能。
但在创建表的时候,出现如下问题。看起来是 PostgreSQL 10版本不支持在分区表上创建 primary key 或者 unique。想请教一下,什么方法能实现这个需求呢?
这是在分区表上创建 unique
create table test (id bigserial not null,
name varchar(32),
time timestamp(3) not null,
constraint test_unique_id unique(id))
partition by range (time);
ERROR: unique constraints are not supported on partitioned tables
LINE 4: constraint test_unique_id unique(id))
这是在分区表上创建 primary key
create table test (id bigserial primary key,
name varchar(32),
time timestamp(3) not null)
partition by range (time);
ERROR: primary key constraints are not supported on partitioned tables
LINE 1: create table test (id bigserial primary key
实际上数据是按时间存储到各个子表中的,主表没有数据,在创建主表时指定主键 然后子表继承主表
create table parent__table(
id bigserial primary key ,
name varchar(32),
time timestamp(3) not null);
建立子表,继承主表
create table parent_table_2018_05(
check (time>=date '2018-05-01' and time<date '2018-06-01'))
inherits(parent_table);
不支持全局的unique, primary key, exclude, foreign key约束,只能在对应的分区建立这些约束
你需要自己在多个分区表中进行判断来避免违例