PostgreSQL码农集散地

PostgreSQL 19 preview - 增加逻辑解析工作进程超内存限制计数器

PostgreSQL 19 preview - 增加逻辑解析工作进程超内存限制计数器

PostgreSQL 19 引入mem_exceeded_count计数器, 记录logical decoding超过logical_decoding_work_mem内存限制次数.

https://github.com/postgres/postgres/commit/d3b6183dd988928dd369b4b7d641917e77f1ae4e

Add mem_exceeded_count column to pg_stat_replication_slots.  
author Masahiko Sawada <[email protected]>   
Wed, 8 Oct 2025 17:05:04 +0000 (10:05 -0700)  
committer Masahiko Sawada <[email protected]>   
Wed, 8 Oct 2025 17:05:04 +0000 (10:05 -0700)  
commit d3b6183dd988928dd369b4b7d641917e77f1ae4e  
tree 021b43628b3e43871984121ba75c4a41b8992720 tree  
parent 14ad0d7bf2b8d5f26061df7cbdf18cfffcdcb225 commit | diff  
Add mem_exceeded_count column to pg_stat_replication_slots.  

This commit introduces a new column mem_exceeded_count to the  
pg_stat_replication_slots view. This counter tracks how often the  
memory used by logical decoding exceeds the logical_decoding_work_mem  
limit. The new statistic helps users determine whether exceeding the  
logical_decoding_work_mem limit is a rare occurrences or a frequent  
issue, information that wasn't available through existing statistics.  

Bumps catversion.  

Author: Bertrand Drouvot <[email protected]>  
Reviewed-by: Masahiko Sawada <[email protected]>  
Reviewed-by: Amit Kapila <[email protected]>  
Reviewed-by: shveta malik <[email protected]>  
Reviewed-by: Ashutosh Bapat <[email protected]>  
Reviewed-by: Chao Li <[email protected]>  
Discussion: https://postgr.es/m/[email protected]  

下面看一下AI如何解读该patch:

一、什么是 logical_decoding_work_mem?

logical_decoding_work_mem 是 PostgreSQL 的一个参数,主要用于 逻辑解码(logical decoding) 过程。逻辑解码是实现流式数据变更捕获(Change Data Capture, CDC)、逻辑复制(Logical Replication)等功能的核心机制。它能够将 WAL(Write-Ahead Log,预写日志)里的变更解码为用户可读的数据格式,比如发送到订阅者或外部系统。

参数作用:

  • logical_decoding_work_mem 限制单个解码进程在处理变更时可用的内存量(以MB为单位)。
  • 当解码时内存使用超过这个限制,部分中间状态会被写入磁盘(spill),以保证系统不会因解码过程而耗尽内存。

相关场景:

  • 例如大量的变更事件需要解码时,内存可能会暴涨。合理设置这个参数,有利于系统稳定运行,但设置过小会导致频繁写磁盘,影响性能。

二、解读该 Patch

Commit 链接

d3b6183dd988928dd369b4b7d641917e77f1ae4e

主要内容

  • 在 pg_stat_replication_slots 视图中新增 mem_exceeded_count 字段。
  • 该字段统计逻辑解码过程中内存超出 logical_decoding_work_mem 限制的次数。
  • 这个统计有助于用户了解内存溢出(spill)是偶发还是高频,便于优化参数设置和系统性能。

影响

  • 用户可以更精准地监控和分析逻辑复制、变更数据捕获的内存行为。
  • 如果 mem_exceeded_count 频繁增长,说明当前 logical_decoding_work_mem 设置过小,建议调大以减少磁盘I/O。
  • 以往 PostgreSQL 没有暴露这种指标,难以判断 spill 的实际发生频率。现在运维和开发者可以据此优化配置和排查性能瓶颈。

典型应用场景

  • 逻辑复制/同步:例如生产环境有大量订阅者,观察该指标可以帮助调优复制性能。
  • 数据变更流处理:如用 Debezium、pgoutput 等工具采集变更,监控该指标能提升整体数据链路的稳定性和效率。

三、总结

  • logical_decoding_work_mem 是限制逻辑解码内存的参数,防止单进程消耗过多内存。
  • 该 patch 新增统计字段 mem_exceeded_count,让用户可以直观了解逻辑解码溢出(spill)行为,优化参数和性能。
  • 这是 PostgreSQL 19 在 CDC/逻辑复制运维方面的重要可观测性增强。

如需进一步了解该字段的实际作用,可以查询 pg_stat_replication_slots 视图,查看每个 slot 的 mem_exceeded_count 数值,并结合业务量和参数调优。