PostgreSQL 11 preview - 多阶段并行聚合array_agg, string_agg

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , string_agg , array_agg


背景

并行聚合原理请参考:

《PostgreSQL 10 自定义并行计算聚合函数的原理与实践 - (含array_agg合并多个数组为单个一元数组的例子)》

实际上PostgreSQL支持并行计算后,聚合就分为多阶段聚合与原始的一阶段聚合两种玩法。

多阶段聚合会将聚合任务分配给所有的WORKER执行,然后再将聚合的中间结果合并。

postgres=# explain select count(id) from generate_series(1,100) id;  
                                      QUERY PLAN                                         
---------------------------------------------------------------------------------------  
 Gather  (cost=1012.50..1012.61 rows=1 width=8)  
   Workers Planned: 1  
   Single Copy: true  
   ->  Aggregate  (cost=12.50..12.51 rows=1 width=8)  
         ->  Function Scan on generate_series id  (cost=0.00..10.00 rows=1000 width=4)  
(5 rows)  

而单阶段聚合,并行不可能出现在聚合节点,聚合一定要等前面节点完成所有工作后才能开始。

postgres=# explain select string_agg(id::text,',') from t_only;  
                                  QUERY PLAN                                    
------------------------------------------------------------------------------  
 Gather  (cost=230056.09..230056.20 rows=1 width=32)  
   Workers Planned: 1  
   Single Copy: true  
   ->  Aggregate  (cost=229056.09..229056.10 rows=1 width=32)  
         ->  Seq Scan on t_only  (cost=0.00..154055.62 rows=10000062 width=4)  
(5 rows)  
  
postgres=# explain select array_agg(id) from t_only;  
                                  QUERY PLAN                                    
------------------------------------------------------------------------------  
 Gather  (cost=180055.78..180055.89 rows=1 width=32)  
   Workers Planned: 1  
   Single Copy: true  
   ->  Aggregate  (cost=179055.78..179055.79 rows=1 width=32)  
         ->  Seq Scan on t_only  (cost=0.00..154055.62 rows=10000062 width=4)  
(5 rows)  

多阶段聚合需要满足一个条件,即可以通过中间结果合并出最终的聚合结果,在定义聚合时,指定合并用到的combinefunction。

在MPP数据库中,为了达到高性能的效果,大部分聚合都是多阶段聚合。如果是一阶段聚合,效果是不好的。

《HybridDB PostgreSQL "Sort、Group、distinct 聚合、JOIN" 不惧怕数据倾斜的黑科技和原理 - 多阶段聚合》

《Postgres-XC customized aggregate introduction》

《Greenplum 最佳实践 - 估值插件hll的使用(以及hll分式聚合函数优化)》

在pipelinedb分布式流计算中,对于估值聚合,也使用了多阶段聚合,原因是这些估值类型都支持合并。因此能够让WORKER并行计算,最后合并WORKER的聚合中间结果,达到并行目的。

以下patch针对array_agg, string_agg实现了combinefunction,可以支持多阶段并行聚合。

Hi,  
  
While working on partial aggregation a few years ago, I didn't really  
think it was worthwhile allowing partial aggregation of string_agg and  
array_agg. I soon realised that I was wrong about that and allowing  
parallelisation of these aggregates still could be very useful when  
many rows are filtered out during the scan.  
  
Some benchmarks that I've done locally show that parallel string_agg  
and array_agg do actually perform better, despite the fact that the  
aggregate state grows linearly with each aggregated item. Obviously,  
the performance will get even better when workers are filtering out  
rows before aggregation takes place, so it seems worthwhile doing  
this. However, the main reason that I'm motivated to do this is that  
there are more uses for partial aggregation other than just parallel  
aggregation, and it seems a shame to disable all these features if a  
single aggregate does not support partial mode.  
  
I've attached a patch which implements all this. I've had most of it  
stashed away for a while now, but I managed to get some time this  
weekend to get it into a more completed state.  
  
Things are now looking pretty good for the number of aggregates that  
support partial mode.  
  
Just a handful of aggregates now don't support partial aggregation;  
  
postgres=# select aggfnoid from pg_aggregate where aggcombinefn=0 and  
aggkind='n';  
     aggfnoid  
------------------  
 xmlagg  
 json_agg  
 json_object_agg  
 jsonb_agg  
 jsonb_object_agg  
(5 rows)  
  
... and a good number do support it;  
  
postgres=# select count(*) from pg_aggregate where aggcombinefn<>0 and  
aggkind='n';  
 count  
-------  
   122  
(1 row)  
  
There's probably no reason why the last 5 of those couldn't be done  
either, it might just require shifting a bit more work into the final  
functions, although, I'm not planning on that for this patch.  
  
As for the patch; there's a bit of a quirk in the implementation of  
string_agg. We previously always threw away the delimiter that belongs  
to the first aggregated value, but we do now need to keep that around  
so we can put it in between two states in the combine function. I  
decided the path of least resistance to do this was just to borrow  
StringInfo's cursor variable to use as a pointer to the state of the  
first value and put the first delimiter before that. Both the  
string_agg(text) and string_agg(bytea) already have a final function,  
so we just need to skip over the bytes up until the cursor position to  
get rid of the first delimiter. I could go and invent some new state  
type to do the same, but I don't really see the trouble with what I've  
done with StringInfo, but I'll certainly listen if someone else thinks  
this is wrong.  
  
Another thing that I might review later about this is seeing about  
getting rid of some of the code duplication between  
array_agg_array_combine and accumArrayResultArr.  
  
I'm going to add this to PG11's final commitfest rather than the  
January 'fest as it seems more like a final commitfest type of patch.  
  
--   
 David Rowley                   http://www.2ndQuadrant.com/  
 PostgreSQL Development, 24x7 Support, Training & Services  

参考

https://commitfest.postgresql.org/17/1468/

《PostgreSQL 10 自定义并行计算聚合函数的原理与实践 - (含array_agg合并多个数组为单个一元数组的例子)》

https://www.postgresql.org/message-id/flat/CAKJS1f9sx_6GTcvd6TMuZnNtCh0VhBzhX6FZqw17TgVFH-ga_A@mail.gmail.com#CAKJS1f9sx_6GTcvd6TMuZnNtCh0VhBzhX6FZqw17TgVFH-ga_A@mail.gmail.com

《HybridDB PostgreSQL "Sort、Group、distinct 聚合、JOIN" 不惧怕数据倾斜的黑科技和原理 - 多阶段聚合》

《Postgres-XC customized aggregate introduction》

《Greenplum 最佳实践 - 估值插件hll的使用(以及hll分式聚合函数优化)》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
5月前
|
消息中间件 存储 关系型数据库
PostgreSQL技术大讲堂 - 第33讲:并行查询管理
PostgreSQL从小白到专家,技术大讲堂 - 第33讲:并行查询管理
289 1
|
4月前
|
关系型数据库 MySQL 分布式数据库
PolarDB MySQL版并行查询技术探索与实践
PolarDB MySQL版并行查询技术探索与实践 PolarDB MySQL版在企业级查询加速特性上进行了深度技术探索,其中并行查询作为其重要组成部分,已经在线稳定运行多年,持续演进。本文将详细介绍并行查询的背景、挑战、方案、特性以及实践。
108 2
|
4月前
|
SQL 关系型数据库 分布式数据库
深度解析PolarDB数据库并行查询技术
深度解析PolarDB数据库并行查询技术:加速SQL执行的关键问题和核心技术 随着数据规模的不断扩大,用户SQL的执行时间越来越长,这不仅对数据库的优化能力提出更高的要求,并且对数据库的执行模式也提出了新的挑战。为了解决这个问题,许多数据库系统,包括Oracle、SQL Server等,都开始提供并行查询引擎的支持,以充分利用系统资源,达到加速SQL执行的效果。本文将深入探讨基于代价进行并行优化、并行执行的云数据库的并行查询引擎的关键问题和核心技术。
123 2
|
9月前
|
关系型数据库 大数据 PostgreSQL
PostgreSQL16-新特性-并行聚合
PostgreSQL16-新特性-并行聚合
102 0
|
存储 SQL 人工智能
PolarDB 弹性并行查询(ePQ)功能使用白皮书
1 法律声明阿里云提醒您在阅读或使用本文档之前仔细阅读、充分理解本法律声明各条款的内容。如果您 阅读或使用本文档,您的阅读或使用行为将被视为对本声明全部内容的认可。您应当通过阿里云网站或阿里云提供的其他授权通道下载、获取本文档,且仅能用于自身的 合法合规的业务活动。本文档的内容视为阿里云的保密信息,您应当严格遵守保密义务; 未经 阿里云事先书面同意,您不得向任何第三方披露本手册内容或提供给任何第三
452 0
PolarDB 弹性并行查询(ePQ)功能使用白皮书
|
存储 SQL Cloud Native
基于 PolarDB for MySQL 实现并行创建索引赛题解析 | 学习笔记
快速学习基于 PolarDB for MySQL 实现并行创建索引赛题解析
174 0
基于 PolarDB for MySQL 实现并行创建索引赛题解析 | 学习笔记
|
存储 Cloud Native 关系型数据库
PolarDB MySQL 弹性多机并行深度剖析
背景并行查询(Parallel Query)是自PolarDB MySQL诞生伊始就致力于研发的企业级查询加速功能,这与PolarDB的产品定位密切相关,基于云原生的计算存储分离使底层数据量远突破单机容量的限制,而针对更海量数据的复杂分析、报表类业务也成为用户自然而然的需求,同时由于PolarDB是服务于在线业务(OLTP)的关系数据库系统,用户会希望分析业务能具有"在线"的能
823 0
PolarDB MySQL 弹性多机并行深度剖析
|
存储 SQL Cloud Native
PolarDB 并行查询的前世今生
本文会深入介绍PolarDB MySQL在并行查询这一企业级查询加速特性上做的技术探索、形态演进和相关组件的实现原理,所涉及功能随PolarDB MySQL 8.0.2版本上线。
969 2
PolarDB 并行查询的前世今生
|
存储 SQL Cloud Native
PolarDB 并行查询的前世今生
本文会深入介绍PolarDB MySQL在并行查询这一企业级查询加速特性上做的技术探索、形态演进和相关组件的实现原理,所涉及功能随PolarDB MySQL 8.0.2版本上线。
PolarDB 并行查询的前世今生
|
存储 SQL Cloud Native
PolarDB 并行查询深入剖析
本文会深入介绍PolarDB MySQL在并行查询这一企业级查询加速特性上做的技术探索、形态演进和相关组件的实现原理,所涉及功能随PolarDB MySQL 8.0.2版本上线。背景PolarDB云的兴起为古老而顽固的数据库市场带来了新的发展机遇,据Gartner预测,到 2022 年,所有数据库中将有 75% 部署或迁移到云平台,云原生数据库的诞生为各个数据库厂商、云服务供应商提供了弯道超车的绝
590 0

相关产品

  • 云原生数据库 PolarDB