PostgreSQL码农集散地

PostgreSQL 18 preview - log_connections 模块化 , 精细记录用户连接阶段信息

PostgreSQL 18 preview - log_connections 模块化 , 精细记录用户连接阶段信息

PostgreSQL 18将log_connections参数仅设置on和off改成了模块化设置:

  • receipt:记录收到连接请求时。
  • authentication:记录身份验证尝试(成功或失败)。
  • authorization:记录授权事件(例如,用户被授予或拒绝访问权限)。
  • setup_durations:记录连接建立和后端设置过程中关键步骤的耗时,以便诊断连接建立缓慢的问题。
  • all:记录所有可用的连接方面(相当于旧的 true)。
  • ""(空字符串):禁用所有连接日志记录(相当于旧的 false)。

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=9219093cab2607f34ac70612a65430a9c519157f

Modularize log_connections output  
author Melanie Plageman <[email protected]>   
Wed, 12 Mar 2025 15:33:01 +0000 (11:33 -0400)  
committer Melanie Plageman <[email protected]>   
Wed, 12 Mar 2025 15:35:21 +0000 (11:35 -0400)  
commit 9219093cab2607f34ac70612a65430a9c519157f  
tree e245b0dc191564005192cc596173187ec595ad3e tree  
parent f554a95379a9adef233d21b1e1e8981a8f5f8de3 commit | diff  
Modularize log_connections output  

Convert the boolean log_connections GUC into a list GUC comprised of the  
connection aspects to log.  

This gives users more control over the volume and kind of connection  
logging.  

The current log_connections options are 'receipt', 'authentication', and  
'authorization'. The empty string disables all connection logging. 'all'
enables all available connection logging.  

For backwards compatibility, the most common values for the  
log_connections boolean are still supported (on, off, 1, 0, true, false,  
yes, no). Note that previously supported substrings of on, off, true,  
false, yes, and no are no longer supported.  

Author: Melanie Plageman <[email protected]>  
Reviewed-by: Bertrand Drouvot <[email protected]>  
Reviewed-by: Fujii Masao <[email protected]>  
Reviewed-by: Daniel Gustafsson <[email protected]>  
Discussion: https://postgr.es/m/flat/CAAKRu_b_smAHK0ZjrnL5GRxnAVWujEXQWpLXYzGbmpcZd3nLYw%40mail.gmail.com  

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=18cd15e706ac1f2d6b1c49847a82774ca143352f

Add connection establishment duration logging  
author Melanie Plageman <[email protected]>   
Wed, 12 Mar 2025 15:33:08 +0000 (11:33 -0400)  
committer Melanie Plageman <[email protected]>   
Wed, 12 Mar 2025 15:35:27 +0000 (11:35 -0400)  
commit 18cd15e706ac1f2d6b1c49847a82774ca143352f  
tree 148f8a5def0fc9ebc2d2ccffa113395b0a4c5aea tree  
parent 9219093cab2607f34ac70612a65430a9c519157f commit | diff  
Add connection establishment duration logging  

Add log_connections option 'setup_durations'which logs durations of  
several key parts of connection establishment and backend setup.  

For an incoming connection, starting from when the postmaster gets a  
socket from accept() and ending when the forked child backend is first  
ready for query, there are multiple steps that could each take longer  
than expected due to external factors. This logging provides visibility  
into authentication and fork duration as well as the end-to-end  
connection establishment and backend initialization time.  

To make this portable, the timings captured in the postmaster (socket  
creation time, fork initiation time) are passed through the  
BackendStartupData.  

Author: Melanie Plageman <[email protected]>  
Reviewed-by: Bertrand Drouvot <[email protected]>  
Reviewed-by: Fujii Masao <[email protected]>  
Reviewed-by: Daniel Gustafsson <[email protected]>  
Reviewed-by: Jacob Champion <[email protected]>  
Reviewed-by: Jelte Fennema-Nio <[email protected]>  
Reviewed-by: Guillaume Lelarge <[email protected]>  
Discussion: https://postgr.es/m/flat/CAAKRu_b_smAHK0ZjrnL5GRxnAVWujEXQWpLXYzGbmpcZd3nLYw%40mail.gmail.com  

AI 解读patch

总览:增强 PostgreSQL 连接日志记录,提供更精细的控制和性能分析能力

这两个补丁共同增强了 PostgreSQL 的连接日志记录功能,旨在提供更精细的控制和更深入的性能分析能力。第一个补丁将 log_connections 配置参数从简单的布尔值转换为一个列表,允许用户选择性地记录连接的不同方面。第二个补丁在此基础上,添加了一个新的日志记录选项,用于记录连接建立过程中的各个阶段的耗时。

补丁 1:模块化 log_connections 输出

  • 核心变更: 将 log_connections 配置参数从布尔值改为列表。
  • 目的: 提供更精细的连接日志记录控制,允许用户选择性地记录特定类型的连接事件,从而减少日志量,专注于特定安全问题,并提高性能。
  • 新选项:
    • receipt:记录收到连接请求时。
    • authentication:记录身份验证尝试(成功或失败)。
    • authorization:记录授权事件(例如,用户被授予或拒绝访问权限)。
    • all:记录所有可用的连接方面(相当于旧的 true)。
    • ""(空字符串):禁用所有连接日志记录(相当于旧的 false)。
  • 向后兼容性: 保留了对旧布尔值的支持(on、off、true、false、yes、no、1、0),但不再支持这些值的子字符串。

补丁 2:添加连接建立时长日志记录

  • 核心变更: 添加了 log_connections 的新选项 setup_durations。
  • 目的: 记录连接建立和后端设置过程中关键步骤的耗时,以便诊断连接建立缓慢的问题。
  • 记录的阶段: 从 postmaster 接收到来自 accept() 的套接字开始,到 fork 后的子后端首次准备好处理查询结束。
  • 记录的内容:
    • 套接字创建时间
    • fork 启动时间
    • 身份验证时长
    • fork 时长
    • 端到端连接建立和后端初始化时间
  • 实现细节: 在 postmaster 中捕获的时间信息(套接字创建时间、fork 启动时间)通过 BackendStartupData 传递给后端进程,以确保跨平台兼容性。

这两个补丁共同提供了一个更强大和灵活的连接日志记录系统。通过第一个补丁,用户可以控制记录哪些类型的连接事件(例如,身份验证、授权)。通过第二个补丁,用户可以深入了解连接建立过程中的性能瓶颈,例如,身份验证耗时过长或 fork 过程缓慢。

使用示例:

  • log_connections = 'authentication,authorization,setup_durations':记录身份验证、授权事件以及连接建立时长。
  • log_connections = 'all':记录所有可用的连接信息,包括连接建立时长。

对用户的影响:

  • 升级注意事项: 如果您依赖于旧的 log_connections 设置,您需要查看您的配置并更新它以使用新的列表语法。
  • 性能分析:setup_durations 选项可以帮助您识别和解决连接建立缓慢的问题。
  • 安全审计: 更精细的日志记录控制可以帮助您更好地进行安全审计。

总结:

这两个补丁共同增强了 PostgreSQL 的连接日志记录功能,使其更加灵活、强大和易于使用。它们为用户提供了更精细的控制和更深入的性能分析能力,从而可以更好地管理和优化 PostgreSQL 数据库。