postgresql10以上的自动分区分表功能

一.列分表

1.首先创建主分区表:

  1.  
    create table fenbiao(
  2.  
    id int,
  3.  
    year varchar
  4.  
    ) partition by list(year)

这里设置的是根据year列进行数据分表;创建后使用navicat是看不到的;

2.创建分表:

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

这样这两天数据会依靠规则插入到不同分表中,如果插入一条不符合规则的数据,则会报错误:no partition of relation "fenbiao" found for row.

二.范围分表

1.以year列为范围进行分表

  1.  
    create table fenbiao2(
  2.  
    id int,
  3.  
    year varchar
  4.  
    ) partition by range(year)

2.创建分表

  1.  
     
  2.  
    create table fenbiao2_2018_2020 partition of fenbiao2 for values from ('2018') to ('2020')
  3.  
     
  4.  
    create table fenbiao2_2020_2030 partition of fenbiao2 for values from ('2020') to ('2030')

注意:此时插入year=2020会插入到下面的表;如下面表范围为2021到2030,则会报错;同时插入2030也会报错;范围相当于时a<=year<b;

原文地址:https://www.cnblogs.com/telwanggs/p/13792402.html