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

磐维数据库如何将慢SQL记录到日志文件中

原创 武铮 2025-10-28
53

一、查询日志文件位置

首先需要查询日志文件的位置,可以通过以下方式查询到

1、在主机上通过gs_guc命令查询

[omm@node ~]$ gs_guc check -N all -c "log_directory"
The pw_guc run with the following arguments: [gs_guc -N all -c log_directory check ].

Total GUC values: 3. Failed GUC values: 0.
The details for log_directory:
[node] log_directory='/database/panweidb/log/omm/pg_log/dn_6001'
[panweidb86] log_directory='/database/panweidb/log/omm/pg_log/dn_6002' 
[panweidb87] log_directory='/database/panweidb/log/omm/pg_log/dn_6003'

2、在数据库内通过show命令查询

postgres=# show log_directory;
              log_directory
------------------------------------------
 /database/panweidb/log/omm/pg_log/dn_6001
(1 row)

3、可以查询慢日志文件

[omm@node ~]$ cd /database/panweidb/log/omm/pg_log/dn_6001
[omm@node dn_6001]$ ll
total 476K
-r-------- 1 omm dbgrp 7.3K Nov 12 17:34 postgresql-2025-10-27_233004.log.gz
-rw------- 1 omm dbgrp 88K Nov 12 17:51 postgresql-2025-10-28_000000.log

二、相关参数

在了解慢日志存放的位置后,可以开启记录慢SQL的相关参数,主要有3个:

2.1 log_duration(是否记录慢SQL)

postgres=# show log_duration ;
 log_duration 
--------------
 on
(1 row)

2.2 log_min_duration_statement(定义慢SQL的阈值,超过多少毫秒记录)

postgres=# show log_min_duration_statement;
 log_min_duration_statement 
----------------------------
 1s
(1 row)

2.3 logging_collector(收集日志到文件中)

postgres=# show logging_collector ;
 logging_collector 
-------------------
 on
(1 row)

三、示例

最后可以通过测试演示下如何在日志文件中查到慢SQL:

会话1:

postgres=# select c1 ,sleep(2) from pw01 ;
 c1 | sleep 
----+-------
  1 |     0
  2 |     0
  3 |     0
 99 |     0
(4 rows)

会话2:

[omm@node dn_6001]$ tail -100f postgresql-2025-10-28_172843.log
2025-10-28 17:29:54.757 69008c45.3060 postgres 47416791992064 gsql 0 dn_6001 00000  7036874417766499 [BACKEND] LOG:  duration: 8009.49
8 ms queryid 7036874417766499 unique id 3579428019 statement: select c1 ,sleep(2) from pw01 ;

也可以通过grep方式过滤duration关键字获取所有的慢SQL日志

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

评论