暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL 并行计算解说 之15 - parallel bitmap scan

digoal 2019-03-17
628

作者

digoal

日期

2019-03-17

标签

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 union

parallel fdw table scan

parallel partition join

parallel partition agg

parallel gather

parallel gather merge

parallel rc 并行

parallel rr 并行

parallel GPU 并行

parallel unlogged table
```

接下来进行一一介绍。

关键知识请先自行了解:

1、优化器自动并行度算法 CBO

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

《PostgreSQL 11 并行计算算法,参数,强制并行度设置》

parallel bitmap scan

并行索引位图扫描

数据量:10亿。

create unlogged table table5 (i int, c1 int); insert into table5 select i, random()*100 from generate_series(1,1000000000) t(i); vacuum analyze table5; set max_parallel_maintenance_workers=32; create index idx_table5_1 on table5(c1); alter table table5 set (parallel_workers =64);

场景 | 数据量 | 关闭并行 | 开启并行 | 并行度 | 开启并行性能提升倍数
---|---|---|---|---|---
parallel bitmap scan | 10 亿 | 23.98 秒 | 15.86 秒 | 20 | 1.5 倍

1、关闭并行,耗时: 23.98 秒。

```
postgres=# explain select avg(i) from table5 where c1=1 and i<1000000;
QUERY PLAN


Aggregate (cost=4674865.61..4674865.62 rows=1 width=32)
-> Bitmap Heap Scan on table5 (cost=103061.21..4674840.21 rows=10158 width=4)
Recheck Cond: (c1 = 1)
Filter: (i < 1000000)
-> Bitmap Index Scan on idx_table5_1 (cost=0.00..103058.68 rows=9800000 width=0)
Index Cond: (c1 = 1)
(6 rows)

postgres=# select avg(i) from table5 where c1=1 and i<1000000;
avg


501131.321720902376
(1 row)

Time: 23979.690 ms (00:23.980)
```

2、开启并行,耗时: 15.86 秒。

```
postgres=# explain select avg(i) from table5 where c1=1 and i<1000000;
QUERY PLAN


Finalize Aggregate (cost=4535191.55..4535191.56 rows=1 width=32)
-> Gather (cost=4535191.48..4535191.49 rows=20 width=32)
Workers Planned: 20
-> Partial Aggregate (cost=4535191.48..4535191.49 rows=1 width=32)
-> Parallel Bitmap Heap Scan on table5 (cost=103061.21..4535190.21 rows=508 width=4)
Recheck Cond: (c1 = 1)
Filter: (i < 1000000)
-> Bitmap Index Scan on idx_table5_1 (cost=0.00..103058.68 rows=9800000 width=0)
Index Cond: (c1 = 1)
(9 rows)

postgres=# select avg(i) from table5 where c1=1 and i<1000000;
avg


501131.321720902376
(1 row)

Time: 15862.531 ms (00:15.863)
```

位图扫描的index scan阶段没有使用并行,heap scan阶段开始使用并行,所以总体效率没有index scan高。

为什么bitmap index scan阶段没有并行呢?要从BITMAP INDEX SCAN的原理说起,bitmap scan的index scan部分实际上是要从index中得到满足条件的tuple的heap block id。并给block id排序,顺序返回。然后再进入heap scan阶段,heap scan阶段,按上个阶段拿到的block id顺序扫描,并对条件进行recheck。所以block i顺序是关键。

如果要让bitmap index scan阶段支持并行,那么应该在index scan阶段上面再加一个gather merge, 用于合并bitmap index scan parallel worker process 返回的 block ids。

parallel bitmap heap scan gather merge (block ids) -> 加这个步骤 parallel bit map index scan (return block ids) -> 支持并行

bitmap scan适合离散度非常高,并且离散IO的成本很高的场景。因为bitmap scan的heap scan是按HEAP顺序扫的,同时heap scan阶段需要对index condition进行recheck,所以bitmap scan比index scan更耗费CPU。

其他知识

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

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论