PostgreSQLスキルアップノート(自己啓発のための個人サイト)

psコマンドによるPostgreSQLの状況監視


【一覧に戻る】


━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
■■■■ PostgreSQL スキルアップノート
■■■■
■◆■■ psコマンドによるPostgreSQLの状況監視
■■■■
■■■■
■■■■ 2012/11/16
■■■■ 使用環境:PostgreSQL9.1.4 (CentOS6.2)
                                                                   (C) 2012 ohdb
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
★自己都合により実機確認は9.1、マニュアルへのリンクは9.2としています。ご了承下さい。


【マニュアル】
・データベース活動状況の監視→●[マニュアル]データベース活動状況の監視−標準的なUnixツール


【参考記事】
なし





■1■ psコマンドによるPostgreSQLプロセスの確認
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

この項はpsコマンドオプション指定方法の覚えです。本題は次項以降にて。



1.基本形 postgreSQLマニュアル記載の方法
──────────────────────────────

【□】 ps auxw | grep ^postgres



2.見出しつき grepで消えてしまう見出しも拾っておく。
──────────────────────────────


【□】 ps auxw | egrep "^postgres|^USER"

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres  2595  0.0  0.4 672720 33216 ?        S    00:06   0:00 /usr/local/pgsql/bin/postgres
postgres  2596  0.0  0.0 111124   752 ?        Ss   00:06   0:00 postgres: logger process
postgres  2600  0.0  0.0 672980  4860 ?        Ss   00:06   0:00 postgres: writer process
postgres  2601  0.0  0.0 672980   984 ?        Ss   00:06   0:00 postgres: wal writer process
postgres  2602  0.0  0.0 673688  2228 ?        Ss   00:06   0:00 postgres: autovacuum launcher process
postgres  2603  0.0  0.0 113336   952 ?        Ss   00:06   0:00 postgres: archiver process   last was 00000001000000010000001B
postgres  2604  0.0  0.0 113336   968 ?        Ss   00:06   0:00 postgres: stats collector process
postgres  2847  0.0  0.0  97852  1716 ?        S    00:13   0:00 sshd: postgres@pts/1
postgres  2848  0.0  0.0 108532  1936 pts/1    Ss   00:13   0:00 -bash
postgres  4919  0.0  0.0 110292  1152 pts/1    R+   02:18   0:00 ps auxw
postgres  4920  0.0  0.0 105296   928 pts/1    S+   02:18   0:00 egrep ^postgres|^USER


3.ツリー表示にしてみる(fオプション)
──────────────────────────────

【□】 ps auxwf|egrep "^postgres|^USER"

[postgres@kvm00 ~]$ ps auxwf|egrep "^postgres|^USER"
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres  2847  0.0  0.0  97852  1716 ?        S    00:13   0:00      \_ sshd: postgres@pts/1
postgres  2848  0.0  0.0 108532  1940 pts/1    Ss   00:13   0:00          \_ -bash
postgres  5073  0.0  0.0 110400  1180 pts/1    R+   02:30   0:00              \_ ps auxwf
postgres  5074  0.0  0.0 105292   892 pts/1    S+   02:30   0:00              \_ egrep ^postgres|^USER
postgres  2595  0.0  0.4 672720 33216 ?        S    00:06   0:00 /usr/local/pgsql/bin/postgres
postgres  2596  0.0  0.0 111124   752 ?        Ss   00:06   0:00  \_ postgres: logger process
postgres  2600  0.0  0.0 672980  4876 ?        Ss   00:06   0:00  \_ postgres: writer process
postgres  2601  0.0  0.0 672980   984 ?        Ss   00:06   0:00  \_ postgres: wal writer process
postgres  2602  0.0  0.0 673688  2228 ?        Ss   00:06   0:00  \_ postgres: autovacuum launcher process
postgres  2603  0.0  0.0 113336   952 ?        Ss   00:06   0:00  \_ postgres: archiver process   last was 00000001000000010000001D
postgres  2604  0.0  0.0 113336   968 ?        Ss   00:06   0:00  \_ postgres: stats collector process





4.表示する項目を指定(psのoオプション)
──────────────────────────────

o(または-o)でpsの結果を必要な項目のみに絞ることができる。
シェルスクリプトなどで目的にあった項目を取得する時などに便利。

ここではppidを表示、加えて普通のpsではあいまいになってしまう開始日時をちゃんとした書式にする。(lstart)


oとuは一緒にできないので、uを外してoにする。

ps axwf o 項目名のカンマリスト

ツリーをやめたければ fをはずす



【□】  ps axwf o user,pid,ppid,tty,lstart,%cpu,%mem,vsz,rss,time,command k lstart|egrep "^postgres|^USER" 

USER       PID  PPID TT                        STARTED %CPU %MEM    VSZ   RSS     TIME COMMAND
postgres  2717  2708 ?        Fri Nov 16 16:28:48 2012  0.0  0.0  97852  1744 00:00:01      \_ sshd: postgres@pts/0
postgres  2718  2717 pts/0    Fri Nov 16 16:28:48 2012  0.0  0.0 112856  2064 00:00:00          \_ -bash
postgres  3770  2718 pts/0    Fri Nov 16 17:03:49 2012  0.0  0.0 110400  1180 00:00:00              \_ ps axwfo user,pid,ppid,tty,lstart,%cpu,%mem,vsz,rss,time,command
postgres  3771  2718 pts/0    Fri Nov 16 17:03:49 2012  0.0  0.0 105292   896 00:00:00              \_ egrep ^postgres|^USER
postgres  2746     1 pts/0    Fri Nov 16 16:28:56 2012  0.0  0.4 672720 33220 00:00:00 /usr/local/pgsql/bin/postgres
postgres  2747  2746 ?        Fri Nov 16 16:28:56 2012  0.0  0.0 111124   752 00:00:00  \_ postgres: logger process
postgres  2749  2746 ?        Fri Nov 16 16:28:56 2012  0.0  0.0 672980  4688 00:00:00  \_ postgres: writer process
postgres  2750  2746 ?        Fri Nov 16 16:28:56 2012  0.0  0.0 672980   984 00:00:00  \_ postgres: wal writer process
postgres  2751  2746 ?        Fri Nov 16 16:28:56 2012  0.0  0.0 673788  2232 00:00:00  \_ postgres: autovacuum launcher process
postgres  2752  2746 ?        Fri Nov 16 16:28:56 2012  0.0  0.0 113336   832 00:00:00  \_ postgres: archiver process   last was 00000001000000010000009E
postgres  2753  2746 ?        Fri Nov 16 16:28:56 2012  0.0  0.0 113336  1008 00:00:00  \_ postgres: stats collector process
[postgres@kvm00 ~]$

※ps axwf -o 項目・・ 
とした方がわかりやすい?




■2■ psでPostgreSQLの何が見えるか?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ここからが本題となります。


psでPostgreSQLに関して、以下のような状態を確認できる。


確認例

・idle
・idle in transaction
・SELECT や  SELECT waiting  などSQL文の種類と状態




■3■ 単なるSELECT文実行中のpsコマンド表示例
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


長いSELECTを実行中

[postgres@kvm00 ~]$ ps axwfo user,pid,ppid,tty,command|egrep "^postgres|^USER"
USER       PID  PPID TT       COMMAND
postgres  2847  2844 ?         |   \_ sshd: postgres@pts/1
postgres  2848  2847 pts/1     |       \_ -bash
postgres  6937  2848 pts/1     |           \_ ps axwfo user,pid,ppid,tty,command
postgres  6938  2848 pts/1     |           \_ egrep ^postgres|^USER
postgres  6817  6815 ?             \_ sshd: postgres@pts/2
postgres  6818  6817 pts/2             \_ -bash
postgres  6918  6818 pts/2                 \_ psql test1
postgres  2595     1 ?        /usr/local/pgsql/bin/postgres
postgres  2596  2595 ?         \_ postgres: logger process
postgres  2600  2595 ?         \_ postgres: writer process
postgres  2601  2595 ?         \_ postgres: wal writer process
postgres  2602  2595 ?         \_ postgres: autovacuum launcher process
postgres  2603  2595 ?         \_ postgres: archiver process   last was 000000010000000100000094
postgres  2604  2595 ?         \_ postgres: stats collector process
postgres  6919  2595 ?         \_ postgres: postgres test1 [local] SELECT    ★

終了後

[postgres@kvm00 ~]$ ps axwfo user,pid,ppid,tty,command|egrep "^postgres|^USER"
USER       PID  PPID TT       COMMAND
postgres  2847  2844 ?         |   \_ sshd: postgres@pts/1
postgres  2848  2847 pts/1     |       \_ -bash
postgres  6940  2848 pts/1     |           \_ ps axwfo user,pid,ppid,tty,command
postgres  6941  2848 pts/1     |           \_ egrep ^postgres|^USER
postgres  6817  6815 ?             \_ sshd: postgres@pts/2
postgres  6818  6817 pts/2             \_ -bash
postgres  6918  6818 pts/2                 \_ psql test1
postgres  2595     1 ?        /usr/local/pgsql/bin/postgres
postgres  2596  2595 ?         \_ postgres: logger process
postgres  2600  2595 ?         \_ postgres: writer process
postgres  2601  2595 ?         \_ postgres: wal writer process
postgres  2602  2595 ?         \_ postgres: autovacuum launcher process
postgres  2603  2595 ?         \_ postgres: archiver process   last was 000000010000000100000094
postgres  2604  2595 ?         \_ postgres: stats collector process
postgres  6919  2595 ?         \_ postgres: postgres test1 [local] idle      ★



大量の表示のために時間がかかるような場合、SELECTは終わらないうちに早々にidleになる。
だから、idleだからといってユーザからみてSQLが終わっているとは限らない。




■3■ ロック待ちとなっている場合の例
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


【session1 □−−】 begin;
【session1 □−−】 lock table t1 in share mode;


【session2 −□−】 insert into t1 select * from t1;
                ・・ロックされているので待ちに入る


【session3 −−□】 ps axwfo user,pid,ppid,tty,command|egrep "^postgres|^USER"



USER       PID  PPID TT       COMMAND
postgres  2847  2844 ?         |   \_ sshd: postgres@pts/1
postgres  2848  2847 pts/1     |       \_ -bash
postgres  6326  2848 pts/1     |           \_ ps axwfo user,pid,ppid,tty,command
postgres  6327  2848 pts/1     |           \_ egrep ^postgres|^USER
postgres  5484  5481 ?         |   \_ sshd: postgres@pts/2
postgres  5485  5484 pts/2     |       \_ -bash
postgres  5526  5485 pts/2     |           \_ psql test1
postgres  5828  5825 ?             \_ sshd: postgres@pts/3
postgres  5829  5828 pts/3             \_ -bash
postgres  5858  5829 pts/3                 \_ psql test1
postgres  2595     1 ?        /usr/local/pgsql/bin/postgres
postgres  2596  2595 ?         \_ postgres: logger process
postgres  2600  2595 ?         \_ postgres: writer process
postgres  2601  2595 ?         \_ postgres: wal writer process
postgres  2602  2595 ?         \_ postgres: autovacuum launcher process
postgres  2603  2595 ?         \_ postgres: archiver process   last was 000000010000000100000071
postgres  2604  2595 ?         \_ postgres: stats collector process
postgres  5527  2595 ?         \_ postgres: postgres test1 [local] INSERT waiting    ★1
postgres  5859  2595 ?         \_ postgres: postgres test1 [local] idle in transaction ★2


★1 INSERT waiting のプロセス
★2 トランザクション中で、アイドル状態のプロセス

の2つのプロセス/セッションの状態が確認できる。

PostgreSQLはOracleのようなマルチスレッド(shared server)は構成することができないので、
サーバープロセス=セッションの単位にできるからpsでみてもわかりやすい。

ちなみに、上の方にはpsqlコマンドが2つ確認できる。
が、psqlに対してどのpostgresプロセスが対応付けられているかはここでは確認不可能?(未調査)





■4■ 【参考】アーカイバ動作の状況
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


アーカイバ実行の情報が採れていたので、以下に控えた。


(1)アーカイバ普段の状況

USER       PID  PPID TT        COMMAND
postgres  2603  2595 ?          \_ postgres: archiver process   last was 000000010000000100000068


(2)アーカイブ実行中の状況


[postgres@kvm00 ~]$ ps axwfo user,pid,ppid,tty,command|egrep "^postgres|^USER"
USER       PID  PPID TT       COMMAND
postgres  2595     1 ?        /usr/local/pgsql/bin/postgres
postgres  2596  2595 ?         \_ postgres: logger process
postgres  2600  2595 ?         \_ postgres: writer process
postgres  2601  2595 ?         \_ postgres: wal writer process
postgres  2602  2595 ?         \_ postgres: autovacuum launcher process
postgres  2603  2595 ?         \_ postgres: archiver process   archiving 000000010000000100000086★
postgres  6588  2603 ?         |   \_ sh -c test ! -f walarc/000000010000000100000086 && (echo "`date "+%Y/%m/%d %T"` pg_xlog/000000010000000100000086 archived";cp pg_xlog/000000010000000100000086 walarc/000000010000000100000086) 1>&2
postgres  6589  6588 ?         |       \_ sh -c test ! -f walarc/000000010000000100000086 && (echo "`date "+%Y/%m/%d %T"` pg_xlog/000000010000000100000086 archived";cp pg_xlog/000000010000000100000086 walarc/000000010000000100000086) 1>&2
postgres  6592  6589 ?         |           \_ cp pg_xlog/000000010000000100000086 walarc/000000010000000100000086
postgres  2604  2595 ?         \_ postgres: stats collector process
postgres  5527  2595 ?         \_ postgres: postgres test1 [local] INSERT


・・・archivingという状態になる。
   その下で、起動されたアーカイブ処理が走行している。



(3)アーカイバ失敗時の状況

ディスクFULLで失敗した場合は以下のようになる。


USER       PID  PPID TT        COMMAND
postgres  2603  2595 ?         \_ postgres: archiver process   failed on 00000001000000010000008A★







■5■ 【参考】autovacuumワーカープロセス実行中
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

大量更新をかけ、autovacuumワーカーのプロセスを出現させて記録


【□】 while true ;do sleep 2;ps auxwf|egrep "^postgres|^USER" ;done


USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres  2526  0.0  0.0  97852  1720 ?        S    Nov16   0:00  |   \_ sshd: postgres@pts/1
postgres  2527  0.0  0.0 112856  2072 pts/1    Ss   Nov16   0:00  |       \_ -bash
postgres  6890  0.0  0.0 112564  1864 pts/1    S+   01:00   0:00  |           \_ psql test1
postgres  7787  0.0  0.0  97852  1720 ?        S    01:20   0:00      \_ sshd: postgres@pts/2
postgres  7788  0.0  0.0 112848  2048 pts/2    Ss   01:20   0:00          \_ -bash
postgres  8647  0.0  0.0 110404  1184 pts/2    R+   01:27   0:00              \_ ps auxwf
postgres  8648  0.0  0.0 105292   888 pts/2    S+   01:27   0:00              \_ egrep ^postgres|^USER
postgres  2492  0.0  0.4 672724 33216 ?        S    Nov16   0:00 /usr/local/pgsql/bin/postgres
postgres  2493  0.0  0.0 111128   820 ?        Ss   Nov16   0:00  \_ postgres: logger process
postgres  6833  0.2  7.0 673264 549892 ?       Ss   00:58   0:04  \_ postgres: writer process
postgres  6834  0.0  0.2 672984 17464 ?        Ss   00:58   0:01  \_ postgres: wal writer process
postgres  6835  0.0  0.0 673824  2288 ?        Ss   00:58   0:00  \_ postgres: autovacuum launcher process
postgres  6836  0.0  0.0 113340   940 ?        Ss   00:58   0:00  \_ postgres: archiver process   last was 000000010000000300000000
postgres  6837  0.0  0.0 113492  1164 ?        Ss   00:58   0:00  \_ postgres: stats collector process
postgres  6891  2.1  7.1 676832 559788 ?       Ss   01:00   0:34  \_ postgres: postgres test1 [local] idle
postgres  8594  1.0  3.2 682644 251160 ?       Ss   01:27   0:00  \_ postgres: autovacuum worker process   test1★


以上 
inserted by FC2 system