当前位置: 首页 > ORACLE > 正文

我们的文章会在微信公众号IT民工的龙马人生博客网站( www.htz.pw )同步更新 ,欢迎关注收藏,也欢迎大家转载,但是请在文章开始地方标注文章出处,谢谢!
由于博客中有大量代码,通过页面浏览效果更佳。

今天在自己ESXI环境中测试一个PG功能时遇到IO非常慢的文件,想从数据库中去分析和统计IO的性能,但是PG数据库中暂时没有像Oracle的10046这样的事件,无法非常直观和简单的去分析和统计IO性能问题,所以变相的就转到操作系统层面去分析了,所以这里将自己的分析过程和思路写出来。

1,操作系统定位TOP IO进程

这里可以通过从操作系统的iotop命令去定位top的io进程,也可以通过数据库pg_stat_activity去定位pid,建议是通过pg_stat_activity根据自己业务跑的sql语句定位当前的pid,相关的命令及脚本如下:

[root@pgc ~]# iotop

Total DISK READ :       0.00 B/s | Total DISK WRITE :      33.53 M/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s
    TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND
   4636 be/4 postgres    0.00 B/s   33.53 M/s  0.00 %  0.00 % postgres: checkpointer
      1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 17
      2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
      3 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_gp]
      4 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_par_gp]
      5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [slub_flushwq]
      7 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H-events_highpri]
     10 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [mm_percpu_wq]
     11 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_tasks_rude_]
     12 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_tasks_trace]
     13 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
     14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
     15 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     16 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
     17 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [cpuhp/0]

iotop命令默认最小化Linux环境是没有安装的,如果没有安装,可以通过python模拟写的iotop脚本来实现,如下所示:

[root@pgc ~]# python3 iotop
TIME                   PID     USER  READ KB/s  WRITE KB/s COMMAND
2025-07-28 06:56:11   4747 postgres       2.00        2.00 postgres: postgres postgres [local] idle in transaction
2025-07-28 06:56:11   4636 postgres       0.00    62074.00 postgres: checkpointer
2025-07-28 06:56:11   4638 postgres       0.00        4.00 postgres: walwriter

2,统计进程IO信息

这里是统计每一秒进程的IO统计信息,这个值只能作为参考,因为平均后的值无法用于定位每次IO是否正常

[root@pgc ~]# pidstat -d -p 1794 4
Linux 4.18.0-553.el8_10.x86_64 (pgc)    07/28/2025      _x86_64_        (10 CPU)

05:22:21 AM   UID       PID   kB_rd/s   kB_wr/s kB_ccwr/s iodelay  Command
05:22:25 AM   601      1794      0.00      0.00      0.00       0  postgres
05:22:29 AM   601      1794      0.00      0.00      0.00       0  postgres
05:22:33 AM   601      1794      0.00      0.00      0.00       0  postgres
05:22:37 AM   601      1794    256.00   2328.00      0.00       0  postgres
05:22:41 AM   601      1794   4192.00  41794.00      0.00       0  postgres
05:22:45 AM   601      1794   4128.00  41370.00      0.00       0  postgres
05:22:49 AM   601      1794      0.00      0.00      0.00       0  postgres
05:22:53 AM   601      1794      0.00      0.00      0.00       0  postgres
05:22:57 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:01 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:05 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:09 AM   601      1794   3296.00  32600.00      0.00       0  postgres
05:23:13 AM   601      1794   4224.00  42374.00      0.00       0  postgres
05:23:17 AM   601      1794   1056.00  10518.00      0.00       0  postgres
05:23:21 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:25 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:29 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:33 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:37 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:41 AM   601      1794     96.00    728.00      0.00       0  postgres
05:23:45 AM   601      1794   4320.00  43118.00      0.00       0  postgres
05:23:49 AM   601      1794   4160.00  41714.00      0.00       0  postgres
05:23:53 AM   601      1794      0.00      0.00      0.00       0  postgres
05:23:57 AM   601      1794      0.00      0.00      0.00       0  postgres
05:24:01 AM   601      1794      0.00      0.00      0.00       0  postgres

3,统计每次的响应事件

这里通过io_count.py的脚本来统计进程每次io的响应时间,确定不存在io抖动现象的存在,同时需要将io响应时间与物理磁盘io能达到的io性能做对比,物理磁盘io性能如果不知道,请与存储工程师或者查阅本次磁盘的说明书,不同磁盘的性能差异现在是太大了,没有任何可比性,比如我在我自己的MacOS的nvme与ESXI上面的SATA SSD的IO性能指标相差很多倍。

[postgres@pgc ~]$ python3 io_count.py -h
usage: io_count.py [-h] -p PID -t TIME [-f FILE]

Trace and analyze IO syscalls.

optional arguments:
  -h, --help            show this help message and exit
  -p PID, --pid PID     Target process ID
  -t TIME, --time TIME  strace duration in seconds
  -f FILE, --file FILE  strace output file
[postgres@pgc ~]$ python3 io_count.py -p 4747 -t 100 -f /tmp/4747.log
2025-07-28 06:34:17 : Script started.
2025-07-28 06:34:17 : Start strace on PID 4747, duration: 100s, output: /tmp/4747.log
strace: Process 4747 attached
strace: Process 4747 detached
2025-07-28 06:35:57 : Strace process terminated. Starting log analysis...
2025-07-28 06:36:00 : Log analysis completed. Report:
2025-07-28 06:36:00 : IO Type: pread64
Range       Count   Total(ms)   Avg(ms)   Percent   
0-0.1ms     946421  45339.46    0.05      99.34%
0.1-0.5ms   2441    116.94      0.05      0.26%
0.5-1ms     491     23.52       0.05      0.05%
1-2ms       1791    85.80       0.05      0.19%
2-3ms       236     11.31       0.05      0.02%
3-4ms       170     8.14        0.05      0.02%
4-5ms       109     5.22        0.05      0.01%
5-8ms       267     12.79       0.05      0.03%
8-15ms      284     13.61       0.05      0.03%
15-30ms     377     18.06       0.05      0.04%
>30ms       124     5.94        0.05      0.01%
------------------------------------------------------------
2025-07-28 06:36:00 : IO Type: pwrite64
Range       Count   Total(ms)   Avg(ms)   Percent   
0-0.1ms     897     20.99       0.02      100.00%
------------------------------------------------------------
2025-07-28 06:36:00 : Script finished.

4,总结

上面只是分析自己分析IO的一个思路,其中用到的几个脚本都是用AI来写的,因为现在这些小脚本,完全没有必要花心思去自己写了,当然如果大家需要脚本,可以加我微信,我发给大家。

——————作者介绍———————–
姓名:黄廷忠
现就职:Oracle中国高级服务团队
曾就职:OceanBase、云和恩墨、东方龙马等
电话、微信、QQ:18081072613
个人博客: (http://www.htz.pw)
CSDN地址: (https://blog.csdn.net/wwwhtzpw)
博客园地址: (https://www.cnblogs.com/www-htz-pw)


PG系列:PG数据库中分析操作系统IO是否正常:等您坐沙发呢!

发表评论

gravatar

? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen

快捷键:Ctrl+Enter