在分析SQL的时候,可以临时设置client_min_messages=log结合如下几个参数分别查看SQL在explain, parser, execute 过程系统资源使用分析。
生产环境不建议开启,建议临时分析会话级别开启,用于定位问题。并且需要对SQL类型评估,DML类不建议使用,因为会更改生产数据,查询类SQL需先评估是否会影响当前的业务。(数据量较大的时候,部分select操作可能会影响负载,影响磁盘的io,从而影响业务)
postgres=# select name from pg_settings where name like '%stats';
name
---------------------
log_executor_stats
log_parser_stats
log_planner_stats
log_statement_stats
(4 rows)
一、log_executor_stats 查看执行过程资源使用
postgres=# set client_min_messages =log;
SET
postgres=# set log_executor_stats=on;
SET
postgres=# select 1;
LOG: EXECUTOR STATISTICS
DETAIL: ! system usage stats:
! 0.000000 s user, 0.000011 s system, 0.000010 s elapsed
! [0.000000 s user, 0.002140 s system total]
! 4216 kB max resident size
! 0/0 [0/0] filesystem blocks in/out
! 0/2 [0/915] page faults/reclaims, 0 [0] swaps
! 0 [0] signals rcvd, 0/0 [0/0] messages rcvd/sent
! 0/0 [4/0] voluntary/involuntary context switches
?column?
----------
1
(1 row)
二、log_parser_stats 查看解析过程资源使用
postgres=# set client_min_messages =log;
SET
postgres=# set log_parser_stats=on;
SET
postgres=# select 1;
LOG: PARSER STATISTICS
DETAIL: ! system usage stats:
! 0.000017 s user, 0.000017 s system, 0.000032 s elapsed
! [0.000934 s user, 0.000934 s system total]
! 3692 kB max resident size
! 0/0 [0/0] filesystem blocks in/out
! 0/17 [0/819] page faults/reclaims, 0 [0] swaps
! 0 [0] signals rcvd, 0/0 [0/0] messages rcvd/sent
! 0/0 [3/0] voluntary/involuntary context switches
LOG: PARSE ANALYSIS STATISTICS
DETAIL: ! system usage stats:
! 0.000013 s user, 0.000013 s system, 0.000026 s elapsed
! [0.000983 s user, 0.000983 s system total]
! 3948 kB max resident size
! 0/0 [0/0] filesystem blocks in/out
! 0/19 [0/854] page faults/reclaims, 0 [0] swaps
! 0 [0] signals rcvd, 0/0 [0/0] messages rcvd/sent
! 0/0 [3/0] voluntary/involuntary context switches
LOG: REWRITER STATISTICS
DETAIL: ! system usage stats:
! 0.000003 s user, 0.000003 s system, 0.000005 s elapsed
! [0.000994 s user, 0.000994 s system total]
! 3948 kB max resident size
! 0/0 [0/0] filesystem blocks in/out
! 0/4 [0/858] page faults/reclaims, 0 [0] swaps
! 0 [0] signals rcvd, 0/0 [0/0] messages rcvd/sent
! 0/0 [3/0] voluntary/involuntary context switches
?column?
----------
1
(1 row)
三、log_planner_stats查看PLANNER过程资源使用
postgres=# set client_min_messages =log;
SET
postgres=# set log_planner_stats=on;
SET
postgres=# select 1;
LOG: PLANNER STATISTICS
DETAIL: ! system usage stats:
! 0.000000 s user, 0.000098 s system, 0.000097 s elapsed
! [0.000000 s user, 0.002105 s system total]
! 3952 kB max resident size
! 0/0 [0/0] filesystem blocks in/out
! 0/53 [0/895] page faults/reclaims, 0 [0] swaps
! 0 [0] signals rcvd, 0/0 [0/0] messages rcvd/sent
! 0/0 [3/0] voluntary/involuntary context switches
?column?
----------
1
(1 row)
四、log_statement_stats报告总的语句统计。
其它的报告针每个模块的统计。log_statement_stats不能和 其它任何针对每个模块统计的选项一起启用。
postgres=# set client_min_messages =log;
SET
postgres=# set log_statement_stats=on;
SET
postgres=# select 1;
LOG: QUERY STATISTICS
DETAIL: ! system usage stats:
! 0.000000 s user, 0.000249 s system, 0.000250 s elapsed
! [0.000000 s user, 0.002201 s system total]
! 4216 kB max resident size
! 0/0 [0/0] filesystem blocks in/out
! 0/114 [0/916] page faults/reclaims, 0 [0] swaps
! 0 [0] signals rcvd, 0/0 [0/0] messages rcvd/sent
! 0/0 [3/0] voluntary/involuntary context switches
?column?
----------
1
(1 row)
最后修改时间:2024-07-11 09:55:50
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




