PostgreSQL 并行计算解说 之26 - parallel gather | gathermerge - enable leader worker process

简介: 标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan parallel index scan parallel index only scan

标签

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 9.6 并行计算 优化器算法浅析》

《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 9.6 并行计算 优化器算法浅析》

《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实例)》

《PostgreSQL dblink异步调用实现 并行hash分片JOIN - 含数据交、并、差 提速案例 - 含dblink VS pg 11 parallel hash join VS pg 11 智能分区JOIN》

暂时不允许并行的场景(将来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合并多个数组为单个一元数组的例子)》

《PostgreSQL 9.6 并行计算 优化器算法浅析》

 

免费领取阿里云RDS PostgreSQL实例、ECS虚拟机

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
SQL 存储 Oracle
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
4334 0
|
SQL 存储 弹性计算
PostgreSQL 分页, offset, 返回顺序, 扫描方法原理(seqscan, index scan, index only scan, bitmap scan, parallel xx scan),游标
标签 PostgreSQL , 数据离散性 , 扫描性能 , 重复扫 , bitmap index scan , 排序扫描 , 扫描方法 , 顺序 背景 一个这样的问题: 为什么select x from tbl offset x limit x; 两次查询连续的OFFSET,会有重复数据呢? select ctid,* from tbl where ... offset 0 li
2482 0
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之13 - parallel OLAP : 中间结果 parallel with unlogged table
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan parallel
880 0
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之14 - parallel index scan
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan paral
1542 0
|
SQL 分布式计算 并行计算
PostgreSQL 并行计算解说 之15 - parallel bitmap scan
标签 PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持 背景 PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。 parallel seq scan
1714 0
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
812 158
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
2112 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1440 156

推荐镜像

更多