1. 程式人生 > >postgresql10以上的自動分割槽分表功能

postgresql10以上的自動分割槽分表功能

一.列分表

1.首先建立主分割槽表:

create table fenbiao(
id int,
year varchar
) partition by list(year)

這裡設定的是根據year列進行資料分表;建立後使用navicat是看不到的;

2.建立分表:

create table fenbiao_2017 partition of fenbiao for values in ('2017')
create table fenbiao_2018 partition of fenbiao for values in ('2018')

這樣這兩天資料會依靠規則插入到不同分表中,如果插入一條不符合規則的資料,則會報錯誤:no partition of relation "fenbiao" found for row.

 

二.範圍分表

1.以year列為範圍進行分表

create table fenbiao2(
id int,
year varchar
) partition by range(year)

2.建立分表


create table fenbiao2_2018_2020 partition of fenbiao2 for values from ('2018') to ('2020')

create table fenbiao2_2020_2030 partition of fenbiao2 for values from ('2020') to ('2030')

注意:此時插入year=2020會插入到下面的表;如下面表範圍為2021到2030,則會報錯;同時插入2030也會報錯;範圍相當於時a<=year<b;