天天看点

postgresql 12 分区表(partition table) 中的sequence支持

目的

调查分区表是否支持sequence。即在分区表中,如果引用了sequence的话,是否能够保证每次insert新的record后,sequence自增。

结论

支持

测试环境

centos 7 2004, postgresql 12.4

实验过程

创建sequence

test=# create sequence myseq minvalue 0;
CREATE SEQUENCE
           

创建分区表

test=# create table parttab (w_id integer, seq_field integer default nextval('myseq'), info text) partition by hash (w_id);
CREATE TABLE
           
test=# \d parttab
                   Partitioned table "public.parttab"
  Column   |  Type   | Collation | Nullable |          Default
-----------+---------+-----------+----------+----------------------------
 w_id      | integer |           |          |
 seq_field | integer |           |          | nextval('myseq'::regclass)
 info      | text    |           |          |
Partition key: HASH (w_id)
Number of partitions: 0
           

创建分区表

test=# create table parttab0 partition of parttab  for values WITH (MODULUS 4, REMAINDER 0);
CREATE TABLE
test=# create table parttab1 partition of parttab  for values WITH (MODULUS 4, REMAINDER 1);
CREATE TABLE
test=# create table parttab2 partition of parttab  for values WITH (MODULUS 4, REMAINDER 2);
CREATE TABLE
test=# create table parttab3 partition of parttab  for values WITH (MODULUS 4, REMAINDER 3);
CREATE TABLE
           

插入数据

insert into parttab(w_id, info)
select
s,
'info ' || s
from generate_series(0,50) as s;
           

效果等同:

insert into parttab(w_id, info) values(0, 'info 0');
insert into parttab(w_id, info) values(1, 'info 1');
...
insert into parttab(w_id, info) values(50, 'info 50');
           

查看数据

插入数据是以w_id为序列顺序添加的(0-50),可以看到对应的seq_field也是随w_id递增,说明对于分区表,能保证其中的sequence和非分区表一样正常递增。

test=# select * from parttab order by w_id;
 w_id | seq_field |  info
------+-----------+--------
    0 |         0 | info 0
    1 |         1 | info 1
    2 |         2 | info 2
    3 |         3 | info 3
    4 |         4 | info 4
    5 |         5 | info 5
    6 |         6 | info 6
...
   49 |        49 | info 49
   50 |        50 | info 50
(51 rows)
           

参考资料

https://developer.aliyun.com/article/66946