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

PG系列:PG数据库中分析操作系统IO是否正常

我们的文章会在微信公众号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 ~]# iotopTotal 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/sTID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND4636 be/4 postgres    0.00 B/s   33.53 M/s  0.00 %  0.00 % postgres: checkpointer1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 172 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来写的,因为现在这些小脚本,完全没有必要花心思去自己写了,当然如果大家需要脚本,可以加我微信,我发给大家。

------------------作者介绍-----------------------
姓名:黄廷忠
个人博客: (http://www.htz.pw)
CSDN地址: (https://blog.csdn.net/wwwhtzpw)
博客园地址: (https://www.cnblogs.com/www-htz-pw)

http://www.jsqmd.com/news/36704/

相关文章:

  • 2025年智能控制与计算科学国际学术会议(ICICCS 2025)
  • 【连续六届稳定出版检索,往届见刊后1个月左右完成EI检索】第七届机器人、智能控制与人工智能国际学术会议(RICAI 2025)
  • Vue nextTick 全景指南:一篇吃透 $nextTick 用法与底层达成
  • Intigriti Bug Bytes
  • 098_尚硅谷_经典案例九九乘法表
  • 基于MATLAB的光纤光传播特性仿真
  • 【多名IEEE Fellow,作为主讲嘉宾出席大会】第五届计算机科学、电子信息工程和智能控制技术国际会议(CEI 2025)
  • 差分进化(DE)与灰狼优化(GWO)结合的混合算法(DE-GWO)
  • 2025年广东软件开发外包公司权威推荐榜单:软件开发方案/软件开发测试/教育软件开发源头公司精选
  • 极限学习机(ELM)的原理、实现和应用
  • 安卓开发入门
  • ModelScope 模型一键上线?FunModel 帮你 5 分钟从零到生产
  • 记录WPF 在清单列表设置了UIACESS为true,没有签名的报错“从服务器返回了一个参照”
  • 新手在哪里找预防感冒类公众号排版?
  • 领嵌iLeadE-588网关实现设备联网实现远程控制-工业智能网关
  • 2025年北京中央空调更换铜管维修护理权威推荐榜单:中央空调维修保养/中央空调电控系统改造升级/地源热泵进水维修护理精选
  • 2025年水果月饼批发厂家权威推荐榜单:五仁月饼/榴莲冰皮月饼/华美食品源头厂家精选
  • springboot框架非常简单清晰
  • 智慧医疗新突破:揭秘ModbusRTU转EtherCAT神器
  • 打破工业现场的“物理围墙”,如何让工程师在家也能改程序?
  • 近红外与可见光图像融合的多种方法实现
  • 赛博扫盲(2)
  • 基于PKHV3000系列无源高压衰减棒的应用案例
  • 课程设计
  • 2025年尼龙共挤膜直销厂家权威推荐:五层共挤膜/洁净尼龙多层共挤膜/无菌设备保护套厂家精选
  • 2025年全封闭超声波清洗机制造企业权威推荐榜单:零排放清洗机/蒸馏回收机/双溶剂清洗机源头厂家精选
  • 【RK3568 NPU实战】别再闲置你的NPU!手把手带你用迅为资料跑通Android AI检测Demo,附完整流程与效果
  • 智能守护绿水青山:视频融合平台EasyCVR在森林防火监控中的实战应用
  • 微算法科技(NASDAQ MLGO)将租赁权益证明(LPoS)共识算法与零知识证明结合,实现租赁代币的隐私保护
  • 在 Java 中实现插件化:使用 PF4J 的实战指南