【BCC动态跟踪PostgreSQL】

news/2024/7/9 23:12:12 标签: 数据库, postgresql

BPF Compiler Collection (BCC)是基于eBPF的Linux内核分析、跟踪、网络监控工具。其源码存放于GitCode - 开发者的代码家园
 

image.png


想要监控PostgreSQL数据库的相关SQL需要在编译PostgreSQL的时候开启dtrace。下文主要介绍几个和PostgreSQL相关的工具,其他工具可根据需求自行了解。

1.dbslower:跟踪慢查询,然后打印超出延时的,默认延时是1ms

//终端 1:
[root@localhost tools]# ./dbslower postgres -p 58474

//终端 2:
postgres=# select pg_backend_pid();
 pg_backend_pid
----------------
          58474
(1 row)
postgres=# select pg_sleep(3);
 pg_sleep
----------

(1 row)
postgres=# select pg_sleep(2);
 pg_sleep
----------

(1 row)
postgres=# select pg_sleep(1);
 pg_sleep
----------

(1 row)
postgres=# select pg_sleep(2);
 pg_sleep
----------

(1 row)

/终端1:

[root@localhost tools]# ./dbslower postgres -p 58474
Tracing database queries for pids 58474 slower than 1 ms...
TIME(s)        PID          MS QUERY
1.893949       58474  3004.362 select pg_sleep(3);
8.492827       58474  2002.784 select pg_sleep(2);
23.334969      58474  2004.094 select pg_sleep(2);

image.png

2.dbstat:跟踪PostgreSQL数据库进程查询,并显示查询延时的直方图

//终端1:
[root@localhost tools]# ./dbstat postgres -p 58518

//终端2:
postgres=# select pg_backend_pid();
 pg_backend_pid
----------------
          58518
(1 row)
postgres=# select pg_sleep(0.2);
 pg_sleep
----------

(1 row)
postgres=# select pg_sleep(0.2);
 pg_sleep
----------

(1 row)
postgres=# select pg_sleep(0.01);
 pg_sleep
----------

(1 row)
postgres=# select pg_sleep(2);
 pg_sleep
----------

(1 row)


//ctrl+c 终端1:
[root@localhost tools]# ./dbstat postgres -p 58518
Tracing database queries for pids 58518 slower than 0 ms...
[02:37:57]
     query latency (ms)  : count     distribution
         0 -> 1          : 0        |                                        |
         2 -> 3          : 0        |                                        |
         4 -> 7          : 0        |                                        |
         8 -> 15         : 1        |********************                    |
        16 -> 31         : 0        |                                        |
        32 -> 63         : 0        |                                        |
        64 -> 127        : 0        |                                        |
       128 -> 255        : 2        |****************************************|
       256 -> 511        : 0        |                                        |
       512 -> 1023       : 0        |                                        |
      1024 -> 2047       : 1        |********************                    |

image.png

3.execsnoop:跟踪新进程创建,跟踪exec函数

//终端1:
[root@localhost tools]# ./execsnoop

//终端2:
[postgres@localhost data_uuid]$ psql
psql (11.20)
Type "help" for help.
postgres=# \q
[postgres@localhost data_uuid]$ psql
psql (11.20)
Type "help" for help.
postgres=# \q
[postgres@localhost data_uuid]$ ls
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION   postmaster.opts
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal       postmaster.pid
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact
[postgres@localhost data_uuid]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

//终端1:
[root@localhost tools]# ./execsnoop
PCOMM            PID    PPID   RET ARGS
psql             58579  43545    0 /home/postgres/soft-11-20_uuid/bin/psql
psql             58582  43545    0 /home/postgres/soft-11-20_uuid/bin/psql
ls               58584  43545    0 /bin/ls --color=auto
cat              58585  43545    0 /bin/cat /etc/hosts

image.png

4.filetop:文件读和写的top(文件系统相关)

[root@localhost tools]# ./filetop

02:45:02 loadavg: 0.06 0.10 0.76 2/141 58566

TID     COMM             READS  WRITES R_Kb    W_Kb    T FILE
58566   clear            2      0      8       0       R xterm
58565   filetop          1      0      4       0       R retprobe
58565   filetop          1      0      4       0       R type
24865   irqbalance       3      0      3       0       R stat
24865   irqbalance       3      0      3       0       R interrupts
58565   filetop          2      0      2       0       R loadavg
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity
24865   irqbalance       1      0      1       0       R smp_affinity

image.png


http://www.niftyadmin.cn/n/5308155.html

相关文章

Spring面试整理-Spring概述

Spring是什么? Spring是一个开源框架,它是为了解决企业级应用开发的复杂性而创建的。最初由Rod Johnson在2003年发布,Spring已经成为Java企业级应用最流行的框架之一。 Spring优缺点 Spring的主要优势包括: 轻量级: Spring是一个轻量级的控制反转(IoC)和面向切面编程(A…

【IC前端虚拟项目】MVU FS文档编写与注意事项

【IC前端虚拟项目】数据搬运指令处理模块前端实现虚拟项目说明-CSDN博客 FS文档即模块特性说明文档: FS - Functional Specification(功能规格):"FS" 表示功能规格,它是芯片设计和开发的早期阶段的一个文档。功能规格详细描述了芯片的功能、性能和特性,以及各…

数据库中二进制数据图片的读取并存储为文件C++

插入二进制数据https://blog.csdn.net/wjl990316fddwjl/article/details/135334950我们需要将插入的数据进行读取,本文主要介绍如何C如何将数据库中二进制数据图片进行读取和存储 1、查询数据 sql"select * from t_data"; 2、获得查询结果 MYSQL_RES …

NVIDIA Jetpack6.0DP使用过程中的问题

Jetpack6.0DP是2023年12月才发布, 操作系统使用了ubuntu 22.04, gcc是11.4,版本都很高, 用起来还存在一些问题 无法使用jtop https://forums.developer.nvidia.com/t/jtop-no-longer-works-on-jp-6-0-dp/275215 使用$ sudo -H p…

什么?谁?w (who what)

文章目录 什么?谁?w (who & what)默认的显示不显示标题行简洁模式显示更多信息 什么?谁?w (who & what) w可以认为是加强版的who,果然越简洁越强大,就比如less比more是功能更多的。 w不仅可以显示…

【逆向】C与汇编的关系

程序1 使用 VC6.0 编译如下程序(使用 VC6.0 的原因是该编译器不会对代码进行过多的优化,因此适合逆向入门) // 01.cpp : Defines the entry point for the console application. // # include "stdafx.h"int main(int argc, char…

【JavaEE】Java多线程状态-- 多线程篇(10)

Java 多线程状态 1. 线程一共有几个状态?2. 每种状态表示的含义 & 状态之间的切换条件 1. 线程一共有几个状态? 在 Java 中, 线程的状态主要有新建, 就绪, 运行, 阻塞, 等待和超时等待; 2. 每种状态表示的含义 & 状态之间的切换条件 首先处于新建状态, 当线程调用…

使用Vue3 + Vite创建uni-app项目(Webstorm)

使用Vue3 Vite创建uni-app项目(Webstorm) 参考:前端VUE3Vite UniAPP-- 框架搭建_uniapp vite-CSDN博客 // 参考github.com的库:https://github.com/dcloudio/uni-preset-vue npx degit dcloudio/uni-preset-vue#vite-ts vite-vu…