<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
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)
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=#
#包括左邊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');
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=#
因為有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
建立沒有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).
結論:
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=#
主鍵不包括分割區鍵,報錯提示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
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=#
到此這篇關於PostgreSQL12.5中分割區表的一些操作的文章就介紹到這了,更多相關pg12.5分割區表操作內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45