主頁(yè) > 知識(shí)庫(kù) > 淺談PostgreSQL 11 新特性之默認(rèn)分區(qū)

淺談PostgreSQL 11 新特性之默認(rèn)分區(qū)

熱門(mén)標(biāo)簽:Linux服務(wù)器 呼叫中心市場(chǎng)需求 鐵路電話系統(tǒng) 網(wǎng)站排名優(yōu)化 百度競(jìng)價(jià)排名 服務(wù)外包 地方門(mén)戶(hù)網(wǎng)站 AI電銷(xiāo)

文章目錄

PosgtreSQL 11 支持為分區(qū)表創(chuàng)建一個(gè)默認(rèn)(DEFAULT)的分區(qū),用于存儲(chǔ)無(wú)法匹配其他任何分區(qū)的數(shù)據(jù)。顯然,只有 RANGE 分區(qū)表和 LIST 分區(qū)表需要默認(rèn)分區(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)將會(huì)無(wú)法找到相應(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).

使用默認(rèn)分區(qū)可以解決這類(lèi)問(wèn)題。創(chuàng)建默認(rèn)分區(qū)時(shí)使用 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

有了默認(rèn)分區(qū)之后,未定義分區(qū)的數(shù)據(jù)將會(huì)插入到默認(rèn)分區(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)

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

一個(gè)分區(qū)表只能擁有一個(gè) DEFAULT 分區(qū);

對(duì)于已經(jīng)存儲(chǔ)在 DEFAULT 分區(qū)中的數(shù)據(jù),不能再創(chuàng)建相應(yīng)的分區(qū);參見(jiàn)下文示例;

如果將已有的表掛載為 DEFAULT 分區(qū),將會(huì)檢查該表中的所有數(shù)據(jù);如果在已有的分區(qū)中存在相同的數(shù)據(jù),將會(huì)產(chǎn)生一個(gè)錯(cuò)誤;

哈希分區(qū)表不支持 DEFAULT 分區(qū),實(shí)際上也不需要支持。

使用默認(rèn)分區(qū)也可能導(dǎo)致一些不可預(yù)見(jiàn)的問(wèn)題。例如,往 measurement 表中插入一條 2019 年的數(shù)據(jù),由于沒(méi)有創(chuàng)建相應(yīng)的分區(qū),該記錄同樣會(huì)分配到默認(rèn)分區(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)

此時(shí),如果再創(chuàng)建 2019 年的分區(qū),操作將會(huì)失敗。因?yàn)樘砑有碌姆謪^(qū)需要修改默認(rèn)分區(qū)的范圍(不再包含 2019 年的數(shù)據(jù)),但是默認(rèn)分區(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

為了解決這個(gè)問(wèn)題,可以先將默認(rèn)分區(qū)從分區(qū)表中卸載(DETACH PARTITION),創(chuàng)建新的分區(qū),將默認(rèn)分區(qū)中的相應(yīng)的數(shù)據(jù)移動(dòng)到新的分區(qū),最后重新掛載默認(rèn)分區(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

補(bǔ)充:postgresql10以上的自動(dòng)分區(qū)分表功能

一.列分表

1.首先創(chuàng)建主分區(qū)表:

create table fenbiao( 
id int, 
year varchar 
) partition by list(year)

這里設(shè)置的是根據(jù)year列進(jìn)行數(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ù)會(huì)依靠規(guī)則插入到不同分表中,如果插入一條不符合規(guī)則的數(shù)據(jù),則會(huì)報(bào)錯(cuò)誤:no partition of relation "fenbiao" found for row.

二.范圍分表

1.以year列為范圍進(jìn)行分表

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')

注意:此時(shí)插入year=2020會(huì)插入到下面的表;如下面表范圍為2021到2030,則會(huì)報(bào)錯(cuò);同時(shí)插入2030也會(huì)報(bào)錯(cuò);范圍相當(dāng)于時(shí)a=yearb;

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • PostgreSQL LIST、RANGE 表分區(qū)的實(shí)現(xiàn)方案
  • PostgreSQL 創(chuàng)建表分區(qū)
  • 淺析postgresql 數(shù)據(jù)庫(kù) TimescaleDB 修改分區(qū)時(shí)間范圍
  • 利用python為PostgreSQL的表自動(dòng)添加分區(qū)
  • 如何為PostgreSQL的表自動(dòng)添加分區(qū)
  • PostgreSQL之分區(qū)表(partitioning)
  • PostgreSQL分區(qū)表(partitioning)應(yīng)用實(shí)例詳解
  • PostgreSQL教程(三):表的繼承和分區(qū)表詳解
  • 淺談PostgreSQL表分區(qū)的三種方式

標(biāo)簽:崇左 仙桃 衡水 蘭州 銅川 湘潭 湖南 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《淺談PostgreSQL 11 新特性之默認(rèn)分區(qū)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266