PostgreSQL 10 Beta1分区和分区外部表测试说明
admin
2023-03-23 07:40:40
0

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=# 


相关内容

热门资讯

杭州景区咖啡店闯入1条幼蛇,仅... 夏天气温高、湿度大,蛇类活动进入高峰期,千万别被“小蛇无害”的刻板印象骗了!有些幼蛇看着迷你可爱,实...
AI开源公开信签署企业增至50... IT之家 7 月 26 日消息,英伟达、微软、IBM、Meta 等 25 家美国科技企业本周(7 月...
南阳快手短视频代运营企业的服务... 在短视频平台快速发展的背景下,企业通过快手等渠道进行内容运营已成为常见的市场动作。对于希望借助短视频...
普通人该如何跟上科技发展的步伐 这个被称作科技的词汇, 听起来既有宏大之感, 又颇为遥远, 好似仅仅归属于在实验室里专心致志钻研的科...
原创 王... 菲尔兹奖名单一出,邓煜和王虹两位华人数学家共同获奖,本应是一件令人振奋的喜讯。然而,王虹那段仅3分1...
《万物》杂志首个阅读空间亮相正... 26日,由国内知名的青少年家庭科普期刊《万物》杂志与广州正佳自然科学探索中心联合打造的阅读互动空间—...
从康复机器人到脑机接口 智能康... 天津北方网讯:对脑卒中患者来说,重新抬起手臂、迈出一步,往往需要持续的康复训练,逐步恢复运动功能;对...
原创 苹... 当苹果、谷歌、斯坦福、麻省理工都无法留住一位年轻的中国AI天才时,究竟是什么力量吸引他毅然回国?为何...
为什么新能源车供应链要本地化? 当前汽车产业变革加速,电动化成为核心发展方向。车企为了在激烈市场中站稳脚跟,必须优化生产结构。其中,...
携程罚单的真正影响,不是酒店变... 文 | 听风译码 7月25日,市场监管总局对携程开出51.79亿元天价罚单。 全网叫好。 但如果你...