最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PostgreSQL12.5中分區(qū)表的一些操作實例

 更新時間:2022年08月12日 11:57:21   作者:面子拿錢砸  
PostgreSQL支持通過表繼承進行分區(qū),下面這篇文章主要給大家介紹了關于PostgreSQL12.5中分區(qū)表的一些操作的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

1、創(chuàng)建一個有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);
#因為是serial類型,自增的所以會自動創(chuàng)建一個序列
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ū)就直接插入數據會報錯

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

因為有default 分區(qū),再加分區(qū),所以會報錯

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ū)報錯,需要解綁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ū)報錯,如下:

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分區(qū)內有2018-04-01的數據,把這個數據從tbl_log_default中導出到對應的分區(qū),并清理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分區(qū)成功

postgres=# ALTER TABLE tbl_log ATTACH PARTITION tbl_log_default DEFAULT;
ALTER TABLE

3、沒有default的分區(qū)

創(chuàng)建沒有default的分區(qū),當插入的數據超過規(guī)劃好的分區(qū)的時候會報錯

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月的數據就會報錯

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ū)表加主鍵

結論:

1、在主表加主鍵,主鍵為僅僅想要的主鍵,會報錯,需要用想要的主鍵+分區(qū)鍵組合為主鍵

2、分區(qū)表可以單獨添加主鍵

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、在主表添加主鍵需要是想要的主鍵+分區(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ū)表單獨添加主鍵

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ū)表時,就指定主鍵

主鍵不包括分區(qū)鍵,報錯提示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ū)表加字段會報錯

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中分區(qū)表的一些操作的文章就介紹到這了,更多相關pg12.5分區(qū)表操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • postgresql高級應用之合并單元格的思路詳解

    postgresql高級應用之合并單元格的思路詳解

    這篇文章主要介紹了postgresql高級應用之合并單元格,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05
  • 一文詳解PostgreSQL復制參數

    一文詳解PostgreSQL復制參數

    PostgreSQL 作為一款功能強大的開源關系型數據庫,其復制功能對于構建高可用性系統(tǒng)至關重要,本文給大家詳細介紹了PostgreSQL的復制參數,需要的朋友可以參考下
    2025-05-05
  • PostgreSQL誤刪數據庫該怎么辦詳解

    PostgreSQL誤刪數據庫該怎么辦詳解

    這篇文章主要介紹了PostgreSQL中誤刪數據庫的恢復方法,包括備份恢復、歸檔日志恢復和操作系統(tǒng)層面的快照恢復,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-03-03
  • PostgreSQL進行數據導入和導出的操作代碼

    PostgreSQL進行數據導入和導出的操作代碼

    在數據庫管理中,數據的導入和導出是非常常見的操作,特別是在 PostgreSQL 中,提供了多種工具和方法來實現數據的有效管理,本文將詳細介紹在 PostgreSQL 中如何進行數據導入和導出,并給出具體的命令及示例,需要的朋友可以參考下
    2024-10-10
  • 基于PostgreSQL和mysql數據類型對比兼容

    基于PostgreSQL和mysql數據類型對比兼容

    這篇文章主要介紹了基于PostgreSQL和mysql數據類型對比兼容,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • PostgreSQL連接數過多的原因分析與連接池方案

    PostgreSQL連接數過多的原因分析與連接池方案

    在 PostgreSQL 的生產運維中,連接數過多是最常見且影響深遠的性能問題之一,本文將系統(tǒng)性地剖析 連接數過多的根本原因,詳解 PostgreSQL 連接機制與資源開銷,并對比主流 連接池方案的原理、配置與適用場景,需要的朋友可以參考下
    2026-02-02
  • 在PostgreSQL中使用ltree處理層次結構數據的方法

    在PostgreSQL中使用ltree處理層次結構數據的方法

    這篇文章主要介紹了在PostgreSQL中使用ltree處理層次結構數據,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • PostgreSQL 自定義自動類型轉換操作(CAST)

    PostgreSQL 自定義自動類型轉換操作(CAST)

    這篇文章主要介紹了PostgreSQL 自定義自動類型轉換操作(CAST),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 如何將postgresql數據庫表內數據導出為excel格式(推薦)

    如何將postgresql數據庫表內數據導出為excel格式(推薦)

    這篇文章主要介紹了如何將postgresql數據庫表內數據導出為excel格式(推薦),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • PostgreSQL登陸方式(本地和遠程)的實現

    PostgreSQL登陸方式(本地和遠程)的實現

    本次分享一下PostgreSQL 的登陸方式,包括本地登錄和遠程登錄,文中通過圖文示例介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-11-11

最新評論

峨山| 江津市| 随州市| 宽城| 海门市| 广州市| 灵宝市| 双流县| 镇平县| 津市市| 东宁县| 芜湖县| 晋江市| 桓台县| 即墨市| 周宁县| 方山县| 盈江县| 白银市| 晋州市| 昂仁县| 兴宁市| 西乌珠穆沁旗| 阿勒泰市| 远安县| 万全县| 桂林市| 鸡泽县| 榕江县| 诸暨市| 雅江县| 东乡| 罗山县| 恭城| 自治县| 江山市| 丰都县| 黑龙江省| 永州市| 观塘区| 太仆寺旗|