PostgreSQL 11 新特性之默認分區(qū)

作者: 不剪發(fā)的Tony老師
畢業(yè)于北京航空航天大學(xué),十多年數(shù)據(jù)庫管理與開發(fā)經(jīng)驗,目前在一家全球性的金融公司從事數(shù)據(jù)庫架構(gòu)設(shè)計。CSDN學(xué)院簽約講師以及GitChat專欄作者。csdn上的博客收藏于以下地址:https://tonydong.blog.csdn.net

文章目錄

PosgtreSQL 11 支持為分區(qū)表創(chuàng)建一個默認(DEFAULT)的分區(qū),用于存儲無法匹配其他任何分區(qū)的數(shù)據(jù)。顯然,只有 RANGE 分區(qū)表和 LIST 分區(qū)表需要默認分區(qū)。

CREATE TABLE measurement (
    city_id         int not null,
    logdate         date not null,
    peaktemp        int,
    unitsales       int
) PARTITION BY RANGE (logdate);

CREATE TABLE measurement_y2018 PARTITION OF measurement
    FOR VALUES FROM ('2018-01-01') TO ('2019-01-01');

以上示例只創(chuàng)建了 2018 年的分區(qū),如果插入 2017 年的數(shù)據(jù),系統(tǒng)將會無法找到相應(yīng)的分區(qū):

INSERT INTO measurement(city_id,logdate,peaktemp,unitsales)
VALUES (1, '2017-10-01', 50, 200);
ERROR:  no partition of relation "measurement" found for row
DETAIL:  Partition key of the failing row contains (logdate) = (2017-10-01).

使用默認分區(qū)可以解決這類問題。創(chuàng)建默認分區(qū)時使用 DEFAULT 子句替代 FOR VALUES 子句。

CREATE TABLE measurement_default PARTITION OF measurement DEFAULT;
\d+ measurement
                                 Table "public.measurement"
  Column   |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
-----------+---------+-----------+----------+---------+---------+--------------+-------------
 city_id   | integer |           | not null |         | plain   |              |
 logdate   | date    |           | not null |         | plain   |              |
 peaktemp  | integer |           |          |         | plain   |              |
 unitsales | integer |           |          |         | plain   |              |
Partition key: RANGE (logdate)
Partitions: measurement_y2018 FOR VALUES FROM ('2018-01-01') TO ('2019-01-01'),
            measurement_default DEFAULT

有了默認分區(qū)之后,未定義分區(qū)的數(shù)據(jù)將會插入到默認分區(qū)中:

INSERT INTO measurement(city_id,logdate,peaktemp,unitsales)
VALUES (1, '2017-10-01', 50, 200);
INSERT 0 1

select * from measurement_default;
 city_id |  logdate   | peaktemp | unitsales
---------+------------+----------+-----------
       1 | 2017-10-01 |       50 |       200
(1 row)

默認分區(qū)存在以下限制:

    一個分區(qū)表只能擁有一個 DEFAULT 分區(qū);
    對于已經(jīng)存儲在 DEFAULT 分區(qū)中的數(shù)據(jù),不能再創(chuàng)建相應(yīng)的分區(qū);參見下文示例;
    如果將已有的表掛載為 DEFAULT 分區(qū),將會檢查該表中的所有數(shù)據(jù);如果在已有的分區(qū)中存在相同的數(shù)據(jù),將會產(chǎn)生一個錯誤;
    哈希分區(qū)表不支持 DEFAULT 分區(qū),實際上也不需要支持。

使用默認分區(qū)也可能導(dǎo)致一些不可預(yù)見的問題。例如,往 measurement 表中插入一條 2019 年的數(shù)據(jù),由于沒有創(chuàng)建相應(yīng)的分區(qū),該記錄同樣會分配到默認分區(qū):

INSERT INTO measurement(city_id,logdate,peaktemp,unitsales)
VALUES (1, '2019-03-25', 66, 100);
INSERT 0 1

select * from measurement_default;
 city_id |  logdate   | peaktemp | unitsales
---------+------------+----------+-----------
       1 | 2017-10-01 |       50 |       200
       1 | 2019-03-25 |       66 |       100
(2 rows)

此時,如果再創(chuàng)建 2019 年的分區(qū),操作將會失敗。因為添加新的分區(qū)需要修改默認分區(qū)的范圍(不再包含 2019 年的數(shù)據(jù)),但是默認分區(qū)中已經(jīng)存在 2019 年的數(shù)據(jù)。

CREATE TABLE measurement_y2019 PARTITION OF measurement
    FOR VALUES FROM ('2019-01-01') TO ('2020-01-01');
ERROR:  updated partition constraint for default partition "measurement_default" would be violated by some row

為了解決這個問題,可以先將默認分區(qū)從分區(qū)表中卸載(DETACH PARTITION),創(chuàng)建新的分區(qū),將默認分區(qū)中的相應(yīng)的數(shù)據(jù)移動到新的分區(qū),最后重新掛載默認分區(qū)。

ALTER TABLE measurement DETACH PARTITION measurement_default;

CREATE TABLE measurement_y2019 PARTITION OF measurement
    FOR VALUES FROM ('2019-01-01') TO ('2020-01-01');

INSERT INTO measurement_y2019
SELECT * FROM measurement_default WHERE logdate >= '2019-01-01' AND logdate < '2020-01-01';
INSERT 0 1

DELETE FROM measurement_default WHERE logdate >= '2019-01-01' AND logdate < '2020-01-01';
DELETE 1

ALTER TABLE measurement ATTACH PARTITION measurement_default DEFAULT;

CREATE TABLE measurement_y2020 PARTITION OF measurement
    FOR VALUES FROM ('2020-01-01') TO ('2021-01-01');

\d+ measurement
                                 Table "public.measurement"
  Column   |  Type   | Collation | Nullable | Default | Storage | Stats target | Description
-----------+---------+-----------+----------+---------+---------+--------------+-------------
 city_id   | integer |           | not null |         | plain   |              |
 logdate   | date    |           | not null |         | plain   |              |
 peaktemp  | integer |           |          |         | plain   |              |
 unitsales | integer |           |          |         | plain   |              |
Partition key: RANGE (logdate)
Partitions: measurement_y2018 FOR VALUES FROM ('2018-01-01') TO ('2019-01-01'),
            measurement_y2019 FOR VALUES FROM ('2019-01-01') TO ('2020-01-01'),
            measurement_y2020 FOR VALUES FROM ('2020-01-01') TO ('2021-01-01'),
            measurement_default DEFAULT


官方文檔:Table Partitioning