暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

[译] PostgreSQL 18:更细粒度的 log_connections

原创 刺史武都 2025-03-18
204

许多客户因为审计要求而启用log_connections功能。这是一个简单的布尔值,要么启用,要么禁用。一旦启用并激活,PostgreSQL数据库的每个新连接都会被记录到PostgreSQL日志文件中。在PostgreSQL 17及之前的版本中,日志文件中记录的连接信息通常如下所示:

2025-03-13 08:50:05.607 CET - 1 - 6195 - [local] - [unknown]@[unknown] - 0LOG: connection received: host=[local]
2025-03-13 08:50:05.607 CET - 2 - 6195 - [local] - postgres@postgres - 0LOG: connection authenticated: user="postgres" method=trust (/u02/pgdata/17/pg_hba.conf:117)
2025-03-13 08:50:05.607 CET - 3 - 6195 - [local] - postgres@postgres - 0LOG: connection authorized: user=postgres database=postgres application_name=psql

如你所见,这里记录了三个阶段:连接接收、身份验证和授权。通过比较记录的时间戳,你可以了解每个阶段完成所需的时间。然而,如果连接数量很多,这会在日志文件中产生大量的记录。

在PostgreSQL 18中,这种情况将发生变化。log_connections不再是一个简单的布尔值,而是一个支持多种值的列表。有效选项包括:

receipt
authentication
authorization
[空字符串]
从这个列表中,你可以看出发生了什么变化。现在,你可以选择仅启用特定阶段的日志记录,而不需要一次性启用所有阶段(如果你不需要的话)。空字符串则禁用连接日志记录。

例如,如果你只对授权阶段感兴趣,你可以这样配置:

postgres@pgbox:/home/postgres/ [pgdev] psql
psql (18devel)
输入"help"获取帮助。

postgres=# select version();
version
-----------------------------------------------
PostgreSQL 18devel on x86_64-linux, compiled by gcc-14.2.1, 64-bit
(1行)

postgres=# alter system set log_connections = 'authorization';
ALTER SYSTEM
postgres=# select pg_reload_conf();
pg_reload_conf
----------------
t
(1行)

从现在开始,只有“授权”阶段会被记录到日志文件中:

2025-03-13 09:10:41.710 CET - 1 - 6617 - [local] - postgres@postgres - 0LOG: connection authorized: user=postgres database=postgres application_name=psql

如果你只对这个阶段感兴趣,这将大大减少日志记录的数量。如果你想恢复到之前的日志行为,可以将所有阶段重新启用:

postgres=# alter system set log_connections = 'authorization','receipt','authentication';
ALTER SYSTEM
postgres=# select pg_reload_conf();
pg_reload_conf
----------------
t
(1行)

启用此设置后,日志内容将与之前完全一致:

2025-03-13 09:14:19.520 CET - 1 - 6629 - [local] - [unknown]@[unknown] - 0LOG: connection received: host=[local]
2025-03-13 09:14:19.521 CET - 2 - 6629 - [local] - postgres@postgres - 0LOG: connection authenticated: user="postgres" method=trust (/u02/pgdata/PGDEV/pg_hba.conf:117)
2025-03-13 09:14:19.521 CET - 3 - 6629 - [local] - postgres@postgres - 0LOG: connection authorized: user=postgres database=postgres application_name=psql

原文地址:https://www.dbi-services.com/blog/postgresql-18-more-granular-log_connections/
原文作者:Daniel Westermann

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论