尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

PG系列:PG数据库中分析操作系统IO是否正常
📅 发布时间:2026/6/20 0:34:18

我们的文章会在微信公众号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)

提供ORACLE技术支持(系统优化,故障处理,安装升级,数据恢复等) TEL:18081072613,微信、QQ同手机号。

相关新闻

  • 极限学习机(ELM)的原理、实现和应用
  • 2025年全封闭超声波清洗机制造企业权威推荐榜单:零排放清洗机/蒸馏回收机/双溶剂清洗机源头厂家精选
  • 胶粘剂行业PLM是什么?一文读懂胶粘剂(粘合剂)PLM系统的功能、价值、解决方案等

最新新闻

  • DeepSeek-V4高效长上下文推理技术解析
  • 技术解析-CPR曲面重建:从血管拉直到三维可视化的核心算法与临床价值
  • S12XS中断系统XINT配置详解:从原理到汽车电子实战
  • 【新】5p229基于python的新能源汽车数据分析系统的设计与实现3(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • MCU系统集成模块(SIM)解析:复位、中断与低功耗设计实战
  • 从零到一:基于JasperGold的FPV实战入门与避坑指南

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号