PostgreSQL 11 preview - 分区表智能并行聚合、分组计算(已类似MPP架构,性能暴增)

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
RDS PostgreSQL Serverless,0.5-4RCU 50GB 3个月
推荐场景:
对影评进行热评分析
简介:

标签

PostgreSQL , 分区 , 智能聚合 , 智能分组计算 , enable_partition_wise_agg


背景

PostgreSQL 并行计算开始在细节方面进行打磨,例如11已添加了JOIN的分区并行,当两个分区表的分区定义一致时,在分区字段上JOIN就可以用到分区与分区之间直接并行JOIN,而不需要将数据APPEND后在JOIN。

《PostgreSQL 11 preview - 分区表智能并行JOIN (已类似MPP架构,性能暴增)》

现在又一个PATCH要提交了,还是和分区表有关,这次是以分区为单位的聚合、分组计算。

实际上也蛮好理解的,分两种情况,

1、一种是多阶段聚合,原理如下:(常用在MPP数据库,目前PG的单表多阶段并行聚合也是这么做的)

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

2、第二种是智能分区并行聚合,这种情况与第一种不矛盾,但是在非多阶段聚合时也可能能起到优化作用,比如说根据分区字段进行分组聚合,那么分区与分区之间实际上是没有计算集合的重叠的,各自算各自的。

Hi all,  
  
Declarative partitioning is supported in PostgreSQL 10 and work is already  
in  
progress to support partition-wise joins. Here is a proposal for  
partition-wise  
aggregation/grouping.  Our initial performance measurement has shown 7 times  
performance when partitions are on foreign servers and approximately 15%  
when  
partitions are local.  
  
Partition-wise aggregation/grouping computes aggregates for each partition  
separately.  If the group clause contains the partition key, all the rows  
belonging to a given group come from one partition, thus allowing aggregates  
to be computed completely for each partition.  Otherwise, partial aggregates  
computed for each partition are combined across the partitions to produce  
the  
final aggregates. This technique improves performance because:  
i. When partitions are located on foreign server, we can push down the  
aggregate to the foreign server.  
ii. If hash table for each partition fits in memory, but that for the whole  
relation does not, each partition-wise aggregate can use an in-memory hash  
table.  
iii. Aggregation at the level of partitions can exploit properties of  
partitions like indexes, their storage etc.  
  
Attached an experimental patch for the same based on the partition-wise join  
patches posted in [1].  
  
This patch currently implements partition-wise aggregation when group clause  
contains the partitioning key.  A query below, involving a partitioned table  
with 3 partitions containing 1M rows each, producing total 30 groups showed  
15% improvement over non-partition-wise aggregation. Same query showed 7  
times  
improvement when the partitions were located on the foreign servers.  
Patch needs a lot of improvement including:  
1. Support for partial partition-wise aggregation  
2. Estimating number of groups for every partition  
3. Estimating cost of partition-wise aggregation based on sample partitions  
similar to partition-wise join  
and much more.  
  
In order to support partial aggregation on foreign partitions, we need  
support  
to fetch partially aggregated results from the foreign server. That can be  
handled as a separate follow-on patch.  
  
Though is lot of work to be done, I would like to get suggestions/opinions  
from  
hackers.  
  
I would like to thank Ashutosh Bapat for providing a draft patch and helping  
me off-list on this feature while he is busy working on partition-wise join  
feature.  
  
[1]  
https://www.postgresql.org/message-id/CAFjFpRcbY2QN3cfeMTzVEoyF5Lfku-ijyNR%3DPbXj1e%3D9a%3DqMoQ%40mail.gmail.com  
  
Thanks  
  
--   
Jeevan Chalke  
Principal Software Engineer, Product Development  
EnterpriseDB Corporation  
The Enterprise PostgreSQL Company  

这里有两个例子:

开启分区智能聚合,各个分区聚合后再APPEND结果。

Here is the sample plan:  
  
postgres=# set enable_partition_wise_agg to true;  
SET  
postgres=# EXPLAIN ANALYZE SELECT a, count(*) FROM plt1 GROUP BY a;  
                                                  QUERY  
PLAN  
--------------------------------------------------------------------------------------------------------------  
 Append  (cost=5100.00..61518.90 rows=30 width=12) (actual  
time=324.837..944.804 rows=30 loops=1)  
   ->  Foreign Scan  (cost=5100.00..20506.30 rows=10 width=12) (actual  
time=324.837..324.838 rows=10 loops=1)  
         Relations: Aggregate on (public.fplt1_p1 plt1)  
   ->  Foreign Scan  (cost=5100.00..20506.30 rows=10 width=12) (actual  
time=309.954..309.956 rows=10 loops=1)  
         Relations: Aggregate on (public.fplt1_p2 plt1)  
   ->  Foreign Scan  (cost=5100.00..20506.30 rows=10 width=12) (actual  
time=310.002..310.004 rows=10 loops=1)  
         Relations: Aggregate on (public.fplt1_p3 plt1)  
 Planning time: 0.370 ms  
 Execution time: 945.384 ms  
(9 rows)  

关闭分区智能聚合,需要将数据汇聚后,再进行聚合操作。

postgres=# set enable_partition_wise_agg to false;  
SET  
postgres=# EXPLAIN ANALYZE SELECT a, count(*) FROM plt1 GROUP BY a;  
                                                              QUERY  
PLAN  
---------------------------------------------------------------------------------------------------------------------------------------  
 HashAggregate  (cost=121518.01..121518.31 rows=30 width=12) (actual  
time=6498.452..6498.459 rows=30 loops=1)  
   Group Key: plt1.a  
   ->  Append  (cost=0.00..106518.00 rows=3000001 width=4) (actual  
time=0.595..5769.592 rows=3000000 loops=1)  
         ->  Seq Scan on plt1  (cost=0.00..0.00 rows=1 width=4) (actual  
time=0.007..0.007 rows=0 loops=1)  
         ->  Foreign Scan on fplt1_p1  (cost=100.00..35506.00 rows=1000000  
width=4) (actual time=0.587..1844.506 rows=1000000 loops=1)  
         ->  Foreign Scan on fplt1_p2  (cost=100.00..35506.00 rows=1000000  
width=4) (actual time=0.384..1839.633 rows=1000000 loops=1)  
         ->  Foreign Scan on fplt1_p3  (cost=100.00..35506.00 rows=1000000  
width=4) (actual time=0.402..1876.505 rows=1000000 loops=1)  
 Planning time: 0.251 ms  
 Execution time: 6499.018 ms  
(9 rows)  

场景

1、枚举、哈希分区表:

因为在不同分区中,分区字段值非常鲜明分离。可以按分区字段聚合,按分区字段GROUP BY。

2、范围分区表:

某些按分区字段GROUP BY(看GROUP BY的表达式是否有优化空间)

3、继承表,根据子表约束,选择是否可以使用分区智能聚合、GROUP。

参考

https://www.postgresql.org/message-id/flat/CAM2+6=V64_xhstVHie0Rz=KPEQnLJMZt_e314P0jaT_oJ9MR8A@mail.gmail.com#CAM2+6=V64_xhstVHie0Rz=KPEQnLJMZt_e314P0jaT_oJ9MR8A@mail.gmail.com

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

相关实践学习
阿里云百炼xAnalyticDB PostgreSQL构建AIGC应用
通过该实验体验在阿里云百炼中构建企业专属知识库构建及应用全流程。同时体验使用ADB-PG向量检索引擎提供专属安全存储,保障企业数据隐私安全。
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
目录
相关文章
|
1月前
|
机器学习/深度学习 弹性计算 人工智能
阿里云服务器ECS架构区别及选择参考:X86计算、ARM计算等架构介绍
在我们选购阿里云服务器的时候,云服务器架构有X86计算、ARM计算、GPU/FPGA/ASIC、弹性裸金属服务器、高性能计算可选,有的用户并不清楚他们之间有何区别,本文主要简单介绍下这些架构各自的主要性能及适用场景,以便大家了解不同类型的架构有何不同,主要特点及适用场景有哪些。
148 10
|
1月前
|
存储 人工智能 运维
面向AI的服务器计算软硬件架构实践和创新
阿里云在新一代通用计算服务器设计中,针对处理器核心数迅速增长(2024年超100核)、超多核心带来的业务和硬件挑战、网络IO与CPU性能增速不匹配、服务器物理机型复杂等问题,推出了磐久F系列通用计算服务器。该系列服务器采用单路设计减少爆炸半径,优化散热支持600瓦TDP,并实现CIPU节点比例灵活配比及部件模块化可插拔设计,提升运维效率和客户响应速度。此外,还介绍了面向AI的服务器架构挑战与软硬件结合创新,包括内存墙问题、板级工程能力挑战以及AI Infra 2.0服务器的开放架构特点。最后,探讨了大模型高效推理中的显存优化和量化压缩技术,旨在降低部署成本并提高系统效率。
|
2月前
|
存储 机器学习/深度学习 人工智能
【AI系统】计算图优化架构
本文介绍了推理引擎转换中的图优化模块,涵盖算子融合、布局转换、算子替换及内存优化等技术,旨在提升模型推理效率。计算图优化技术通过减少计算冗余、提高计算效率和减少内存占用,显著改善模型在资源受限设备上的运行表现。文中详细探讨了离线优化模块面临的挑战及解决方案,包括结构冗余、精度冗余、算法冗余和读写冗余的处理方法。此外,文章还介绍了ONNX Runtime的图优化机制及其在实际应用中的实现,展示了如何通过图优化提高模型推理性能的具体示例。
76 4
【AI系统】计算图优化架构
|
2月前
|
机器学习/深度学习 人工智能 API
【AI系统】昇腾异构计算架构 CANN
本文介绍了昇腾 AI 异构计算架构 CANN,涵盖硬件层面的达·芬奇架构和软件层面的全栈支持,旨在提供高性能神经网络计算所需的硬件基础和软件环境。通过多层级架构,CANN 实现了高效的 AI 应用开发与性能优化,支持多种主流 AI 框架,并提供丰富的开发工具和接口,助力开发者快速构建和优化神经网络模型。
78 1
|
3月前
|
机器学习/深度学习 弹性计算 人工智能
阿里云服务器架构有啥区别?X86计算、Arm、GPU异构、裸金属和高性能计算对比
阿里云ECS涵盖x86、ARM、GPU/FPGA/ASIC、弹性裸金属及高性能计算等多种架构。x86架构采用Intel/AMD处理器,适用于广泛企业级应用;ARM架构低功耗,适合容器与微服务;GPU/FPGA/ASIC专为AI、图形处理设计;弹性裸金属提供物理机性能;高性能计算则针对大规模并行计算优化。
153 7
|
3月前
|
运维 Serverless 数据处理
Serverless架构通过提供更快的研发交付速度、降低成本、简化运维、优化资源利用、提供自动扩展能力、支持实时数据处理和快速原型开发等优势,为图像处理等计算密集型应用提供了一个高效、灵活且成本效益高的解决方案。
Serverless架构通过提供更快的研发交付速度、降低成本、简化运维、优化资源利用、提供自动扩展能力、支持实时数据处理和快速原型开发等优势,为图像处理等计算密集型应用提供了一个高效、灵活且成本效益高的解决方案。
122 1
|
3月前
|
运维 监控 Serverless
Serverless架构在图像处理等计算密集型应用中展现了显著的优势
Serverless架构在图像处理等计算密集型应用中展现了显著的优势
55 1
|
3天前
|
关系型数据库 MySQL 网络安全
如何排查和解决PHP连接数据库MYSQL失败写锁的问题
通过本文的介绍,您可以系统地了解如何排查和解决PHP连接MySQL数据库失败及写锁问题。通过检查配置、确保服务启动、调整防火墙设置和用户权限,以及识别和解决长时间运行的事务和死锁问题,可以有效地保障应用的稳定运行。
44 25
|
12天前
|
关系型数据库 MySQL 数据库
Docker Compose V2 安装常用数据库MySQL+Mongo
以上内容涵盖了使用 Docker Compose 安装和管理 MySQL 和 MongoDB 的详细步骤,希望对您有所帮助。
91 42
|
25天前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版