8. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 82017/8/11
-- 建立主表
CREATE TABLE tbl( a INT, b VARCHAR(10));
-- 建立繼承表
CREATE TABLE tbl_1 (CHECK( a <= 1000 ) ) INHERITS (tbl);
CREATE TABLE tbl_2 (CHECK( a <= 10000 AND a >1000 ))
INHERITS (tbl);
CREATE TABLE tbl_3 ( CHECK( a <= 100000 AND a >10000 ))
INHERITS (tbl);
原始步驟:
1. 建立主表、繼承表
2. 建立 function
3. 建立 Trigger
9. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 92017/8/11
-- 建立function
CREATE OR REPLACE FUNCTION tbl_part_tg()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW. a <= 1000 ) THEN
INSERT INTO tbl_1 VALUES (NEW.*);
ELSIF ( NEW. a > 1000 and NEW.a <= 10000 ) THEN
INSERT INTO tbl_2 VALUES (NEW.*);
...(略,同上 tbl_3, tbl_4)
ELSE
RAISE EXCEPTION 'data out of range!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
10. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 102017/8/11
-- 建立trigger
CREATE TRIGGER insert_tbl_part_tg
BEFORE INSERT ON tbl
FOR EACH ROW EXECUTE PROCEDURE tbl_part_tg();
-- EXPLAIN
postgres=# explain select *from tbl where a =11111;
QUERY PLAN
-------------------------------------------------------------
Append (cost=0.00..24.50 rows=7 width=42)
-> Seq Scan on tbl (cost=0.00..0.00 rows=1 width=42)
Filter: (a = 11111)
-> Seq Scan on tbl_3 (cost=0.00..24.50 rows=6 width=42)
Filter: (a = 11111)
(5 rows)
11. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 112017/8/11
PG 9.6
• 建立主表、繼承表
• 建立 function
• 建立 Trigger
PG 10
• 建立主表
• PARTITION BY
• LIST
• RANGE
• HASH
• 建立繼承表
• 建立 function
• 建立 Trigger
(Not yet)
12. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 122017/8/11
-- Partition by LIST
CREATE TABLE list_parted (a int) PARTITION BY LIST (a);
CREATE TABLE part_1 PARTITION OF list_parted FOR VALUES IN (1);
CREATE TABLE part_2 PARTITION OF list_parted FOR VALUES IN (2);
CREATE TABLE part_3 PARTITION OF list_parted FOR VALUES IN (3);
CREATE TABLE part_4 PARTITION OF list_parted FOR VALUES IN (4);
CREATE TABLE part_5 PARTITION OF list_parted FOR VALUES IN (5);
13. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 132017/8/11
-- Partition by LIST
postgres=# insert into list_parted values(32); --failed
ERROR: no partition of relation "list_parted" found for row
DETAIL: Failing row contains (32).
postgres=# insert into part_1 values(1);
INSERT 0 1
postgres=# insert into part_1 values(2);--failed
ERROR: new row for relation "part_1" violates partition
constraint
DETAIL: Failing row contains (2).
14. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 142017/8/11
-- Partition by LIST
postgres=# explain select *from list_parted where a =1;
QUERY PLAN
---------------------------------------------------------------
--
Append (cost=0.00..41.88 rows=14 width=4)
-> Seq Scan on list_parted (cost=0.00..0.00 rows=1 width=4)
Filter: (a = 1)
-> Seq Scan on part_1 (cost=0.00..41.88 rows=13 width=4)
Filter: (a = 1)
(5 rows)
※ 主表不會查
15. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 152017/8/11
-- Partition by RANGE
CREATE TABLE range_parted ( a int ) PARTITION BY RANGE (a);
CREATE TABLE range_parted1 PARTITION OF range_parted FOR VALUES
from (1) TO (1000);
CREATE TABLE range_parted2 PARTITION OF range_parted FOR VALUES
FROM (1000) TO (10000);
CREATE TABLE range_parted3 PARTITION OF range_parted FOR VALUES
FROM (1000) TO (100000);
postgres=# insert into range_parted values(123);
INSERT 0 1
postgres=# insert into range_parted1 values(456);
INSERT 0 1
16. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 162017/8/11
-- Partition by RANGE
postgres=# explain select *from range_parted where a=32425;
QUERY PLAN
---------------------------------------------------------------
------
Append (cost=0.00..41.88 rows=14 width=4)
-> Seq Scan on range_parted (cost=0.00..0.00 rows=1
width=4)
Filter: (a = 32425)
-> Seq Scan on range_parted3 (cost=0.00..41.88 rows=13
width=4)
Filter: (a = 32425)
(5 rows)
※ 主表不會查
17. PostgreSQL 10 新特性
1. 資料表分割強化 (Table Partitioning)
Taiwan PostgreSQL User Group 172017/8/11
-- ATTACH
postgres=# alter table range_parted ATTACH PARTITION
range_parted5 FOR VALUES FROM (100000) TO (1000000);
ALTER TABLE
-- DETACH
postgres=# alter table range_parted DETACH PARTITION
range_parted5 ;
ALTER TABLE
20. PostgreSQL 10 新特性
2. 邏輯複製(Logical Replication)
Taiwan PostgreSQL User Group 202017/8/11
$ psql -p 5432 -c "ALTER
SYSTEM SET wal_level =
'logical';" test
$ pg_ctl -p 5432 restart
$ psql -p 5432 test
CREATE TABLE test (x INT
PRIMARY KEY);
INSERT INTO test VALUES (1);
CREATE PUBLICATION mypub FOR
TABLE test;
$ psql -p 5433 test
CREATE TABLE test (x INT
PRIMARY KEY);
CREATE SUBSCRIPTION mysub
CONNECTION 'dbname=test
port=5432' PUBLICATION mypub;
Cluseter 1 Cluseter 2
21. PostgreSQL 10 新特性
2. 邏輯複製(Logical Replication)
• 邏輯複製允許:
– 資料表層級的顆粒細度
– 可從多台主機複製到集中的一台主機
– 可以從單一資料表複製到多台主機
Taiwan PostgreSQL User Group 212017/8/11
INSERT INTO test VALUES (2);
SELECT * FROM test;
1
SELECT * FROM test;
1
2
Cluseter 1 Cluseter 2
22. PostgreSQL 10 新特性
3. 平行查詢強化 (Parallel Queries)
• PG 9.6 已有此功能
• PG 10 開始支援以下平行查詢
– B-tree index scans
– Bitmap heap scans
– Merge joins
– Procedural Languages
Taiwan PostgreSQL User Group 222017/8/11
23. PostgreSQL 10 新特性
3. 平行查詢強化 (Parallel Queries)
Taiwan PostgreSQL User Group 232017/8/11
-- 建立測試資料表
create table test_big1(id int4 primary key, create_time
timestamp without time zone default clock_timestamp(), name
character varying(32));
-- 插入 1000萬筆資料
insert into test_big1(id,name) select n,n*random()*10000 from
generate_series(1,10000000) n ;
-- 資料插入後的資料表
postgres=# select * from test_big1 limit 3;
id | create_time | name
----+----------------------------+------------------
1 | 2017-05-21 16:02:24.921751 | 2298.13809040934
2 | 2017-05-21 16:02:24.922051 | 7580.18649183214
3 | 2017-05-21 16:02:24.922064 | 24218.4893181548
(3 rows)
24. PostgreSQL 10 新特性
3. 平行查詢強化 (Parallel Queries)
Taiwan PostgreSQL User Group 242017/8/11
-- 查詢 max_parallel_workers
postgres=# show max_parallel_workers;
max_parallel_workers
----------------------
4
(1 row)
-- 在啟動平行查詢的狀態執行EXPLAIN
postgres=# explain analyze select count(*) from test_big1 where
id <1000000 ;
25. PostgreSQL 10 新特性
3. 平行查詢強化 (Parallel Queries)
Taiwan PostgreSQL User Group 252017/8/11
QUERY PLAN
---------------------------------------------------------------
Finalize Aggregate (cost=18576.59..18576.60 rows=1 width=8)
(actual time=73.362..73.362 rows=1 loops=1)
-> Gather (cost=18576.17..18576.58 rows=4 width=8) (actual
time=73.200..73.355 rows=5 loops=1)
Workers Planned: 4
Workers Launched: 4
-> Partial Aggregate (cost=17576.17..17576.18 rows=1
width=8) (actual time=68.992..68.992 rows=1 loops=5)
-> Parallel Index Only Scan using
test_big1_pkey on test_big1 (cost=0.43..16947.37 rows=251523
width=0) (actual time=0.053..54.343 rows=200000 loops=5)
Index Cond: (id < 1000000)
Heap Fetches: 174195
Planning time: 0.105 ms
Execution time: 74.572 ms
(10 rows)
26. PostgreSQL 10 新特性
3. 平行查詢強化 (Parallel Queries)
Taiwan PostgreSQL User Group 262017/8/11
-- 關閉 max_parallel_workers
postgres=# set max_parallel_workers=0;
SET
-- 關閉平行查詢後執行EXPLAIN
postgres=# explain analyze select count(*) from test_big1 where
id <1000000 ;
27. PostgreSQL 10 新特性
3. 平行查詢強化 (Parallel Queries)
Taiwan PostgreSQL User Group 272017/8/11
QUERY PLAN
---------------------------------------------------------------
Finalize Aggregate (cost=18576.59..18576.60 rows=1 width=8)
(actual time=257.585..257.585 rows=1 loops=1)
-> Gather (cost=18576.17..18576.58 rows=4 width=8) (actual
time=257.579..257.579 rows=1 loops=1)
Workers Planned: 4
Workers Launched: 0
-> Partial Aggregate (cost=17576.17..17576.18 rows=1
width=8) (actual time=257.251..257.251 rows=1 loops=1)
-> Parallel Index Only Scan using
test_big1_pkey on test_big1 (cost=0.43..16947.37 rows=251523
width=0) (actual time=0.042..183.384 rows=999999 loops=1)
Index Cond: (id < 1000000)
Heap Fetches: 999999
Planning time: 0.102 ms
Execution time: 257.717 ms
(10 rows)
※ 大約 74.572 ms 的4倍
29. PostgreSQL 10 新特性
4. FDW強化(Additional FDW Push-Down)
• PG10 FDW: Query Push-Down (Aggregation)
Taiwan PostgreSQL User Group 292017/8/11
PG 9.x
FDW
FTBL
TBL
All data
explain (analyze on,verbose on) select flag,count(*)
from ft_test_fdw3 group by flag;
Table,
Where
PG 10
FDW
FTBL
TBL
Selected
data
Table,
Where,
Group by,
Count()
…
Rows: 100,000
9.6 Time: 441.758 ms
10 Time: 19.928 ms
30. PostgreSQL 10 新特性
4. FDW強化(Additional FDW Push-Down)
Taiwan PostgreSQL User Group 302017/8/11
QUERY PLAN
---------------------------------------------------------------
------------------------------------------
Sort (cost=211.41..211.91 rows=200 width=12) (actual
time=19.662..19.662 rows=3 loops=1)
Output: flag, (count(*))
Sort Key: ft_test_fdw3.flag
Sort Method: quicksort Memory: 25kB
-> Foreign Scan (cost=129.25..203.76 rows=200 width=12)
(actual time=19.648..19.649 rows=3 loops=1)
Output: flag, (count(*))
Relations: Aggregate on (francs.ft_test_fdw3)
Remote SQL: SELECT flag, count(*) FROM
francs.test_fdw3 GROUP BY flag
Planning time: 0.212 ms
Execution time: 19.928
※ Push-down :
count(*)、Group By
※ 9.6: rows=100K
31. PostgreSQL 10 新特性
5.多節點同步寫入 (Quorum Commit)
• 有兩種方式:
– FIRST num_sync (standby_name [, ...])
– ANY num_sync (standby_name [, ...])
• num_sync 是指需要同步複製的備節點個數。
• standby_name 是指同步複製備節點的名稱。
Taiwan PostgreSQL User Group 312017/8/11
postgres=# ALTER SYSTEM SET synchronous_standby_names = 'FIRST
1 (s1, s2)';
postgres=# ALTER SYSTEM SET synchronous_standby_names = 'ANY 1
(s1, s2)' ;
postgres=# ALTER SYSTEM SET synchronous_standby_names = 'ANY 2
(s1, s2, s3)';
32. PostgreSQL 10 新特性
6. ID欄位功能(Identity columns )
• Identity 需要與 Serial 互相比較
Taiwan PostgreSQL User Group 322017/8/11
-- 示範Serial:複製 t_serial 資料表至 t_serial2
postgres=# create table t_serial2 (like t_serial including
all);
CREATE TABLE
postgres=# d t_serial2
Table "francs.t_serial2"
Column | Type | Collation | Nullable |
Default
--------+---------+-----------+----------+---------------------
id | integer | | not null |
nextval('t_serial_id_seq'::regclass)
name | text | | |
Indexes:
"t_serial2_pkey" PRIMARY KEY, btree (id)
33. PostgreSQL 10 新特性
6. ID欄位功能(Identity columns )
• Identity 需要與 Serial 互相比較
Taiwan PostgreSQL User Group 332017/8/11
-- 示範Identity:複製 t_identity資料表至 t_identity2
postgres=# create table t_identity2 ( like t_identity
including all);
CREATE TABLE
postgres=# d t_identity2
Table "francs.t_identity2"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+----------------------
------------
id | bigint | | not null | generated by default
as identity
name | text | | |
Indexes:
"t_identity2_pkey" PRIMARY KEY, btree (id)