PostgreSQL 10 Beta1分區和分區外部表格測試說明

來源:互聯網
上載者:User

標籤:postgresql10 partition_table foreign_partition_table

1.建立主表

CREATE TABLE part_tab (c1 int, c2 int,name varchar(20)) PARTITION BY RANGE (c1);

2.建立子分區表

CREATE TABLE part1 PARTITION OF part_tab FOR VALUES FROM (0) TO (100);

CREATE TABLE part2 PARTITION OF part_tab FOR VALUES FROM (100) TO (200);


3.附加已經存在的表作為主表的子分區

1).attach partition(附加表分區)

-- 執行附加分區命令前,要附加分區的表必須已經存在

create table ext_part(c1 int not null, c2 int,name varchar(20));

附加分區前,要附加分區的資料必須滿足主表分區列的約束條件,如果不滿足條件則無法把新的分區附加到主表。

ALTER TABLE part_tab ATTACH PARTITION ext_part FOR VALUES FROM (400) to (500);


2).detach partition(摘除分區表)

--解除分區綁定,解除後\d+命令顯示分區表中就不包含已經摘除的分區。

ALTER TABLE part_tab DETACH PARTITION ext_part;


4.準備外部伺服器(子表格服務器)

$ psql testdb

# create table fpart3 (c1 int not null, c2 int,name varchar(20));

# create table fpart4 (c1 int not null, c2 int,name varchar(20));


testdb=# \dt

         List of relations

 Schema |  Name  | Type  |  Owner   

--------+--------+-------+----------

 public | fpart3 | table | postgres

 public | fpart4 | table | postgres

(2 rows)


testdb=# 


5.增加擴充

$ psql testdb

# create extension postgres_fdw;

# create server server_remote_226 foreign data wrapper postgres_fdw options(host ‘172.16.3.226‘,port ‘5432‘,dbname ‘testdb‘);

# create user mapping for postgres server server_remote_226 options(user ‘postgres‘,password ‘111111‘);


testdb=# \des+

                                                                   List of foreign servers

       Name        |  Owner   | Foreign-data wrapper | Access privileges | Type | Version |                     FDW Options         

            | Description 

-------------------+----------+----------------------+-------------------+------+---------+-----------------------------------------

------------+-------------

 server_remote_226 | postgres | postgres_fdw         |                   |      |         | (host ‘172.16.3.226‘, port ‘5432‘, dbnam

e ‘testdb‘) | 

(1 row)


testdb=# \deu+

                         List of user mappings

      Server       | User name |              FDW Options               

-------------------+-----------+----------------------------------------

 server_remote_226 | postgres  | ("user" ‘postgres‘, password ‘111111‘)

(1 row)



6.建立外部表格

CREATE FOREIGN TABLE part3 PARTITION OF part_tab FOR VALUES FROM (200) TO (300) SERVER server_remote_226 options (schema_name ‘public‘,table_name ‘fpart3‘);

CREATE FOREIGN TABLE part4 PARTITION OF part_tab FOR VALUES FROM (300) TO (400) SERVER server_remote_226 options (schema_name ‘public‘,table_name ‘fpart4‘);


testdb=# \det+

                                     List of foreign tables

 Schema | Table |      Server       |                 FDW Options                 | Description 

--------+-------+-------------------+---------------------------------------------+-------------

 public | part3 | server_remote_226 | (schema_name ‘public‘, table_name ‘fpart3‘) | 

 public | part4 | server_remote_226 | (schema_name ‘public‘, table_name ‘fpart4‘) | 

(2 rows)


testdb=# 


7.插入測試資料


-- 外部分區表無法通過主表插入資料,需要通過外部表格方式插入

testdb=# insert into part_tab values(1,1,‘Chris‘),(101,101,‘Peter‘),(201,201,‘William‘),(301,301,‘Feng‘);

ERROR:  cannot route inserted tuples to a foreign table

testdb=# 

資料分別在各自伺服器插入

主表格服務器,通過主表插入本地分區資料

# insert into part_tab values(1,1,‘Chris‘),(101,101,‘Peter‘);

INSERT 0 2

testdb=# 


外部表格伺服器,通過外部表格分別插入

testdb=# insert into part3 values(201,201,‘William‘);

INSERT 0 1

testdb=# insert into part4 values(301,301,‘Feng‘);

INSERT 0 1

testdb=# 

testdb=# 

testdb=# select * from part_tab ;

 c1  | c2  |  name   

-----+-----+---------

   1 |   1 | Chris

 101 | 101 | Peter

 201 | 201 | William

 301 | 301 | Feng

(4 rows)


testdb=# 


外部分區表對違反分區列的插入無約束機制,這樣的資料可以任意插入。

testdb=# insert into part4 values(201,301,‘Feng‘);

INSERT 0 1

testdb=# select * from part_tab ;

 c1  | c2  |  name   

-----+-----+---------

   1 |   1 | Chris

 101 | 101 | Peter

 201 | 201 | William

 301 | 301 | Feng

 201 | 301 | Feng

(5 rows)


testdb=# 


8.添加主鍵和約束


-- 主表分區列不支援建立主鍵約束

testdb=# alter table part_tab add constraint part_tab_c1_pkey primary key(c1);

ERROR:  primary key constraints are not supported on partitioned tables

LINE 1: alter table part_tab add constraint part_tab_c1_pkey primary...

                                 ^

testdb=# 


--- 約束、索引需在子表添加


--主表格服務器

testdb=# alter table part1 add constraint part1_c1_pkey primary key(c1);

ALTER TABLE

testdb=# alter table part2 add constraint part2_c1_pkey primary key(c1);

ALTER TABLE

testdb=# 

testdb=# create index idx_part1_c1_c2_name on part1(c1,c2,name);

CREATE INDEX

testdb=# create index idx_part2_c1_c2_name on part2(c1,c2,name);

CREATE INDEX

testdb=# 

testdb=# \d part1

                      Table "public.part1"

 Column |         Type          | Collation | Nullable | Default 

--------+-----------------------+-----------+----------+---------

 c1     | integer               |           | not null | 

 c2     | integer               |           |          | 

 name   | character varying(20) |           |          | 

Partition of: part_tab FOR VALUES FROM (0) TO (100)

Indexes:

    "part1_c1_pkey" PRIMARY KEY, btree (c1)

    "idx_part1_c1_c2_name" btree (c1, c2, name)


testdb=# 

testdb=# \d part2

                      Table "public.part2"

 Column |         Type          | Collation | Nullable | Default 

--------+-----------------------+-----------+----------+---------

 c1     | integer               |           | not null | 

 c2     | integer               |           |          | 

 name   | character varying(20) |           |          | 

Partition of: part_tab FOR VALUES FROM (100) TO (200)

Indexes:

    "part2_c1_pkey" PRIMARY KEY, btree (c1)

    "idx_part2_c1_c2_name" btree (c1, c2, name)


testdb=#  


-- 子表格服務器

testdb=# alter table fpart3 add constraint fpart3_c1_pkey primary key(c1);

ALTER TABLE

testdb=# alter table fpart4 add constraint fpart4_c1_pkey primary key(c1);

ALTER TABLE

testdb=# create index idx_fpart3_c1_c2_name on fpart3(c1,c2,name);

CREATE INDEX

testdb=# create index idx_fpart4_c1_c2_name on fpart4(c1,c2,name);

CREATE INDEX

testdb=# 

testdb=# \d fpart3

                      Table "public.fpart3"

 Column |         Type          | Collation | Nullable | Default 

--------+-----------------------+-----------+----------+---------

 c1     | integer               |           | not null | 

 c2     | integer               |           |          | 

 name   | character varying(20) |           |          | 

Indexes:

    "fpart3_c1_pkey" PRIMARY KEY, btree (c1)

    "idx_fpart3_c1_c2_name" btree (c1, c2, name)


testdb=# 

testdb=# \d fpart4

                      Table "public.fpart4"

 Column |         Type          | Collation | Nullable | Default 

--------+-----------------------+-----------+----------+---------

 c1     | integer               |           | not null | 

 c2     | integer               |           |          | 

 name   | character varying(20) |           |          | 

Indexes:

    "fpart4_c1_pkey" PRIMARY KEY, btree (c1)

    "idx_fpart4_c1_c2_name" btree (c1, c2, name)


testdb=# 


9.查詢語句的分區修剪


testdb=# explain analyze select * from part_tab where c1=1;

                                              QUERY PLAN                                              

------------------------------------------------------------------------------------------------------

 Append  (cost=0.00..1.01 rows=1 width=66) (actual time=0.009..0.009 rows=1 loops=1)

   ->  Seq Scan on part1  (cost=0.00..1.01 rows=1 width=66) (actual time=0.008..0.009 rows=1 loops=1)

         Filter: (c1 = 1)

 Planning time: 0.234 ms

 Execution time: 0.027 ms

(5 rows)


testdb=# explain analyze select * from part_tab where c1=101;

                                              QUERY PLAN                                              

------------------------------------------------------------------------------------------------------

 Append  (cost=0.00..1.01 rows=1 width=66) (actual time=0.025..0.028 rows=1 loops=1)

   ->  Seq Scan on part2  (cost=0.00..1.01 rows=1 width=66) (actual time=0.024..0.026 rows=1 loops=1)

         Filter: (c1 = 101)

 Planning time: 0.271 ms

 Execution time: 0.066 ms

(5 rows)


testdb=# explain analyze select * from part_tab where c1=201;

                                                  QUERY PLAN                                                  

--------------------------------------------------------------------------------------------------------------

 Append  (cost=100.00..121.47 rows=5 width=66) (actual time=2.179..2.180 rows=1 loops=1)

   ->  Foreign Scan on part3  (cost=100.00..121.47 rows=5 width=66) (actual time=2.178..2.178 rows=1 loops=1)

 Planning time: 0.308 ms

 Execution time: 3.551 ms

(4 rows)


testdb=# explain analyze select * from part_tab where c1=301;

                                                  QUERY PLAN                                                  

--------------------------------------------------------------------------------------------------------------

 Append  (cost=100.00..121.47 rows=5 width=66) (actual time=1.218..1.219 rows=1 loops=1)

   ->  Foreign Scan on part4  (cost=100.00..121.47 rows=5 width=66) (actual time=1.217..1.218 rows=1 loops=1)

 Planning time: 0.312 ms

 Execution time: 2.178 ms

(4 rows)


testdb=# explain analyze select * from part_tab where c1<201;

                                                   QUERY PLAN                                                   

----------------------------------------------------------------------------------------------------------------

 Append  (cost=0.00..129.46 rows=305 width=66) (actual time=0.014..2.881 rows=2 loops=1)

   ->  Seq Scan on part1  (cost=0.00..1.01 rows=1 width=66) (actual time=0.014..0.015 rows=1 loops=1)

         Filter: (c1 < 201)

   ->  Seq Scan on part2  (cost=0.00..1.01 rows=1 width=66) (actual time=0.009..0.009 rows=1 loops=1)

         Filter: (c1 < 201)

   ->  Foreign Scan on part3  (cost=100.00..127.44 rows=303 width=66) (actual time=2.855..2.855 rows=0 loops=1)

 Planning time: 0.234 ms

 Execution time: 3.884 ms

(8 rows)


testdb=# 


本文出自 “yiyi” 部落格,請務必保留此出處http://heyiyi.blog.51cto.com/205455/1927626

PostgreSQL 10 Beta1分區和分區外部表格測試說明

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.