PostgreSQL14:自动hash和list分区?
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/
文章转载自yanzongshuaiDBA,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
PostgreSQL14:自动hash和list分区?
3月前

评论
相关阅读
明明想执行的SQL是DELETE、UPDATE,但为什么看到的是SELECT(FDW的实现原理解析)
小满未满、
402次阅读
2025-03-19 23:11:26
PostgreSQL初/中/高级认证考试(3.15)通过考生公示
开源软件联盟PostgreSQL分会
399次阅读
2025-03-20 09:50:36
9.9 分高危漏洞,尽快升级到 pgAdmin 4 v9.2 进行修复
严少安
305次阅读
2025-04-11 10:43:23
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
286次阅读
2025-04-15 14:48:05
openHalo问世,全球首款基于PostgreSQL兼容MySQL协议的国产开源数据库
严少安
243次阅读
2025-04-07 12:14:29
IvorySQL 4.4 发布 - 基于 PostgreSQL 17.4,增强平台支持
通讯员
233次阅读
2025-03-20 15:31:04
PG vs MySQL 执行计划解读的异同点
进击的CJR
167次阅读
2025-03-21 10:50:08
PostgreSQL分区管理扩展——pg_partman
chirpyli
137次阅读
2025-03-19 15:48:31
postgresql+patroni+etcd高可用安装
necessary
128次阅读
2025-03-28 10:11:23
手把手教你在 openKylin 上部署 IvorySQL 4.4
严少安
127次阅读
2025-03-27 20:41:28