安装pg_top
首先安装libbsd,下载地址https://pkgs.org/
[root@db3 atlasdb]# rpm -ivh libbsd-0.8.3-1.el7.x86_64.rpmwarning: libbsd-0.8.3-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEYPreparing... ################################# [100%]Updating installing...1:libbsd-0.8.3-1.el7 ################################# [100%][root@db3 atlasdb]# rpm -ivh libbsd-devel-0.8.3-1.el7error: open of libbsd-devel-0.8.3-1.el7 failed: No such file or directory[root@db3 atlasdb]# rpm -ivh libbsd-devel-0.8.3-1.el7.x86_64.rpmwarning: libbsd-devel-0.8.3-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEYPreparing... ################################# [100%]Updating installing...1:libbsd-devel-0.8.3-1.el7 ################################# [100%][root@db3 atlasdb]#
切换到数据库用户下并解压文件[root@db3 opt]# su - atlasdbLast login: Tue Jun 7 02:05:24 EDT 2022 on pts/2[atlasdb@db3 ~]$ tar zxvf pg_top-main.tar.gz...pg_top-main/utils.hpg_top-main/version.cpg_top-main/version.h[atlasdb@db3 ~]$ cd pg_top-main[atlasdb@db3 pg_top-main]$ cat INSTALL.rstpg_top======Installation------------Configuring~~~~~~~~~~~::cmake [options] CMakeLists.txtoptions:-DCMAKE_INSTALL_PREFIX=PREFIX Install files in PREFIX. Default is'/usr/local'.-DENABLE_COLOR=0 Default on. Include code that allows for theuse of color in the output display. Use-DENABLE_COLOR=0 if you do not want thisfeature compiled in to the code. The configurescript also recognizes the spelling "colour".Installing~~~~~~~~~~::make installUninstalling~~~~~~~~~~~~::xargs rm < install_manifest.txt[atlasdb@db3 pg_top-main]$ cmake -DCMAKE_INSTALL_PREFIX=/opt/pg_top CMakeLists.txtCMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):Compatibility with CMake < 2.8.12 will be removed from a future version ofCMake.Update the VERSION argument <min> value or use a ...<max> suffix to tellCMake that the project does not need compatibility with older versions.-- The C compiler identification is GNU 4.8.5-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working C compiler: bin/cc - skipped-- Detecting C compile features-- Detecting C compile features - done-- machine - linux-- arch - x86_64-- pg_config --includedir - home/atlasdb/atlasdb/include-- pg_config --libdir - home/atlasdb/atlasdb/lib-- Looking for 4 include files stdlib.h, ..., float.h-- Looking for 4 include files stdlib.h, ..., float.h - found-- Looking for include file string.h-- Looking for include file string.h - found-- Looking for include file strings.h-- Looking for include file strings.h - found-- Looking for include files sys/time.h, time.h-- Looking for include files sys/time.h, time.h - found-- Looking for include file sys/time.h-- Looking for include file sys/time.h - found-- Looking for include file sys/resource.h-- Looking for include file sys/resource.h - found-- Looking for include file unistd.h-- Looking for include file unistd.h - found-- Looking for getopt-- Looking for getopt - found-- Looking for memcpy-- Looking for memcpy - found-- Looking for setpriority-- Looking for setpriority - found-- Looking for sigaction-- Looking for sigaction - found-- Looking for sighold-- Looking for sighold - found-- Looking for sigprocmask-- Looking for sigprocmask - found-- Looking for sigrelse-- Looking for sigrelse - found-- Looking for snprintf-- Looking for snprintf - found-- Looking for strchr-- Looking for strchr - found-- Looking for strerror-- Looking for strerror - found-- Performing Test SIGNAL_RETURN-- Performing Test SIGNAL_RETURN - Failed-- Performing Test TIME_T_DEFINED-- Performing Test TIME_T_DEFINED - Success-- Configuring done-- Generating done-- Build files have been written to: home/atlasdb/pg_top-main编译pg_top[atlasdb@db3 pg_top-main]$ make install[ 7%] Building C object CMakeFiles/pg_top.dir/color.c.o[ 14%] Building C object CMakeFiles/pg_top.dir/commands.c.o[ 21%] Building C object CMakeFiles/pg_top.dir/display.c.o[ 28%] Building C object CMakeFiles/pg_top.dir/getopt.c.o[ 35%] Building C object CMakeFiles/pg_top.dir/screen.c.o[ 42%] Building C object CMakeFiles/pg_top.dir/sprompt.c.o[ 50%] Building C object CMakeFiles/pg_top.dir/pg.c.o[ 57%] Building C object CMakeFiles/pg_top.dir/pg_top.c.o[ 64%] Building C object CMakeFiles/pg_top.dir/utils.c.o[ 71%] Building C object CMakeFiles/pg_top.dir/version.c.o[ 78%] Building C object CMakeFiles/pg_top.dir/machine/m_remote.c.o[ 85%] Building C object CMakeFiles/pg_top.dir/machine/m_common.c.o[ 92%] Building C object CMakeFiles/pg_top.dir/machine/m_linux.c.o[100%] Linking C executable pg_top[100%] Built target pg_topInstall the project...-- Install configuration: ""-- Installing: opt/pg_top/bin/pg_top-- Installing: /opt/pg_top/share/man/man1/pg_top.1[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top --helppg_top monitors a PostgreSQL database cluster.Usage:pg_top [OPTION]... [COUNT]General options:-b, --batch use batch mode-c, --show-command display command name of each process-C, --color-mode turn off color mode-i, --interactive use interactive mode-I, --hide-idle hide idle processes-n, --non-interactive use non-interactive mode-o, --order-field=FIELD select sort order-r, --remote-mode activate remote mode-R display replication stats-s, --set-delay=SECOND set delay between screen updates-T, --show-tags show color tags-V, --version output version information, then exit-x, --set-display=COUNT set maximum number of displaysexit once this number is reached-X display i/o stats-z, --show-username=NAME display only processes owned by givenusername-?, --help show this help, then exitConnection options:-d, --dbname=DBNAME database to connect to-h, --host=HOSTNAME database server host or socket directory-p, --port=PORT database server port-U, --username=USERNAME user name to connect as-W, --password force password prompt, and persistent connection[atlasdb@db3 ~]$
使用示例
使用pg_top查看数据库服务器负载情况
[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top -Wlast pid: 13682; load avg: 0.09, 0.25, 0.21; up 0+03:18:587 processes: 5 other background task(s), 1 idle, 1 activeCPU states: 0.0% user, 0.0% nice, 0.2% system, 99.6% idle, 0.2% iowaitMemory: 169M used, 815M free, 0K shared, 0K buffers, 49M cachedSwap: 201M used, 2663M free, 6972K cached, 96K in, 0K outPID USERNAME SIZE RES STATE XTIME QTIME %CPU LOCKS COMMAND13603 atlasdb 614M 12M active 0:00 0:00 0.2 8 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle1732 608M 260K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: background writer1734 609M 996K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: autovacuum launcher1733 607M 1060K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: walwriter1737 atlasdb 609M 540K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: logical replication launcher1731 608M 612K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: checkpointer13520 atlasdb 610M 1132K idle 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle
查看数据库服务器中数据库进程IO情况
[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top -X -WPassword:last pid: 13875; load avg: 1.21, 0.49, 0.29; up 0+04:21:44 02:47:307 processes: 5 other background task(s), 1 idle, 1 activeCPU states: 0.0% user, 0.0% nice, 83.3% system, 0.0% idle, 16.7% iowaitMemory: 925M used, 59M free, 0K shared, 0K buffers, 20M cachedSwap: 1194M used, 1670M free, 788M cached, 0K in, 0K outPID IOPS IORPS IOWPS READS WRITES COMMAND13801 0 0 0 0B 781B vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle1732 0 0 0 0B 0B vastbase: AtlasDB_Cluster: background writer1734 0 0 0 0B 0B vastbase: AtlasDB_Cluster: autovacuum launcher1733 0 0 0 0B 0B vastbase: AtlasDB_Cluster: walwriter1737 0 0 0 0B 0B vastbase: AtlasDB_Cluster: logical replication launcher1731 0 0 0 0B 0B vastbase: AtlasDB_Cluster: checkpointer13520 0 0 0 0B 0B vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle
显示主备节点复制情况(无主备环境,只显示命令)
[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top -R -WPassword:last pid: 13944; load avg: 0.32, 0.37, 0.27; up 0+04:23:05 02:48:510 processes:CPU states: 0.2% user, 0.0% nice, 0.6% system, 98.6% idle, 0.6% iowaitMemory: 915M used, 70M free, 0K shared, 0K buffers, 23M cachedSwap: 1190M used, 1674M free, 772M cached, 0K in, 0K out
仅显示指定用户的进程情况
[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top -z atlasdb -Wlast pid: 15855; load avg: 0.01, 0.09, 0.21; up 0+04:55:46 03:21:327 processes: 5 other background task(s), 1 active, 1 idle txnCPU states: 1.0% user, 0.0% nice, 0.0% system, 98.0% idle, 1.0% iowaitMemory: 913M used, 71M free, 0K shared, 0K buffers, 787M cachedSwap: 191M used, 2673M free, 9088K cached, 96K in, 0K outPID USERNAME SIZE RES STATE XTIME QTIME %CPU LOCKS COMMAND13520 atlasdb 610M 252M idltxn 9:06 9:06 0.0 1 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle in transaction15855 atlasdb 614M 13M active 0:00 0:00 0.0 8 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle1737 atlasdb 609M 500K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: logical replication launcher
按排序显示进程
[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top -o xtime -Wlast pid: 15215; load avg: 0.02, 0.14, 0.29; up 0+04:43:15 03:09:018 processes: 5 other background task(s), 2 idle, 1 activeCPU states: 1.5% user, 0.0% nice, 0.0% system, 98.5% idle, 0.0% iowaitMemory: 393M used, 591M free, 0K shared, 0K buffers, 256M cachedSwap: 339M used, 2525M free, 11M cached, 0K in, 0K outPID USERNAME SIZE RES STATE XTIME QTIME %CPU LOCKS COMMAND15205 atlasdb 614M 13M active 0:00 0:00 0.0 8 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle14887 atlasdb 614M 9796K idle 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle13520 atlasdb 610M 4744K idle 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: atlasdb vastbase [local] idle1732 608M 102M 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: background writer1734 609M 1000K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: autovacuum launcher1733 607M 1308K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: walwriter1737 atlasdb 609M 424K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: logical replication launcher1731 608M 1368K 0:00 0:00 0.0 0 vastbase: AtlasDB_Cluster: checkpointer
显示一定数量后退出,用于信息记录
[atlasdb@db3 ~]$ opt/pg_top/bin/pg_top -o xtime -x 13 -W
帮助命令
进入pg_top之后,可以按 h 进入帮助页面,可以显示可以使用说明指令。
pg_top version 4.0.0, Copyright (c) 1984 through 2007, William LeFebvreA top users display for PostgreSQLThese single-character commands are available:^L - redraw screen<sp> - update screenA - EXPLAIN ANALYZE (UPDATE/DELETE safe)a - show PostgreSQL activity # 刷新C - toggle the use of colorE - show execution plan (UPDATE/DELETE safe) # 显示执行计划I - show I/O statistics per process (Linux only) # 显示IO信息L - show locks held by a process # 显示进程持有的锁R - show PostgreSQL replication subscriptions # 显示复制信息Q - show current query of a process # 显示当前进程的查询c - toggle the display of process commandsd - change number of displays to show # 更改要显示的显示器数量h or ? - help; show this texti - toggle the displaying of idle processes # 切换idle进程的显示n or # - change number of processes to display # 更改要显示的进程数o - specify sort order (cpu, size, res, xtime, qtime, iops, iorps, iowps, reads, writes, locks, command, flag, rlag, slag, wlag) # 制定顺序排序q - quits - change number of seconds to delay between updates # 更改刷新时间u - display processes for only one user (+ selects all users) # 仅显示一个进程的排序Not all commands are available on all systems.Hit any key to continue:
再根据想要获取的信息,直接输入指令即可,列举几个常用命令。
显示进程的SQL语句 --/opt/pg_top/bin/pg_top -W 之后输入QCurrent query for procpid 13520:delete from t where id <3;Hit any key to continue:显示进程SQL语句的执行计划 --/opt/pg_top/bin/pg_top -W 之后输入ECurrent query plan for procpid 13520:Statement:DELETE FROM T;Query Plan:Delete on t (cost=0.00..267173.87 rows=10050687 width=6)-> Seq Scan on t (cost=0.00..267173.87 rows=10050687 width=6)Hit any key to continue:显示进程对应语句锁持有的锁 --/opt/pg_top/bin/pg_top -W 之后输入LLocks held by procpid 13520:| database | schema | table | index | type | granted--+----------+--------+-------+-------+------------------+--------1 | vastbase | public | t | | RowExclusiveLock | tHit any key to continue:
文章转载自PostgreSQL数据库工作学习随笔,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




