1. 程式人生 > >PostgreSQL 10 Beta1分區和分區外部表測試說明

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

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分區和分區外部表測試說明