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

在分析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 查看执行过程资源使用

language 复制代码
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 查看解析过程资源使用

language 复制代码
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过程资源使用

language 复制代码
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

language 复制代码
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)
相关推荐
vvvae12345 小时前
分布式数据库
数据库
雪域迷影5 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
bug菌¹6 小时前
滚雪球学Oracle[4.2讲]:PL/SQL基础语法
数据库·oracle
逸巽散人6 小时前
SQL基础教程
数据库·sql·oracle
月空MoonSky6 小时前
Oracle中TRUNC()函数详解
数据库·sql·oracle
momo小菜pa6 小时前
【MySQL 06】表的增删查改
数据库·mysql
向上的车轮7 小时前
Django学习笔记二:数据库操作详解
数据库·django
编程老船长7 小时前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
Mephisto.java7 小时前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
全栈师8 小时前
SQL Server中关于个性化需求批量删除表的做法
数据库·oracle