【PostgreSQL查看SQL执行过程主机的资源使用情况】

2024-01-08 12:30:39

在分析SQL的时候,可以临时设置client_min_messages=log结合如下几个参数分别查看SQL在explain, parser, execute 过程系统资源使用分析。

生产环境不建议开启,建议临时分析会话级别开启,用于定位问题。

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

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)

文章来源:https://blog.csdn.net/weixin_47308871/article/details/135392240
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。