網站首頁 編程語言 正文
1、創建一個有DEFAULT的分區表
1、先創建主表
create table tbl_log
(
id serial,
create_time timestamp(0) without time zone,
remark char(1)
) PARTITION BY RANGE (create_time);
#因為是serial類型,自增的所以會自動創建一個序列
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、如果沒有創建分區就直接插入數據會報錯
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、創建分區
#包括左邊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、查看分區情況
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 分區,再加分區
因為有default 分區,再加分區,所以會報錯
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
解決辦法:
以上添加分區報錯,需要解綁default分區,之后再添加,如下
1、解綁Default分區
postgres=# ALTER TABLE tbl_log DETACH PARTITION tbl_log_default;
ALTER TABLE
2、創建想要的分區
postgres=# CREATE TABLE tbl_log_p201804 PARTITION OF tbl_log FOR VALUES FROM ('2018-04-01') TO ('2018-05-01');
CREATE TABLE
3、分區創建成功,分區創建之后需把DEFAULT分區連接。
連接DEFAULT分區報錯,如下:
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).
因為tbl_log_default分區內有2018-04-01的數據,把這個數據從tbl_log_default中導出到對應的分區,并清理tbl_log_default中的對應的數據
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分區成功
postgres=# ALTER TABLE tbl_log ATTACH PARTITION tbl_log_default DEFAULT;
ALTER TABLE
3、沒有default的分區
創建沒有default的分區,當插入的數據超過規劃好的分區的時候會報錯
1、創建1月份分區
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月的數據就會報錯
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、給分區表ddl
4.1、在原來沒有主鍵的分區表加主鍵
結論:
1、在主表加主鍵,主鍵為僅僅想要的主鍵,會報錯,需要用想要的主鍵+分區鍵組合為主鍵
2、分區表可以單獨添加主鍵
1.1、在主表加主鍵,主鍵為僅僅想要的主鍵,報錯如下 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、在主表添加主鍵需要是想要的主鍵+分區鍵
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、可以給分區表單獨添加主鍵
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、創建分區表時,就指定主鍵
主鍵不包括分區鍵,報錯提示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.
修改語句,添加分區鍵也為主鍵,創建成功
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、分區表加字段,修改字段
1、加字段,可以成功添加,在主表加字段,分區表會自動添加
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、直接在分區表加字段會報錯
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=#
總結
原文鏈接:https://blog.csdn.net/weixin_42583514/article/details/123063420
相關推薦
- 2023-01-18 Android?Admob接入原理及完整教程示例_Android
- 2022-05-22 Docker容器搭建Kafka集群的詳細過程_docker
- 2022-06-11 嵌入式C語言二級指針在鏈表中的應用_C 語言
- 2023-06-17 解讀C語言非void函數卻沒有return會怎么樣_C 語言
- 2022-09-29 docker啟動rabbitmq以及使用方式詳解_docker
- 2023-01-15 rust異步編程詳細講解_Rust語言
- 2022-07-03 C#入門之結構類型Struct_C#教程
- 2022-10-25 linux服務器校對時間方法命令詳解_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支