当前位置:   article > 正文

PostgreSQL14:自动hash和list分区?_postgres auto range list

postgres auto range list

PostgreSQL14:自动hash和list分区?

PG10中引入了声明式分区,自此随着各个版本的发布,此项功能逐渐完善。以下功能PG14之前版本已支持:

1) 您可以按照range、list和hash进行分区

2) 添加和合并分区

3) 外键

4) 子分区

5) 在分区上添加索引和约束

6) 分区修剪

缺少的是PG自动创建分区的能力,有了这个patch,一旦提交,hash和list自动分区功能就可以使用。

从list分区开始:看下引入的新语法

  1. CREATE TABLE tbl_list (i intPARTITION BY LIST (i)
  2. CONFIGURATION (values in (1, 2), (3, 4) DEFAULT PARTITION tbl_default);

作为一个例子,可以看到如果像下面一样创建分区表,会自动创建所有分区:

  1. postgres=create table tpart_list ( a text primary key, b int, c int )
  2.            partition by list(a)
  3.            configuration (values in ('a'),('b'),('c'),('d'default partition tpart_list_default);
  4. CREATE TABLE

会自动创建5个分区:a、b、c、d和默认分区:

  1. postgres=# \d+ tpart_list
  2. Partitioned table "public.tpart_list"
  3. Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
  4. --------+---------+-----------+----------+---------+----------+--------------+-------------
  5. a | text | | not null | | extended | |
  6. b | integer | | | | plain | |
  7. c | integer | | | | plain | |
  8. Partition key: LIST (a)
  9. Indexes:
  10. "tpart_list_pkey" PRIMARY KEY, btree (a)
  11. Partitions: tpart_list_0 FOR VALUES IN ('a'),
  12. tpart_list_1 FOR VALUES IN ('b'),
  13. tpart_list_2 FOR VALUES IN ('c'),
  14. tpart_list_3 FOR VALUES IN ('d'),
  15.             tpart_list_default DEFAULT

非常好,hash分区表做类似工作,但是语法稍微不一样:

  1. CREATE TABLE tbl_hash (i intPARTITION BY HASH (i)
  2. CONFIGURATION (modulus 3);

思路相同,需要指定configuration,并在进行hash分区时需要提供modulus。

  1. postgres=# create table tpart_hash ( a int primary key, b text)
  2. partition by hash (a)
  3. configuration (modulus 5);
  4. CREATE TABLE
  5. postgres=# \d+ tpart_hash
  6. Partitioned table "public.tpart_hash"
  7. Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
  8. --------+---------+-----------+----------+---------+----------+--------------+-------------
  9. a | integer | | not null | | plain | |
  10. b | text | | | | extended | |
  11. Partition key: HASH (a)
  12. Indexes:
  13. "tpart_hash_pkey" PRIMARY KEY, btree (a)
  14. Partitions: tpart_hash_0 FOR VALUES WITH (modulus 5, remainder 0),
  15. tpart_hash_1 FOR VALUES WITH (modulus 5, remainder 1),
  16. tpart_hash_2 FOR VALUES WITH (modulus 5, remainder 2),
  17. tpart_hash_3 FOR VALUES WITH (modulus 5, remainder 3),
  18. tpart_hash_4 FOR VALUES WITH (modulus 5, remainder 4)

真的很好,工作很棒,感谢所有相关人员,我希望接下来的步骤是:

支持范围分区的自动创建

支持数据进来时动态自动创建分区,这需要一个新的分区。在thread中称为动态分区,实现时称为静态分区。

原文

https://blog.dbi-services.com/postgresql-14-automatic-hash-and-list-partitioning/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/879018
推荐阅读
相关标签
  

闽ICP备14008679号