天天看點

PostgreSQL14:自動hash和list分區?PostgreSQL14:自動hash和list分區?

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/