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)將會無法找到相應的分區(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ū)表只能擁有一個 DEFAULT 分區(qū);
對于已經存儲在 DEFAULT 分區(qū)中的數(shù)據(jù),不能再創(chuàng)建相應的分區(qū);參見下文示例;
如果將已有的表掛載為 DEFAULT 分區(qū),將會檢查該表中的所有數(shù)據(jù);如果在已有的分區(qū)中存在相同的數(shù)據(jù),將會產生一個錯誤;
哈希分區(qū)表不支持 DEFAULT 分區(qū),實際上也不需要支持。
使用默認分區(qū)也可能導致一些不可預見的問題。例如,往 measurement 表中插入一條 2019 年的數(shù)據(jù),由于沒有創(chuà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ū)中已經存在 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ū)中的相應的數(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
補充:postgresql10以上的自動分區(qū)分表功能
1.首先創(chuàng)建主分區(qū)表:
create table fenbiao( id int, year varchar ) partition by list(year)
這里設置的是根據(jù)year列進行數(shù)據(jù)分表;創(chuàng)建后使用navicat是看不到的;
2.創(chuàng)建分表:
create table fenbiao_2017 partition of fenbiao for values in ('2017')
create table fenbiao_2018 partition of fenbiao for values in ('2018')
這樣這兩天數(shù)據(jù)會依靠規(guī)則插入到不同分表中,如果插入一條不符合規(guī)則的數(shù)據(jù),則會報錯誤:no partition of relation "fenbiao" found for row.
1.以year列為范圍進行分表
create table fenbiao2( id int, year varchar ) partition by range(year)
2.創(chuàng)建分表
create table fenbiao2_2018_2020 partition of fenbiao2 for values from ('2018') to ('2020')
create table fenbiao2_2020_2030 partition of fenbiao2 for values from ('2020') to ('2030')
注意:此時插入year=2020會插入到下面的表;如下面表范圍為2021到2030,則會報錯;同時插入2030也會報錯;范圍相當于時a=yearb;
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。