PostgreSQL 18 新增获取进程内存上下文函数: pg_get_process_memory_contexts . 此函数处理显示具有指定进程 ID 的 PostgreSQL 进程的内存上下文的请求。 该函数可用于向后端进程以及辅助进程发送请求。 提供了一种非侵入式的方式来监控 PostgreSQL 进程的内存使用情况,有助于识别内存泄漏、过度分配和其他内存相关的问题,从而提高数据库的稳定性和性能。
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=042a66291b04f473cbc72f95f07438abd75ae3a9
Add function to get memory context stats for processes
author Daniel Gustafsson <dgustafsson@postgresql.org>
Tue, 8 Apr 2025 09:06:56 +0000 (11:06 +0200)
committer Daniel Gustafsson <dgustafsson@postgresql.org>
Tue, 8 Apr 2025 09:06:56 +0000 (11:06 +0200)
commit 042a66291b04f473cbc72f95f07438abd75ae3a9
tree 730314170d4743e6dc62a128c4f3ff2f17797595 tree
parent 15f0cb26b530b6725a37391738cfc62d4745c49b commit | diff
Add function to get memory context stats for processes
This adds a functionfor retrieving memory context statistics
and information from backends as well as auxiliary processes.
The intended usecase is cluster debugging when under memory
pressure or unanticipated memory usage characteristics.
When calling the function it sends a signal to the specified
process to submit statistics regarding its memory contexts
into dynamic shared memory. Each memory context is returned
in detail, followed by a cumulative total incase the number
of contexts exceed the max allocated amount of shared memory.
Each process is limited to use at most 1Mb memory for this.
A summary can also be explicitly requested by the user, this
will return the TopMemoryContext and a cumulative total of
all lower contexts.
In order to not block on busy processes the caller specifies
the number of seconds during which to retry before timing out.
In the casewhere no statistics are published within the set
timeout, the last known statistics are returned, or NULL if
no previously published statistics exist. This allows dash-
board type queries to continually publish even if the target
process is temporarily congested. Context records contain a
timestamp to indicate when they were submitted.
Author: Rahila Syed <rahilasyed90@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Reviewed-by: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Reviewed-by: Fujii Masao <masao.fujii@oss.nttdata.com>
Reviewed-by: Alexander Korotkov <aekorotkov@gmail.com>
Discussion: https://postgr.es/m/CAH2L28v8mc9HDt8QoSJ8TRmKau_8FM_HKS41NeO9-6ZAkuZKXw@mail.gmail.com
新增函数如下
https://www.postgresql.org/docs/devel/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL
函数: pg_get_process_memory_contexts ( pid integer, summary boolean, timeout float ) → setof record ( name text, ident text, type text, path integer[], level integer, total_bytes bigint, total_nblocks bigint, free_bytes bigint, free_chunks bigint, used_bytes bigint, num_agg_contexts integer, stats_timestamp timestamptz )
功能:
此函数处理显示具有指定进程 ID 的 PostgreSQL 进程的内存上下文的请求。 该函数可用于向后端进程以及辅助进程发送请求。
返回记录包含每个内存上下文的扩展统计信息:
参数 summary 的作用:
当 summary 为 true 时,将显示级别 1 和 2 的内存上下文的统计信息,其中级别 1 表示根节点(即 TopMemoryContext)。 级别 2 及以下级别的上下文的统计信息是所有子上下文统计信息的聚合,其中 num_agg_contexts 指示聚合的子上下文的数量。 当 summary 为 false 时,num_agg_contexts 值为 1,表示正在显示单个统计信息。 级别限制为前 100 个上下文。
参数 timeout 的作用:
繁忙的进程可能会延迟报告内存上下文统计信息,timeout 指定等待更新统计信息的秒数。 timeout 可以指定为秒的小数部分。
函数返回值:
在收到目标进程的内存上下文统计信息后,它将结果作为每个上下文一行返回。 如果所有上下文都无法容纳在预先确定的尺寸限制内,则剩余的上下文统计信息将被聚合并显示累积总计。 num_agg_contexts 列指示显示的统计信息中聚合的上下文的数量。 当 num_agg_contexts 为 1 时,表示上下文统计信息是单独显示的。
这个补丁增加了一个新功能,允许用户获取数据库进程的内存上下文统计信息。
核心内容:
总结:
这个补丁引入了一个强大的调试工具,允许管理员和开发者深入了解数据库进程的内存使用情况。 通过非阻塞的超时机制和两种统计信息模式,用户可以灵活地获取所需的内存信息,从而更好地诊断和解决内存相关的问题。 该功能对于集群环境下的内存管理和性能优化尤为重要。
更详细的解释:
总而言之,这个补丁提供了一种非侵入式的方式来监控 PostgreSQL 进程的内存使用情况,有助于识别内存泄漏、过度分配和其他内存相关的问题,从而提高数据库的稳定性和性能。