pg_statsinfo(統計・OSリソース情報時系列取得保存ツール)

pg_statsinfo とは

pg_statsinfo は PostgreSQL の統計情報やOSリソース情報を時系列で取得・保存するツールです。複数スナップショットを取得してその期間のレポートが参照出来ます。


取得した情報はテキストファイル形式でレポート出力が可能ですが、pg_stats_reporter という Web ベースで参照が容易なツールが別途用意されており pg_statsinfo とあわせて pg_stats_reporter もインストールして使用することをお勧めします。

公式サイトは[こちら]で、分かりやすいドキュメントも公式サイトにリンクがあります。(例: [pg_statsinfo 13 ドキュメント], [pg_stats_reporter 13 ドキュメント])

pg_statsinfo の機能

pg_statsinfo の主要な機能として以下のものがあります。

  • 統計情報の取得機能
    PostgreSQL 統計情報やOSリソース情報をスナップショットとして時系列で取得してリポジトリDBに蓄積します。取得した情報から性能情報のレポートを出力したり、一定期間スナップショットを保持して古くなったスナップショットを削除するなどメンテナンス機能もあります。
  • サーバログ分配機能
    csv形式で出力されたサーバログを加工して、 PostgreSQLログや syslog に出力することが可能です。またログの圧縮やローテーションも可能です。
  • アラート機能
    リポジトリDB内に保持したアラートしきい値を超えた項目についてアラート出力することが可能です。

スナップショットとは

スナップショットとはとあるタイミングで取得した[統計情報]です。スナップショットを複数取得して異なるタイミングでの統計情報のトレンドの変化を把握したり、開始のスナップショットと終了のスナップショットを指定してその期間の性能情報のレポートを作成することが可能です。

リポジトリDBとは

pg_statsinfo は計測対象の PostgreSQL に導入しますが、取得したスナップショットはリポジトリDBと呼ばれるデータベースに保存します。独立したリポジトリDBを別途用意して pg_statsinfo 計測対象の PostgreSQL で取得した計測データをリポジトリDBに保存する構成も可能ですし、リポジトリDBは pg_statsinfo 計測対象の PostgreSQL の中に同居させることも可能です。
PostgreSQLを2台用意したアクティブ/スタンバイのストリーミングレプリケーション構成でリポジトリDBを同居する場合は、スタンバイ側計測データはスタンバイ側DB(自分自身)には書き込みが出来ませんので、常にプライマリに書き込むように接続先IPアドレスを設定する必要があります。
なお計測データをWeb画面で参照する pg_stats_reporter はリポジトリDBのサーバにインストールします。

バージョン構成

pg_statsinfo 3.3 までは複数の PostgreSQL バージョンがサポート対象でしたが、pg_statsinfo 10 以降は1つの PostgreSQL バージョンのみがサポート対象です。
また RHEL 8.x / CentOS 8.x がサポートされているバージョンは pg_statsinfo 12 と pg_statsinfo 13 です。

pg_statsinfo バージョン サポート対象 PostgreSQL バージョン サポート対象OS
pg_statsinfo 13 PostgreSQL 13 RHEL/CentOS 7.x,RHEL/CentOS 8.x
pg_statsinfo 12 PostgreSQL 12 RHEL/CentOS 7.x, RHEL/CentOS 8.x
pg_statsinfo 11 PostgreSQL 11 RHEL/CentOS 7.x
pg_statsinfo 10 PostgreSQL 10 RHEL/CentOS 7.x
pg_statsinfo 3.3 PostgreSQL 9.2, 9.3, 9.4, 9.5, 9.6, 10 RHEL/CentOS 6.x, RHEL/CentOS 7.x

pg_stats_reporter はそれぞれの pg_statsinfo のバージョンに対応したバージョンが用意されています。

pg_statsinfo 11 on RHEL 8.x / CentOS 8.x

RHEL 8.x / CentOS 8.x 上での pg_statsinfo 11 については、rpm は用意されておらずソースコードはコンパイル時にエラーが出ますが、コンパイルエラーは libstatsinfo.c に

#include <sys/sysmacros.h>

の行を追加すればエラーは解消します。

pg_statsinfo インストール(rpmパッケージ)

pg_statsinfo のインストール方法は、rpm からのインストールとソースコードからのインストールの二つがありますが、今回は rpm からのインストールをご紹介します。
下記のインストールの実行例では1台のサーバ上で CentOS Linux release 8.3.2011, PostgreSQL 13.2, pg_statsinfo 13.0, pg_stats_reporter 13.0 を使用して説明します。
CentOS から直接インターネット接続が可能な環境を使用しています。
また PostgreSQL 13.2 はPostgreSQL公式サイトのrpmを使って既に設定済の環境を使用します。

pg_statsinfo rpmパッケージのインストール

pg_statsinfo 13.0 の rpm ファイル(pg_statsinfo-13.0-1.pg13.rhel8.x86_64.rpm)を[こちら]からダウンロードしてインストールします。

# dnf install pg_statsinfo-13.0-1.pg13.rhel8.x86_64.rpm
依存関係が解決しました。
=========================================================================================================================================
パッケージ アーキテクチャー バージョン リポジトリー サイズ
=========================================================================================================================================
インストール:
pg_statsinfo x86_64 13.0-1.pg13.rhel8 @commandline 758 k

トランザクションの概要
=========================================================================================================================================
インストール 1 パッケージ

合計サイズ: 758 k
インストール済みのサイズ: 1.2 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
準備 : 1/1
scriptletの実行中: pg_statsinfo-13.0-1.pg13.rhel8.x86_64 1/1
インストール : pg_statsinfo-13.0-1.pg13.rhel8.x86_64 1/1
scriptletの実行中: pg_statsinfo-13.0-1.pg13.rhel8.x86_64 1/1
検証 : pg_statsinfo-13.0-1.pg13.rhel8.x86_64 1/1
Installed products updated.

インストール済み:
pg_statsinfo-13.0-1.pg13.rhel8.x86_64

完了しました!
#

pg_statsinfo の設定 (postgresql.conf ファイル)

[公式ドキュメント]の「設定ファイル – postgresql.confの設定」に記載されている「推奨設定」の記載をpostgresql.conf ファイルに追加し、PostgreSQL 13.2 を再起動します。

$ cd $PGDATA
$ vi postgresql.conf (下記の内容を最後に追加)
#推奨設定
shared_preload_libraries = 'pg_statsinfo' # 事前ロードを行う

pg_statsinfo.snapshot_interval = 30min # スナップショットの取得間隔 (本検証では10minに設定します)
pg_statsinfo.enable_maintenance = 'on' # 自動メンテナンス設定
pg_statsinfo.maintenance_time = '00:02:00' # 自動メンテナンス実行時刻設定
pg_statsinfo.repolog_min_messages = disable # ログ蓄積機能の設定

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # ログファイル名を指定する
log_min_messages = 'log' # ログへ出力するメッセージレベル。
pg_statsinfo.syslog_min_messages = 'error' # syslogに出力するログレベルを指定する。
pg_statsinfo.textlog_line_prefix = '%t %p %c-%l %x %q(%u, %d, %r, %a) '
# pg_statsinfoがテキストログに出力する際、各行の先頭に追加される書式を指定する。log_line_prefixと同じ形式で指定する。
pg_statsinfo.syslog_line_prefix = '%t %p %c-%l %x %q(%u, %d, %r, %a) '
# pg_statsinfoがsyslog経由でログを出力する際、各行の先頭に追加される書式を指定する。

track_functions = 'all' # ストアドプロシージャの呼び出しに関する統計情報を収集する
log_checkpoints = on # チェックポイントを記録
log_autovacuum_min_duration = 0 # 自動バキュームを記録
#pg_statsinfo.long_lock_threshold = 30s # ロック競合情報に記録する対象の条件(閾値)を指定する
$ su -
# systemctl restart postgresql-13.service
#

pg_stats_reporter インストール(rpmパッケージ)

pg_stats_reporter も同一サーバ上で使えるようにインストールします。

pg_stats_reporter rpmパッケージのインストール

pg_stats_reporter 13.0 の rpm ファイル(pg_stats_reporter-13.0-1.el8.noarch.rpm)を[こちら]からダウンロードしてインストールします。

# dnf install pg_stats_reporter-13.0-1.el8.noarch.rpm
依存関係が解決しました。
=========================================================================================================================================
パッケージ アーキテクチャー バージョン リポジトリー サイズ
=========================================================================================================================================
インストール:
pg_stats_reporter noarch 13.0-1.el8 @commandline 2.9 M
依存関係のインストール:
apr x86_64 1.6.3-11.el8 appstream 125 k
apr-util x86_64 1.6.1-6.el8 appstream 105 k
centos-logos-httpd noarch 80.5-2.el8 baseos 24 k
httpd x86_64 2.4.37-30.module_el8.3.0+561+97fdbbcc appstream 1.7 M
httpd-filesystem noarch 2.4.37-30.module_el8.3.0+561+97fdbbcc appstream 37 k
httpd-tools x86_64 2.4.37-30.module_el8.3.0+561+97fdbbcc appstream 104 k
mod_http2 x86_64 1.15.7-2.module_el8.3.0+477+498bb568 appstream 154 k
nginx-filesystem noarch 1:1.14.1-9.module_el8.0.0+184+e34fea82 appstream 24 k
php x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 1.5 M
php-cli x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 3.1 M
php-common x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 661 k
php-pdo x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 122 k
php-pgsql x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 117 k
php-xml x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 188 k
弱い依存関係のインストール:
apr-util-bdb x86_64 1.6.1-6.el8 appstream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 appstream 27 k
php-fpm x86_64 7.2.24-1.module_el8.2.0+313+b04d0a66 appstream 1.6 M
モジュールストリームの有効化中:
httpd 2.4
nginx 1.14
php 7.2

トランザクションの概要
=========================================================================================================================================
インストール 18 パッケージ

合計サイズ: 12 M
ダウンロードサイズの合計: 9.6 M
インストール済みのサイズ: 47 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/17): apr-util-bdb-1.6.1-6.el8.x86_64.rpm 405 kB/s | 25 kB 00:00
(2/17): apr-util-openssl-1.6.1-6.el8.x86_64.rpm 537 kB/s | 27 kB 00:00
(3/17): apr-1.6.3-11.el8.x86_64.rpm 959 kB/s | 125 kB 00:00
(4/17): apr-util-1.6.1-6.el8.x86_64.rpm 797 kB/s | 105 kB 00:00
:
(略)
:
検証 : centos-logos-httpd-80.5-2.el8.noarch 17/18
検証 : pg_stats_reporter-13.0-1.el8.noarch 18/18
Installed products updated.

インストール済み:
apr-1.6.3-11.el8.x86_64 apr-util-1.6.1-6.el8.x86_64
apr-util-bdb-1.6.1-6.el8.x86_64 apr-util-openssl-1.6.1-6.el8.x86_64
centos-logos-httpd-80.5-2.el8.noarch httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64
httpd-filesystem-2.4.37-30.module_el8.3.0+561+97fdbbcc.noarch httpd-tools-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64
mod_http2-1.15.7-2.module_el8.3.0+477+498bb568.x86_64 nginx-filesystem-1:1.14.1-9.module_el8.0.0+184+e34fea82.noarch
pg_stats_reporter-13.0-1.el8.noarch php-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-cli-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 php-pdo-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-pgsql-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 php-xml-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64

完了しました!
#

pg_stats_reporter の設定 (httpd 起動設定)

pg_stats_reporterで使用するために、OS起動時に自動的にhttpdが起動するように設定します。

# systemctl start httpd
# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/httpd.service.d
mqphp-fpm.conf
Active: active (running) since Tue 2021-03-30 15:22:36 JST; 1h 15min ago
Docs: man:httpd.service(8)
Process: 12032 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 1293 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 4816)
Memory: 29.2M
CGroup: /system.slice/httpd.service
tq 1293 /usr/sbin/httpd -DFOREGROUND
tq12052 /usr/sbin/httpd -DFOREGROUND
tq12053 /usr/sbin/httpd -DFOREGROUND
tq12054 /usr/sbin/httpd -DFOREGROUND
mq12055 /usr/sbin/httpd -DFOREGROUND

3月 30 15:22:27 c832011 systemd[1]: Starting The Apache HTTP Server...
3月 30 15:22:36 c832011 httpd[1293]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80:>
3月 30 15:22:36 c832011 systemd[1]: Started The Apache HTTP Server.
3月 30 15:23:02 c832011 httpd[1293]: Server configured, listening on: port 80
3月 30 16:07:01 c832011 systemd[1]: Reloading The Apache HTTP Server.
3月 30 16:07:09 c832011 httpd[12032]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80>
3月 30 16:07:09 c832011 systemd[1]: Reloaded The Apache HTTP Server.
3月 30 16:07:17 c832011 httpd[1293]: Server configured, listening on: port 80
#

 

pg_stats_reporter の設定 (pg_stats_reporter設定ファイル)

/etc ディレクトリ直下に pg_stats_reporter.ini ファイルがありますので repository DB setting section と書かれた箇所にリポジトリDBの情報を記入してください。検証環境では下記のように設定しました。

;=====================================
; repository DB setting section
;=====================================

;-------------------------------------
; repository1
;-------------------------------------

[repository1]

;-------------------------------------
; database connection
;-------------------------------------

host = localhost
port = 5432
dbname = postgres
username = postgres
password = password
;

pg_statsinfo の使い方

起動プロセスの確認

PostgreSQL のプロセスを確認すると、pg_statsinfo launcher と pg_statsinfod が起動されていることが分かります。

$ ps -ef | grep postgres | grep -v grep
postgres 1070 1 0 10:42 ? 00:00:00 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
postgres 1103 1070 0 10:42 ? 00:00:00 postgres: logger
postgres 1105 1070 0 10:42 ? 00:00:00 postgres: checkpointer
postgres 1106 1070 0 10:42 ? 00:00:00 postgres: background writer
postgres 1107 1070 0 10:42 ? 00:00:00 postgres: walwriter
postgres 1108 1070 0 10:42 ? 00:00:00 postgres: autovacuum launcher
postgres 1109 1070 0 10:42 ? 00:00:00 postgres: stats collector
postgres 1110 1070 0 10:42 ? 00:00:01 postgres: pg_statsinfo launcher
postgres 1111 1070 0 10:42 ? 00:00:00 postgres: logical replication launcher
postgres 1112 1110 0 10:42 ? 00:00:13 /usr/pgsql-13/bin/pg_statsinfod 1070
root 6501 6466 0 11:35 pts/0 00:00:00 su - postgres
postgres 6502 6501 0 11:35 pts/0 00:00:00 -bash
postgres 13338 1070 0 14:10 ? 00:00:00 postgres: postgres postgres [local] idle
postgres 13409 6502 0 14:12 pts/0 00:00:00 ps -ef

PostgreSQLログファイル

PostgreSQL ログディレクトリを確認すると、固定ファイル名のPostgreSQLログ(pg_statsinfo.log)が一つと、年月日時分秒が付いたCSVログ(.csv)とPostgreSQLログ(.log)と.copyの付いたPostgreSQLログ(.log.copy)があります。監視ソフトで設定する監視対象は固定ファイル名のPostgreSQLログとなります。

$ cd $PGDATA/log
$ ls -l
合計 932
drwx------. 2 postgres postgres 6 3月 23 15:32 log_archive
-rw------- 1 postgres postgres 44098 4月 14 14:12 pg_statsinfo.log
-rw------- 1 postgres postgres 65643 3月 23 16:15 postgresql-2021-03-23_153328.csv
-rw------- 1 postgres postgres 52741 3月 23 16:15 postgresql-2021-03-23_153328.log
-rw------- 1 postgres postgres 168 3月 23 15:33 postgresql-2021-03-23_153328.log.copy
(略)
-rw------- 1 postgres postgres 7047 4月 13 19:13 postgresql-2021-04-13_190131.csv
-rw------- 1 postgres postgres 5009 4月 14 10:42 postgresql-2021-04-13_190131.log
-rw------- 1 postgres postgres 166 4月 13 19:01 postgresql-2021-04-13_190131.log.copy
-rw------- 1 postgres postgres 57678 4月 14 14:12 postgresql-2021-04-14_104210.csv
-rw------- 1 postgres postgres 166 4月 14 10:42 postgresql-2021-04-14_104210.log

スナップショット取得(自動)

スナップショットはpg_statsinfo.snapshot_intervalパラメータで設定した値で自動的に取得されます。
今回検証環境では10minに設定していますので、10分おきにスナップショットが取得されます。

スナップショット取得(手動)

スナップショットは SELECT statsinfo.snapshot(‘コメント’)の実行でも手動で取得出来ます。
スナップショットの取得自体は非同期に実行されますので、コマンドプロンプトはすぐに返ってきてもスナップショットがまだ完了していない場合もあります。

$ psql -d postgres -c "SELECT statsinfo.snapshot('comment202104141602')"
snapshot
----------

(1 行)

$

スナップショット一覧

コマンドラインから pg_statsinfo -l を実行すると現在リポジトリに保存されているスナップショットの一覧が表示されます。

$ pg_statsinfo -l
----------------------------------------
Snapshot List
----------------------------------------
SnapshotID InstanceID Host Port Timestamp Comment Execute Time Size
-----------------------------------------------------------------------------------------------------------------------------------------
28 1 c832011 5432 2021-03-24 10:50:00 00:00:01 232 KiB
29 1 c832011 5432 2021-03-24 11:09:11 00:00:02 176 KiB
(略)
165 1 c832011 5432 2021-04-14 15:50:00 00:00:01 368 KiB
166 1 c832011 5432 2021-04-14 16:00:00 00:00:01 360 KiB
167 1 c832011 5432 2021-04-14 16:02:05 comment202104141602 00:00:01 368 KiB

簡易レポート機能

スナップショットの開始と終了を指定してテキスト形式のレポート出力が出来ます。レポートは標準出力に出ますので、適切なファイル名にリダイレクトして保存します。
下記の例では全てをレポート出力(All)としていますが、指定可能な種類は[コマンドライン機能の使い方]のレポート生成に記載されています。

$ pg_statsinfo -r All -b 165 -e 167 -h localhost -d postgres -p 5432 -U postgres > sireport165_167.txt
Password:
$ cat sireport165_167.txt
---------------------------------------------
STATSINFO Report (host: c832011, port: 5432)
---------------------------------------------

----------------------------------------
/* Summary */
----------------------------------------
Database System ID : 6940138282810842183
Host : c832011
Port : 5432
PostgreSQL Version : 13.2
Snapshot Begin : 2021-04-14 15:50:00
Snapshot End : 2021-04-14 16:02:05
Snapshot Duration : 00:12:05
Total Database Size : 151 MiB
Total Commits : 273
Total Rollbacks : 0

/** Alert **/
-----------------------------------
DateTime Message
--------------------------------------------------------------------------------

----------------------------------------
/* Database Statistics */
----------------------------------------
Database Name : postgres
Database Size : 52 MiB
Database Size Increase : 0 MiB
Commit/s : 0.335
Rollback/s : 0.000
Cache Hit Ratio : 99.4 %
Block Read/s (disk+cache) : 205.821
Block Read/s (disk) : 1.312
Rows Read/s : 1692.930
Temporary Files : 0
Temporary Bytes : 0 MiB
Deadlocks : 0
Block Read Time : 0.000 ms
Block Write Time : 0.000 ms

Database Name : test1
Database Size : 99 MiB
Database Size Increase : 0 MiB
Commit/s : 0.041
Rollback/s : 0.000
Cache Hit Ratio : 100.0 %
Block Read/s (disk+cache) : 6.675
Block Read/s (disk) : 0.000
Rows Read/s : 41.593
Temporary Files : 0
Temporary Bytes : 0 MiB
Deadlocks : 0
Block Read Time : 0.000 ms
Block Write Time : 0.000 ms

/** Transaction Statistics **/
-----------------------------------
DateTime Database Commit/s Rollback/s
-----------------------------------------------------------------
2021-04-14 16:00 postgres 0.308 0.000
2021-04-14 16:00 test1 0.038 0.000
2021-04-14 16:02 postgres 0.463 0.000
2021-04-14 16:02 test1 0.056 0.000

/** Database Size **/
-----------------------------------
DateTime Database Size
-----------------------------------------------------
2021-04-14 15:50 postgres 51.411 MiB
2021-04-14 15:50 test1 99.090 MiB
2021-04-14 16:00 postgres 51.770 MiB
2021-04-14 16:00 test1 99.090 MiB
2021-04-14 16:02 postgres 52.129 MiB
2021-04-14 16:02 test1 99.090 MiB

/** Recovery Conflicts **/
-----------------------------------
Database Conflict Tablespace Conflict Lock Conflict Snapshot Conflict Bufferpin Conflict Deadlock
-----------------------------------------------------------------------------------------------------------------
postgres 0 0 0 0 0
test1 0 0 0 0 0

----------------------------------------
/* Instance Activity */
----------------------------------------

/** WAL Statistics **/
-----------------------------------
WAL Write Total : 1.586 MiB
WAL Write Speed : 0.004 MiB/s
WAL Archive Total : 0 file(s)
WAL Archive Failed : 0 file(s)

-----------------------------------
DateTime Location Segment File Write Size Write Size/s Last Archived WAL
-------------------------------------------------------------------------------------------------------------------
2021-04-14 16:00 0/95B3B738 000000010000000000000095 0.838 MiB 0.001 MiB
2021-04-14 16:02 0/95BFAD18 000000010000000000000095 0.748 MiB 0.006 MiB

/** Instance Processes **/
-----------------------------------
DateTime Idle Idle In Xact Waiting Running
---------------------------------------------------------------------------
2021-04-14 15:50 100.0 % 0.0 % 0.0 % 0.0 %
2021-04-14 16:00 100.0 % 0.0 % 0.0 % 0.0 %
2021-04-14 16:02 100.0 % 0.0 % 0.0 % 0.0 %
Average 100.0 % 0.0 % 0.0 % 0.0 %

/** BGWriter Statistics **/
-----------------------------------
Written Buffers By BGWriter (Average) : 0.000 buffers/s
Written Buffers By BGWriter (Maximum) : 0.000 buffers/s
Written Buffers By Backend (Average) : 0.219 buffers/s
Written Buffers By Backend (Maximum) : 0.360 buffers/s
Backend Executed fsync (Average) : 0.000 sync/s
Backend Executed fsync (Maximum) : 0.000 sync/s

----------------------------------------
/* OS Resource Usage */
----------------------------------------

/** CPU Usage + Load Average **/
-----------------------------------
DateTime User System Idle IOwait Loadavg1 Loadavg5 Loadavg15
------------------------------------------------------------------------------------------
2021-04-14 16:00 0.5 % 0.3 % 99.2 % 0.0 % 0.020 0.010 0.000
2021-04-14 16:02 0.8 % 0.3 % 98.9 % 0.1 % 0.020 0.010 0.000
Average 0.7 % 0.3 % 99.1 % 0.1 % 0.020 0.010 0.000

/** IO Usage **/
-----------------------------------
Device Including TableSpaces Total Read Total Write Total Read Time Total Write Time Current IO Queue Total IO Time
----------------------------------------------------------------------------------------------------------------------------------------------
dm-0 {pg_default,pg_global} 17 MiB 18 MiB 412 ms 1571 ms 0.000 1983 ms

-----------------------------------
DateTime Device Read Size/s (Peak) Write Size/s (Peak) Read Time Rate Write Time Rate
---------------------------------------------------------------------------------------------------------------------------------
2021-04-14 16:00 dm-0 24.30 KiB ( 1416.00 KiB) 22.77 KiB ( 164.00 KiB) 0.1 % 0.2 %
2021-04-14 16:02 dm-0 20.33 KiB ( 271.20 KiB) 35.62 KiB ( 219.00 KiB) 0.0 % 0.4 %

/** Memory Usage **/
-----------------------------------
DateTime Memfree Buffers Cached Swap Dirty
-----------------------------------------------------------------------------------------
2021-04-14 15:50 113.38 MiB 3.09 MiB 390.30 MiB 0.26 MiB 0.19 MiB
2021-04-14 16:00 61.36 MiB 3.09 MiB 390.11 MiB 0.76 MiB 0.32 MiB
2021-04-14 16:02 80.42 MiB 3.09 MiB 395.30 MiB 0.76 MiB 0.33 MiB

----------------------------------------
/* Disk Usage */
----------------------------------------

/** Disk Usage per Tablespace **/
-----------------------------------
Tablespace Location Device Used Avail Remain
-----------------------------------------------------------------------------------------------------------
pg_default /var/lib/pgsql/13/data 253:0 6470 MiB 10923 MiB 62.8 %
pg_global /var/lib/pgsql/13/data 253:0 6470 MiB 10923 MiB 62.8 %

/** Disk Usage per Table **/
-----------------------------------
Database Schema Table Size Table Reads Index Reads Toast Reads
---------------------------------------------------------------------------------------------------------------
postgres statsrepo column_20210414 5 MiB 642 29 0
postgres statsrepo index_20210414 0 MiB 67 2 0
:
(略)
:
postgres statsrepo database 0 MiB 9 0 0
postgres statsrepo setting 0 MiB 3 2 0

----------------------------------------
/* Long Transactions */
----------------------------------------
PID Client Address Xact Start Duration Query
-----------------------------------------------------------------------------------------

----------------------------------------
/* Notable Tables */
----------------------------------------

/** Heavily Updated Tables **/
-----------------------------------
Database Schema Table INSERT Rows UPDATE Rows DELETE Rows Total Rows HOT Ratio(%)
-----------------------------------------------------------------------------------------------------------------------------
postgres statsrepo column_20210414 2892 0 0 2892 0.0
postgres statsrepo table_20210414 166 0 0 166 0.0
postgres statsrepo function 162 0 0 162 0.0
:
(略)
:
postgres statsrepo memory 2 0 0 2 0.0
postgres statsrepo xlog 2 0 0 2 0.0

/** Heavily Accessed Tables **/
-----------------------------------
Database Schema Table Seq Scans Read Rows Read Rows/Scan Cache Hit Ratio(%)
-----------------------------------------------------------------------------------------------------------------------
postgres statsrepo column_20210413 4 4992 1248.000 93.2
postgres statsrepo table_20210326 6 2286 381.000 99.3
:
(略)
:
postgres statsrepo instance 106 106 1.000 100.0
postgres statsrepo alert 2 2 1.000 100.0

/** Low Density Tables **/
-----------------------------------
Database Schema Table Live Tuples Logical Pages Physical Pages Logical Page Ratio(%)
----------------------------------------------------------------------------------------------------------------------------
test1 public pgbench_accounts 500000 8197 8476 96.7
test1 public pgbench_history 283914 1809 1809 100.0

/** Correlation **/
-----------------------------------
Database Schema Table Column Correlation
---------------------------------------------------------------------------------------
test1 public pgbench_branches bid -0.600
postgres statsrepo column_20210413 tbl -0.200
test1 public pgbench_tellers tid -0.059
:
(略)
:
postgres statsrepo function snapid 1.000
postgres statsrepo table_20210405 snapid 1.000

----------------------------------------
/* Checkpoint Activity */
----------------------------------------
Total Checkpoints : 3
Checkpoints By Time : 3
Checkpoints By WAL : 0
Written Buffers Average : 105.667
Written Buffers Maximum : 133.000
Write Duration Average : 10.661 sec
Write Duration Maximum : 13.404 sec
----------------------------------------
/* Autovacuum Activity */
----------------------------------------

/** Vacuum Basic Statistics (Average) **/
-----------------------------------
Table Count Removed Rows Remain Rows Remain Dead Index Scans Duration Duration(Max) Cancels
-------------------------------------------------------------------------------------------------------------------------------------------

/** Vacuum I/O Statistics (Average) **/
-----------------------------------
Table Page Hit Page Miss Page Dirty Read Rate Write Rate
-----------------------------------------------------------------------------------------------------

/** Analyze Statistics **/
-----------------------------------
Table Count Duration(Total) Duration(Avg) Duration(Max) Last Analyze Time Cancels Mod Rows(Max)
---------------------------------------------------------------------------------------------------------------------------------------------
postgres.statsrepo.column_20210414 1 0.280 s 0.280 s 0.280 s 2021-04-14 16:00:50 0 2892
postgres.statsrepo.index_20210414 1 0.040 s 0.040 s 0.040 s 2021-04-14 15:50:50 0 180
postgres.statsrepo.inherits 1 0.010 s 0.010 s 0.010 s 2021-04-14 16:00:50 0 360

----------------------------------------
/* Query Activity */
----------------------------------------

/** Functions **/
-----------------------------------
OID Database Schema Function Calls Total Time Self Time Time/Call
----------------------------------------------------------------------------------------------------------------------
16990 postgres statsrepo partition_snapshot_insert 3178 470.809 ms 470.809 ms 0.148 ms
16984 postgres statsrepo get_snap_date 3178 235.482 ms 235.482 ms 0.074 ms
:
(略)
:
16960 postgres statsrepo get_heavily_accessed_tables 2 9.207 ms 8.761 ms 4.604 ms
16975 postgres statsrepo get_bgwriter_tendency 4 8.280 ms 4.834 ms 2.070 ms

/** Statements **/
-----------------------------------
User Database Calls Total Time Time/Call Block Read Time Block Write Time Query
------------------------------------------------------------------------------------------------------------------------

/** Plans **/
-----------------------------------
Query ID Plan ID User Database Calls Total Time Time/Call Block Read Time Block Write Time
-----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------
/* Lock Conflicts */
----------------------------------------
Database Schema Relation Duration Blockee PID Blocker PID Blocker GID
Blockee Query
Blocker Query
--------------------------------------------------------------------------------------------------------

----------------------------------------
/* Replication Activity */
----------------------------------------

/** Current Replication Status **/
-----------------------------------

----------------------------------------
/* Setting Parameters */
----------------------------------------
Name Setting Unit Source
-------------------------------------------------------------------------------------------------------
TimeZone Asia/Tokyo configuration file
default_text_search_config pg_catalog.english configuration file
lc_messages C configuration file
log_autovacuum_min_duration 0 ms configuration file
log_checkpoints on configuration file
log_destination csvlog override
log_min_messages log configuration file
log_rotation_size 0 kB configuration file
log_timezone Asia/Tokyo configuration file
log_truncate_on_rotation on configuration file
logging_collector on override
max_stack_depth 2048 kB environment variable
password_encryption scram-sha-256 configuration file
pg_statsinfo.repolog_min_messages disable configuration file
pg_statsinfo.syslog_line_prefix %t %p %c-%l %x %q(%u, %d, %r, %a) configuration file
pg_statsinfo.syslog_min_messages error configuration file
pg_statsinfo.textlog_line_prefix %t %p %c-%l %x %q(%u, %d, %r, %a) configuration file
server_encoding UTF8 override
shared_buffers 16384 8kB configuration file
shared_preload_libraries pg_statsinfo configuration file
track_functions all configuration file
wal_buffers 512 8kB override

----------------------------------------
/* Schema Information */
----------------------------------------

/** Tables **/
-----------------------------------
Database Schema Table Columns Rows Size Size Incr Table Scans Index Scans
-----------------------------------------------------------------------------------------------------------------------------
test1 public pgbench_accounts 4 500000 66 MiB 0 MiB 0 0
test1 public pgbench_branches 3 5 0 MiB 0 MiB 0 0
test1 public pgbench_history 6 283914 14 MiB 0 MiB 0 0
test1 public pgbench_tellers 4 50 0 MiB 0 MiB 0 0

/** Indexes **/
-----------------------------------
Database Schema Index Table Size Size Incr Index Scans Rows/Scan Disk Reads Cache Reads Index Key
-------------------------------------------------------------------------------------------------------------------------------------------------------------
test1 public pgbench_accounts_pkey pgbench_accounts 10 MiB 0 MiB 0 0.000 0 0 aid
test1 public pgbench_branches_pkey pgbench_branches 0 MiB 0 MiB 0 0.000 0 0 bid
test1 public pgbench_tellers_pkey pgbench_tellers 0 MiB 0 MiB 0 0.000 0 0 tid

----------------------------------------
/* Profiles */
----------------------------------------
Processing Executes
---------------------------------------------

pg_stats_reporter の使い方

Webレポート

下記のURLへアクセスすると、pg_stats_reporterのレポート画面が出ますので、ブラウザからアクセスして色々操作をしてみてください。

http://<ホスト名>/pg_stats_reporter/pg_stats_reporter.php

pg_stats_reporter のサンプル画面は[こちら]です。

終わりに

今回は pg_statsinfo と pg_stats_reporter を1台のサーバに導入して使い始めることができるところまで紹介しました。
日常的なモニタリングは何かが起こった時の調査に大いに役立ちますので、ぜひ導入を検討してみてください。