赞
踩
PG10中引入了声明式分区,自此随着各个版本的发布,此项功能逐渐完善。以下功能PG14之前版本已支持:
1) 您可以按照range、list和hash进行分区
2) 添加和合并分区
3) 外键
4) 子分区
5) 在分区上添加索引和约束
6) 分区修剪
缺少的是PG自动创建分区的能力,有了这个patch,一旦提交,hash和list自动分区功能就可以使用。
从list分区开始:看下引入的新语法
- CREATE TABLE tbl_list (i int) PARTITION BY LIST (i)
- CONFIGURATION (values in (1, 2), (3, 4) DEFAULT PARTITION tbl_default);
作为一个例子,可以看到如果像下面一样创建分区表,会自动创建所有分区:
- postgres=# create table tpart_list ( a text primary key, b int, c int )
- partition by list(a)
- configuration (values in ('a'),('b'),('c'),('d') default partition tpart_list_default);
- CREATE TABLE
会自动创建5个分区:a、b、c、d和默认分区:
- postgres=# \d+ tpart_list
- Partitioned table "public.tpart_list"
- Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
- --------+---------+-----------+----------+---------+----------+--------------+-------------
- a | text | | not null | | extended | |
- b | integer | | | | plain | |
- c | integer | | | | plain | |
- Partition key: LIST (a)
- Indexes:
- "tpart_list_pkey" PRIMARY KEY, btree (a)
- Partitions: tpart_list_0 FOR VALUES IN ('a'),
- tpart_list_1 FOR VALUES IN ('b'),
- tpart_list_2 FOR VALUES IN ('c'),
- tpart_list_3 FOR VALUES IN ('d'),
- tpart_list_default DEFAULT
非常好,hash分区表做类似工作,但是语法稍微不一样:
- CREATE TABLE tbl_hash (i int) PARTITION BY HASH (i)
- CONFIGURATION (modulus 3);
思路相同,需要指定configuration,并在进行hash分区时需要提供modulus。
- postgres=# create table tpart_hash ( a int primary key, b text)
- partition by hash (a)
- configuration (modulus 5);
- CREATE TABLE
- postgres=# \d+ tpart_hash
- Partitioned table "public.tpart_hash"
- Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
- --------+---------+-----------+----------+---------+----------+--------------+-------------
- a | integer | | not null | | plain | |
- b | text | | | | extended | |
- Partition key: HASH (a)
- Indexes:
- "tpart_hash_pkey" PRIMARY KEY, btree (a)
- Partitions: tpart_hash_0 FOR VALUES WITH (modulus 5, remainder 0),
- tpart_hash_1 FOR VALUES WITH (modulus 5, remainder 1),
- tpart_hash_2 FOR VALUES WITH (modulus 5, remainder 2),
- tpart_hash_3 FOR VALUES WITH (modulus 5, remainder 3),
- tpart_hash_4 FOR VALUES WITH (modulus 5, remainder 4)

真的很好,工作很棒,感谢所有相关人员,我希望接下来的步骤是:
支持范围分区的自动创建
支持数据进来时动态自动创建分区,这需要一个新的分区。在thread中称为动态分区,实现时称为静态分区。
https://blog.dbi-services.com/postgresql-14-automatic-hash-and-list-partitioning/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。