PostgreSQL12.5中分區(qū)表的一些操作實(shí)例
1、創(chuàng)建一個(gè)有DEFAULT的分區(qū)表
1、先創(chuàng)建主表
create table tbl_log
(
id serial,
create_time timestamp(0) without time zone,
remark char(1)
) PARTITION BY RANGE (create_time);
#因?yàn)槭莝erial類型,自增的所以會自動創(chuàng)建一個(gè)序列
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+----------------+-------------------+----------
public | tbl_log | partitioned table | postgres
public | tbl_log_id_seq | sequence | postgres
(7 rows)
2、如果沒有創(chuàng)建分區(qū)就直接插入數(shù)據(jù)會報(bào)錯(cuò)
postgres=# INSERT INTO tbl_log(id, create_time, remark) VALUES (1, '2018-02-01', 'a'); ERROR: no partition of relation "tbl_log" found for row DETAIL: Partition key of the failing row contains (create_time) = (2018-02-01 00:00:00). postgres=#
3、創(chuàng)建分區(qū)
#包括左邊1.1,不包括2.1
CREATE TABLE tbl_log_p201801 PARTITION OF tbl_log FOR VALUES FROM ('2018-01-01') TO ('2018-02-01');
CREATE TABLE tbl_log_p201802 PARTITION OF tbl_log FOR VALUES FROM ('2018-02-01') TO ('2018-03-01');
CREATE TABLE tbl_log_p201803 PARTITION OF tbl_log FOR VALUES FROM ('2018-03-01') TO ('2018-04-01');
CREATE TABLE tbl_log_default PARTITION OF tbl_log DEFAULT;
INSERT INTO tbl_log(id, create_time, remark) VALUES (1, '2018-02-01', 'a');
INSERT INTO tbl_log(id, create_time, remark) VALUES (2, '2018-03-01', 'b');
INSERT INTO tbl_log(id, create_time, remark) VALUES (3, '2018-04-01', 'd');
INSERT INTO tbl_log(id, create_time, remark) VALUES (4, '2020-07-01', 'c');
4、查看分區(qū)情況
postgres=# select * from tbl_log;
id | create_time | remark
----+---------------------+--------
1 | 2018-02-01 00:00:00 | a
2 | 2018-03-01 00:00:00 | b
3 | 2018-04-01 00:00:00 | d
4 | 2020-07-01 00:00:00 | c
(4 rows)
postgres=# select * from tbl_log_p201801;
id | create_time | remark
----+-------------+--------
(0 rows)
postgres=# select * from tbl_log_p201802;
id | create_time | remark
----+---------------------+--------
1 | 2018-02-01 00:00:00 | a
(1 row)
postgres=# select * from tbl_log_p201803;
id | create_time | remark
----+---------------------+--------
2 | 2018-03-01 00:00:00 | b
(1 row)
postgres=# select * from tbl_log_default;
id | create_time | remark
----+---------------------+--------
3 | 2018-04-01 00:00:00 | d
4 | 2020-07-01 00:00:00 | c
(2 rows)
postgres=#
2、有default 分區(qū),再加分區(qū)
因?yàn)橛衐efault 分區(qū),再加分區(qū),所以會報(bào)錯(cuò)
postgres=# CREATE TABLE tbl_log_p201804 PARTITION OF tbl_log FOR VALUES FROM ('2018-04-01') TO ('2018-05-01');
ERROR: updated partition constraint for default partition "tbl_log_default" would be violated by some row
解決辦法:
以上添加分區(qū)報(bào)錯(cuò),需要解綁default分區(qū),之后再添加,如下
1、解綁Default分區(qū)
postgres=# ALTER TABLE tbl_log DETACH PARTITION tbl_log_default; ALTER TABLE
2、創(chuàng)建想要的分區(qū)
postgres=# CREATE TABLE tbl_log_p201804 PARTITION OF tbl_log FOR VALUES FROM ('2018-04-01') TO ('2018-05-01');
CREATE TABLE
3、分區(qū)創(chuàng)建成功,分區(qū)創(chuàng)建之后需把DEFAULT分區(qū)連接。
連接DEFAULT分區(qū)報(bào)錯(cuò),如下:
postgres=# ALTER TABLE tbl_log ATTACH PARTITION tbl_log_default DEFAULT; ERROR: partition constraint is violated by some row postgres=# INSERT INTO tbl_log_p201804 SELECT * FROM tbl_log_default; ERROR: new row for relation "tbl_log_p201804" violates partition constraint DETAIL: Failing row contains (4, 2020-07-01 00:00:00, c).
因?yàn)閠bl_log_default分區(qū)內(nèi)有2018-04-01的數(shù)據(jù),把這個(gè)數(shù)據(jù)從tbl_log_default中導(dǎo)出到對應(yīng)的分區(qū),并清理tbl_log_default中的對應(yīng)的數(shù)據(jù)
postgres=# INSERT INTO tbl_log_p201804 SELECT * FROM tbl_log_default where create_time>='2018-04-01' and create_time<'2018-05-01'; INSERT 0 1 postgres=# delete from tbl_log_default where create_time>='2018-04-01' and create_time<'2018-05-01'; DELETE 1
4、再次連接DEFAULT分區(qū)成功
postgres=# ALTER TABLE tbl_log ATTACH PARTITION tbl_log_default DEFAULT; ALTER TABLE
3、沒有default的分區(qū)
創(chuàng)建沒有default的分區(qū),當(dāng)插入的數(shù)據(jù)超過規(guī)劃好的分區(qū)的時(shí)候會報(bào)錯(cuò)
1、創(chuàng)建1月份分區(qū)
create table tbl_log2
(
id serial,
create_time timestamp(0) without time zone,
remark char(1)
) PARTITION BY RANGE (create_time);
CREATE TABLE tbl_log2_p201801 PARTITION OF tbl_log2 FOR VALUES FROM ('2018-01-01') TO ('2018-02-01');
插入2月的數(shù)據(jù)就會報(bào)錯(cuò)
postgres=# INSERT INTO tbl_log2(id, create_time, remark) VALUES (1, '2018-01-01', 'a'); INSERT 0 1 postgres=# INSERT INTO tbl_log2(id, create_time, remark) VALUES (1, '2018-02-01', 'a'); ERROR: no partition of relation "tbl_log2" found for row DETAIL: Partition key of the failing row contains (create_time) = (2018-02-01 00:00:00).
4、給分區(qū)表ddl
4.1、在原來沒有主鍵的分區(qū)表加主鍵
結(jié)論:
1、在主表加主鍵,主鍵為僅僅想要的主鍵,會報(bào)錯(cuò),需要用想要的主鍵+分區(qū)鍵組合為主鍵
2、分區(qū)表可以單獨(dú)添加主鍵
1.1、在主表加主鍵,主鍵為僅僅想要的主鍵,報(bào)錯(cuò)如下 must include all partitioning columns
postgres=# alter table tbl_log add primary key(id); ERROR: unique constraint on partitioned table must include all partitioning columns DETAIL: PRIMARY KEY constraint on table "tbl_log" lacks column "create_time" which is part of the partition key. postgres=# alter table tbl_log add primary key(id)
1.2、在主表添加主鍵需要是想要的主鍵+分區(qū)鍵
postgres=# alter table tbl_log add primary key (id,create_time);
ALTER TABLE
postgres=# \d tbl_log
Partitioned table "public.tbl_log"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | not null |
remark | character(1) | | |
name | character varying(2) | | |
Partition key: RANGE (create_time)
Indexes:
"tbl_log_pkey" PRIMARY KEY, btree (id, create_time)
Number of partitions: 5 (Use \d+ to list them.)
postgres=# \d tbl_log_p201801
Table "public.tbl_log_p201801"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | not null |
remark | character(1) | | |
name | character varying(2) | | |
Partition of: tbl_log FOR VALUES FROM ('2018-01-01 00:00:00') TO ('2018-02-01 00:00:00')
Indexes:
"tbl_log_p201801_pkey" PRIMARY KEY, btree (id, create_time)
1.3、可以給分區(qū)表單獨(dú)添加主鍵
postgres=# alter table tbl_log_p201801 add primary key (id);
ALTER TABLE
postgres=# \d tbl_log_p201801
Table "public.tbl_log_p201801"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | |
remark | character(1) | | |
name | character varying(2) | | |
Partition of: tbl_log FOR VALUES FROM ('2018-01-01 00:00:00') TO ('2018-02-01 00:00:00')
Indexes:
"tbl_log_p201801_pkey" PRIMARY KEY, btree (id)
postgres=#
4.2、創(chuàng)建分區(qū)表時(shí),就指定主鍵
主鍵不包括分區(qū)鍵,報(bào)錯(cuò)提示must include all partitioning columns
create table tbl_log2
(
id int,
create_time timestamp(0) without time zone,
remark char(1),
primary key (id)
);
ERROR: unique constraint on partitioned table must include all partitioning columns
DETAIL: PRIMARY KEY constraint on table "tbl_log2" lacks column "create_time" which is part of the partition key.
修改語句,添加分區(qū)鍵也為主鍵,創(chuàng)建成功
create table tbl_log2
(
id int,
create_time timestamp(0) without time zone,
remark char(1),
primary key (id,create_time)
) PARTITION BY RANGE (create_time);
CREATE TABLE
4.3、分區(qū)表加字段,修改字段
1、加字段,可以成功添加,在主表加字段,分區(qū)表會自動添加
postgres=# alter table tbl_log add name varchar(2);
ALTER TABLE
postgres=# \d tbl_log;
Partitioned table "public.tbl_log"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | |
remark | character(1) | | |
name | character varying(2) | | |
Partition key: RANGE (create_time)
Number of partitions: 5 (Use \d+ to list them.)
postgres=# \d tbl_log_p201801;
Table "public.tbl_log_p201801"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | |
remark | character(1) | | |
name | character varying(2) | | |
Partition of: tbl_log FOR VALUES FROM ('2018-01-01 00:00:00') TO ('2018-02-01 00:00:00')
2、直接在分區(qū)表加字段會報(bào)錯(cuò)
postgres=# alter table tbl_log_p201801 add name2 varchar(2); ERROR: cannot add column to a partition
3、修改字段
postgres=# alter table tbl_log alter column remark type varchar(10);
ALTER TABLE
postgres=# \d tbl_log;
Partitioned table "public.tbl_log"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | not null |
remark | character varying(10) | | |
name | character varying(2) | | |
Partition key: RANGE (create_time)
Indexes:
"tbl_log_pkey" PRIMARY KEY, btree (id, create_time)
Number of partitions: 5 (Use \d+ to list them.)
postgres=# \d tbl_log_p201801
Table "public.tbl_log_p201801"
Column | Type | Collation | Nullable | Default
-------------+--------------------------------+-----------+----------+-------------------------------------
id | integer | | not null | nextval('tbl_log_id_seq'::regclass)
create_time | timestamp(0) without time zone | | not null |
remark | character varying(10) | | |
name | character varying(2) | | |
Partition of: tbl_log FOR VALUES FROM ('2018-01-01 00:00:00') TO ('2018-02-01 00:00:00')
Indexes:
"tbl_log_p201801_pkey" PRIMARY KEY, btree (id, create_time)
postgres=#
總結(jié)
到此這篇關(guān)于PostgreSQL12.5中分區(qū)表的一些操作的文章就介紹到這了,更多相關(guān)pg12.5分區(qū)表操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫
這篇文章主要介紹了如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02
Postgresql 檢查數(shù)據(jù)庫主從復(fù)制進(jìn)度的操作
這篇文章主要介紹了Postgresql 檢查數(shù)據(jù)庫主從復(fù)制進(jìn)度的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
PostgreSQL數(shù)據(jù)庫從入門到精通實(shí)戰(zhàn)
這是一份詳細(xì)的PostgreSQL數(shù)據(jù)庫使用指南,涵蓋了核心概念、安裝、基本操作、高級功能、管理與維護(hù)、安全、復(fù)制與高可用等多個(gè)方面,幫助用戶從入門到精通PostgreSQL數(shù)據(jù)庫,這份指南提供了 PostgreSQL的全面概覽和核心實(shí)踐,感興趣的朋友跟隨小編一起看看吧2026-01-01
Linux CentOS 7安裝PostgreSQL9.3圖文教程
這篇文章主要為大家詳細(xì)介紹了Linux CentOS 7安裝PostgresSQL9.3圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
PostgreSQL字符切割:substring函數(shù)的用法說明
這篇文章主要介紹了PostgreSQL字符切割:substring函數(shù)的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
PostgreSQL實(shí)時(shí)查看數(shù)據(jù)庫實(shí)例正在執(zhí)行的SQL語句實(shí)例詳解
在任何數(shù)據(jù)庫中,分析和優(yōu)化SQL的執(zhí)行,最重要的工作就是執(zhí)行計(jì)劃的解讀,而說到執(zhí)行計(jì)劃得先了解postgresql的查詢執(zhí)行過程,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL實(shí)時(shí)查看數(shù)據(jù)庫實(shí)例正在執(zhí)行的SQL語句的相關(guān)資料,需要的朋友可以參考下2023-01-01
PostgreSQL連接數(shù)過多報(bào)錯(cuò):too many clients already的解決
在使用 Navicat 連接 PostgreSQL 數(shù)據(jù)庫時(shí),突然遭遇到了一個(gè)報(bào)錯(cuò):“FATAL: sorry, too many clients already”,這一錯(cuò)誤提示表明數(shù)據(jù)庫連接數(shù)已經(jīng)達(dá)到上限,無法再創(chuàng)建新連接,所以本文給大家介紹了相關(guān)的解決辦法,需要的朋友可以參考下2024-03-03

