标签
PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持
背景
PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。
parallel seq scan
parallel index scan
parallel index only scan
parallel bitmap scan
parallel filter
parallel hash agg
parallel group agg
parallel cte
parallel subquery
parallel create table
parallel create index
parallel select into
parallel CREATE MATERIALIZED VIEW
parallel 排序 : gather merge
parallel nestloop join
parallel hash join
parallel merge join
parallel 自定义并行聚合
parallel 自定义并行UDF
parallel append
parallel append merge
parallel union all
parallel fdw table scan
parallel partition join
parallel partition agg
parallel gather
parallel gather merge
parallel rc 并行
parallel rr 并行
parallel GPU 并行
parallel unlogged table
lead parallel
接下来进行一一介绍。
关键知识请先自行了解:
1、优化器自动并行度算法 CBO
《PostgreSQL 11 并行计算算法,参数,强制并行度设置》
parallel gather | gathermerge - enable leader worker process
PG并行计算框架中,并行任务由计算进程执行,计算进程执行的结果,由GATHER(leader process)收集再转交下一个节点。
PG有一个开关parallel_leader_participation,允许leader process参与计算任务,即不是空等所有计算进程。
换句话说说,有点像领导和小弟一起干一样的活,所有人活都干完后,领导依旧需要把结果汇总进行下一个环节。
1、parallel_leader_participation设置为ON,表示领导和小弟一起干活,再将结果汇总进行下一步。
2、parallel_leader_participation设置为OFF,表示领导不干小弟的活,而是等所有小弟干完,再将结果汇总进行下一步。
parallel_leader_participation (boolean)
Allows the leader process to execute the query plan under Gather and Gather Merge nodes instead of waiting for worker processes.
The default is on.
Setting this value to off reduces the likelihood that workers will become blocked because the leader is not reading tuples fast enough,
but requires the leader process to wait for worker processes to start up before the first tuples can be produced.
The degree to which the leader can help or hinder performance depends on the plan type, number of workers and query duration.
开启1个并发时,很好观察到这个现象。
parallel_leader_participation设置为ON
42915 digoal 20 0 16.844g 160428 4188 R 100.0 0.0 3:36.42 postgres: postgres postgres [local] EXPLAIN
50013 digoal 20 0 16.829g 144000 2004 R 100.0 0.0 0:19.32 postgres: parallel worker for PID 42915
parallel_leader_participation设置为OFF
50528 digoal 20 0 16.829g 144032 2016 R 100.0 0.0 0:05.26 postgres: parallel worker for PID 42915
数据量:10亿
场景 | 数据量 | 关闭并行 | 开启并行 | 并行度 | 开启并行性能提升倍数 |
---|---|---|---|---|---|
parallel leader process | 10亿 | 186 秒 | 95 秒 | 1 | 2 倍 |
1、关闭并行,耗时: 186 秒。
实际上不是关闭并行,而是关闭parallel_leader_participation
postgres=# set parallel_leader_participation=off;
SET
postgres=# explain analyze select * from (select * from table2 order by i) t where i<1000 limit 1000000;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------
Gather (cost=174402886.28..174415386.28 rows=1000000 width=4) (actual time=184773.400..185470.626 rows=1000000 loops=1)
Workers Planned: 1
Workers Launched: 1
Single Copy: true
-> Limit (cost=174402886.28..174415386.28 rows=1000000 width=4) (actual time=184770.319..185060.134 rows=1000000 loops=1)
-> Sort (cost=174402886.28..176902636.28 rows=999900000 width=4) (actual time=184770.316..184983.480 rows=1000000 loops=1)
Sort Key: table2.i
Worker 0: Sort Method: top-N heapsort Memory: 128466kB
-> Seq Scan on table2 (cost=0.00..16924779.00 rows=999900000 width=4) (actual time=0.084..100683.624 rows=1000000000 loops=1)
Filter: (i < 1000)
Planning Time: 0.421 ms
Execution Time: 185520.880 ms
(12 rows)
2、开启并行,耗时: 95 秒。
postgres=# set parallel_leader_participation=on;
SET
postgres=# explain analyze select * from (select * from table2 order by i) t where i<1000 limit 1000000;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=102160553.98..102180553.98 rows=1000000 width=4) (actual time=95138.752..95489.131 rows=1000000 loops=1)
-> Gather Merge (cost=102160553.98..108042318.69 rows=588176471 width=4) (actual time=95138.750..95412.176 rows=1000000 loops=1)
Workers Planned: 1
Workers Launched: 1
-> Sort (cost=102160553.97..103630995.15 rows=588176471 width=4) (actual time=95135.326..95236.241 rows=500706 loops=2)
Sort Key: table2.i
Sort Method: top-N heapsort Memory: 128531kB
Worker 0: Sort Method: top-N heapsort Memory: 128519kB
-> Parallel Seq Scan on table2 (cost=0.00..11777720.18 rows=588176471 width=4) (actual time=0.015..50319.769 rows=500000000 loops=2)
Filter: (i < 1000)
Planning Time: 0.097 ms
Execution Time: 95545.547 ms
(12 rows)
其他知识
1、优化器自动并行度算法 CBO
《PostgreSQL 11 并行计算算法,参数,强制并行度设置》
2、function, op 识别是否支持parallel
postgres=# select proparallel,proname from pg_proc;
proparallel | proname
-------------+----------------------------------------------
s | boolin
s | boolout
s | byteain
s | byteaout
3、subquery mapreduce unlogged table
对于一些情况,如果期望简化优化器对非常非常复杂的SQL并行优化的负担,可以自己将SQL拆成几段,中间结果使用unlogged table保存,类似mapreduce的思想。unlogged table同样支持parallel 计算。
4、vacuum,垃圾回收并行。
5、dblink 异步调用并行
《PostgreSQL VOPS 向量计算 + DBLINK异步并行 - 单实例 10亿 聚合计算跑进2秒》
《PostgreSQL 相似搜索分布式架构设计与实践 - dblink异步调用与多机并行(远程 游标+记录 UDF实例)》
暂时不允许并行的场景(将来PG会继续扩大支持范围):
1、修改行,锁行,除了create table as , select into, create mview这几个可以使用并行。
2、query 会被中断时,例如cursor , loop in PL/SQL ,因为涉及到中间处理,所以不建议开启并行。
3、paralle unsafe udf ,这种UDF不会并行
4、嵌套并行(udf (内部query并行)),外部调用这个UDF的SQL不会并行。(主要是防止large parallel workers )
5、SSI 隔离级别
参考
https://www.postgresql.org/docs/11/parallel-plans.html
《PostgreSQL 11 并行计算算法,参数,强制并行度设置》
《PostgreSQL 11 preview - 并行计算 增强 汇总》
《PostgreSQL 10 自定义并行计算聚合函数的原理与实践 - (含array_agg合并多个数组为单个一元数组的例子)》