40
PostgreSQL 10 New Feature @ COSCUP 2017 Taiwan PostgreSQL User Group 林宗禧

PostgreSQL 10 New Features

Embed Size (px)

Citation preview

Page 1: PostgreSQL 10 New Features

PostgreSQL 10 New Feature

@ COSCUP 2017

Taiwan PostgreSQL User Group

林宗禧

Page 2: PostgreSQL 10 New Features

Outline

• About Me

• History: PostgreSQL前世今生

• PostgreSQL 10 新特性

• PostgreSQL Ecosystem

• 台灣PostgreSQL使用者社群

2Taiwan PostgreSQL User Group2017/8/11

Page 3: PostgreSQL 10 New Features

About Me

• 我是林宗禧 (José Lin)– 自Hadoop/HBase入門OSS

• 工作在研究分散式資料庫系統– 熟PG與MongoDB

– 接觸多種NoSQL/NewSQL

• 成為PostgreSQL愛好者– '13年起拜訪國內PG愛好者、認識日本JPUG

– '17年起著手PG社群 (感謝許多PG前輩& JPUG幫忙!!)

2017/8/11 3Taiwan PostgreSQL User Group

Page 4: PostgreSQL 10 New Features

History: PostgreSQL前世今生

1973

Ingres

1985

Post-Ingres

Postgres

1994

Postgres95

42017/8/11

•與Eugene Wong在UCBerkeley共同開發類似於IBM System R 的DB。•免費散佈Ingres代碼,1980年止發行了1000份。

基於Ingres商業化:•1984年 Informix •1980-90年 Sybase

Michael Stonebraker

•1985年著手“後-Ingres”計畫,稱為 “Postgres”

基於Postgres商業化:•1990年成立Illustra,後被Informix 併購基於Ingres商業化:•1987年 NonStop SQL•1992年 Sybase 賣給微軟變為Microsoft SQL Server

Andrew Yu & Jolly Chen

•1994年Andrew Yu和Jolly Chen在UCBerkeley,增加SQL直譯器,發布Postgres95。

•1996年計畫改名為PostgreSQL。

Page 5: PostgreSQL 10 New Features

History: PostgreSQL前世今生

1996

PostgreSQL 6

2000

PostgreSQL 7

2005

PostgreSQL 8

52017/8/11

•PostgreSQL第一次發行,版本為6.0。•透過Internet組成一組來自世界各地的開發者。

•2001年Command Prompt, Inc.釋出Mammoth PostgreSQL套件並且贊助支援社群。

基於Postgres商業化:•2000年Red Hat投資籌組Great Bridge公司來商業化PostgreSQL。•2001年因市場狀況不佳Great Bridge倒閉。

•2005年11月昇陽宣布支援PostgreSQL。•2006年6月Solaris 10包含PostgreSQL一起發布。•8.0版在效能、管理、為運已達24/7水準。基於Postgres商業化:•2005年EnterpriseDB (移轉Oracle)、 Greenplum(資料倉儲、商業智慧) 投入商業化。

1999年6月JPUG成立 台灣建置大型PG案例

Page 6: PostgreSQL 10 New Features

History: PostgreSQL前世今生

2010

PostgreSQL 9

2011~2016

PostgreSQL9.1~9.6

2017

PostgreSQL10

Taiwan PostgreSQL User Group 62017/8/11

•Streaming Replication•Hot Standby•64-bit Windows

•FDW•GiST index•JSON、JSONB•Materialized Views•UPSERT

基於Postgres商業化:•2015年Pivotal開源Greenplum。

•10之後的版號不會有第三碼,下次版號為11

2017年6月TWPUG成立

Page 7: PostgreSQL 10 New Features

PostgreSQL 10 新特性

1. 資料表分割強化 (Table Partitioning)2. 邏輯複製(Logical Replication)3. 平行查詢強化 (Parallel Queries)4. FDW強化(Additional FDW Push-Down)5. 多節點同步寫入 (Quorum Commit)6. ID欄位功能(Identity columns )7. 安全認證提升(SCRAM-SHA-256 Authentication)8. 多欄位關聯(Multi-column Correlation Statistics)9. 全文檢索支持 JSON 和 JSONB10. 新增 pg_hba_file_rules 項目11. 新增 pg_stat_activity 監控項目12. 新增 pg_sequence 系統表13. Row層級的安全政策(Row-level security)14. Schema 預設權限(Default permissions on schemas)

Taiwan PostgreSQL User Group 72017/8/11

Page 8: PostgreSQL 10 New Features

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. 建立 function3. 建立 Trigger

Page 9: PostgreSQL 10 New Features

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;

Page 10: PostgreSQL 10 New Features

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)

Page 11: PostgreSQL 10 New Features

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)

Page 12: PostgreSQL 10 New Features

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

Page 13: PostgreSQL 10 New Features

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

Page 14: PostgreSQL 10 New Features

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)

※主表不會查

Page 15: PostgreSQL 10 New Features

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

Page 16: PostgreSQL 10 New Features

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)

※主表不會查

Page 17: PostgreSQL 10 New Features

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

Page 18: PostgreSQL 10 New Features

PostgreSQL 10 新特性

1. 資料表分割強化 (Table Partitioning)

Taiwan PostgreSQL User Group 182017/8/11

33.65 22.255.85

185.75

488.9

172 171

0 0

40.23.5 4.72

133.5 133.8

傳統方式 PG10

單位:秒

Page 19: PostgreSQL 10 New Features

PostgreSQL 10 新特性

2. 邏輯複製(Logical Replication)

• 由2ndQuadrant回饋的功能

• PG9.0-9.6:Streaming Replication

– WAL Replication

• PG10:Logical Replication

– Table 層級顆粒細度 (以前需用Londiste3 實現)

Taiwan PostgreSQL User Group 192017/8/11

Page 20: PostgreSQL 10 New Features

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

Page 21: PostgreSQL 10 New Features

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

Page 22: PostgreSQL 10 New Features

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

Page 23: PostgreSQL 10 New Features

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)

Page 24: PostgreSQL 10 New Features

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 ;

Page 25: PostgreSQL 10 New Features

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)

Page 26: PostgreSQL 10 New Features

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 ;

Page 27: PostgreSQL 10 New Features

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倍

Page 28: PostgreSQL 10 New Features

PostgreSQL 10 新特性

4. FDW強化(Additional FDW Push-Down)

• PG9.1/9.2 FDW:SELECT

• PG9.3 FDW:INSERT/UPDATE/DELETE

• PG9.6 FDW:JOINS / SORTS

Taiwan PostgreSQL User Group 282017/8/11

PG

FDW

TBL

Foreign Server/Data SourceFTBL

Page 29: PostgreSQL 10 New Features

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,0009.6 Time: 441.758 ms10 Time: 19.928 ms

Page 30: PostgreSQL 10 New Features

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

Page 31: PostgreSQL 10 New Features

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)';

Page 32: PostgreSQL 10 New Features

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)

Page 33: PostgreSQL 10 New Features

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)

Page 34: PostgreSQL 10 New Features

PostgreSQL 10 新特性

7. 安全認證提升(SCRAM-SHA-256 Authentication)

• SCRAM-SHA-256 提供比 MD5 更安全的密碼驗證功能:

– 降低連線重複率 (連線超過64K,MD5會產生機率50% 的重複。)

– 被偷取的hashed密碼更難被重複使用

– 更難使用暴力破解法

Taiwan PostgreSQL User Group 342017/8/11

Page 35: PostgreSQL 10 New Features

PostgreSQL 10 新特性

其他:

• 多欄位關聯(Multi-column Correlation Statistics)

• 全文檢索支持 JSON 和 JSONB

• 新增 pg_hba_file_rules 項目

• 新增 pg_stat_activity 監控項目

• 新增 pg_sequence 系統表

• Row層級的安全政策(Row-level security)

• Schema 預設權限(Default permissions on schemas)

Taiwan PostgreSQL User Group 352017/8/11

Page 36: PostgreSQL 10 New Features

PostgreSQL 10 新特性

• 參考資料/來源:

– http://www.postgres.cn/news/viewone/1/247

– http://francs3.blog.163.com/blog/

– https://momjian.us/main/writings/pgsql/features.pdf

Taiwan PostgreSQL User Group 362017/8/11

Page 37: PostgreSQL 10 New Features

PostgreSQL Ecosystem

• Slony-I / Pgpool-II (multiple replication)

• Pglogical / Postgres-BDR (logical replication)

• Postgres-XC / XL / X2 (Distributed)

• PgBouncer ( pooling )

• Barman (backup / recovery)

• PG-Storm (GPU computing)

• PgAdmin / PhpPgAdmin / PgStudio… (GUI)

• Lots of FDW tools…

Taiwan PostgreSQL User Group 372017/8/11

Page 38: PostgreSQL 10 New Features

台灣PostgreSQL使用者社群

• Github: pgsql-tw

• FB: @pgsqlTaiwan(from 郭朝益先生)

Taiwan PostgreSQL User Group 382017/8/11

Page 39: PostgreSQL 10 New Features

台灣PostgreSQL使用者社群

• 為何要成立PostgreSQL台灣使用者社群?

– 惦惦吃三碗公

– 愛好者有點難找

– 高手藏身在民間

• 這個社群可以做什麼?

– 彼此交流、經驗分享

– 共同研究、合作開創新專案

– 或是任何跟PostgreSQL有關的都可以~

Taiwan PostgreSQL User Group 392017/8/11

Page 40: PostgreSQL 10 New Features

Thank you.歡迎加入台灣PostgreSQL使用者社群

Github : pgsql-tw

Website : pgsql-tw.github.io

Facebook : @pgsqlTaiwan

Taiwan PostgreSQL User Group 402017/8/11