内部構造から学ぶPostgreSQL 設計・運用計画の鉄則 ch1,2途中まで

OSS-DBPostgreSQL勉強メモ

出典: 


データベースとしての分類

  • オブジェクトリレーショナルデータベース

    • オブジェクトデータベースの能力も兼ね備える

歴史

  • PostgreSQL 8.0

    • リカバリ、自動バキューム
  • PostgreSQL 9.0

    • レプリケーション
    • マルチコア環境での性能改善

ライセンス

コミュニティ

  • JPUG

    • 日本のPostgreSQLコミュニティ
    • PostgreSQL文書の日本語訳対応
  • 開発コミュニティ

    • versioning

      • メジャーアップデート: 年1

        • 5年サポート
      • マイナーアップデート: 数カ月に1回
    • なるべく最新を使おう

      • upgrading

        • pg_dumpallを使う
        • pg_upgradeを使う
        • Replicationを使う

プロセス構成

ps -ef | grep postgres
postgres     1     0  0 15:12 ?        00:00:00 postgres
postgres    57     1  0 15:12 ?        00:00:00 postgres: checkpointer   
postgres    58     1  0 15:12 ?        00:00:00 postgres: background writer   
postgres    59     1  0 15:12 ?        00:00:00 postgres: walwriter   
postgres    60     1  0 15:12 ?        00:00:00 postgres: autovacuum launcher   
postgres    61     1  0 15:12 ?        00:00:00 postgres: stats collector   
postgres    62     1  0 15:12 ?        00:00:00 postgres: logical replication launcher   

マスタサーバプロセス

  • これ
postgres     1     0  0 15:12 ?        00:00:00 postgres
  • 起動時に後述のいろいろなプロセスをforkする

ライタプロセス

  • (見当たらない)
  • writerという名前
  • 共有バッファ内の更新されたページを、対応するデータファイルに書き出す
  • 共有バッファ: 更新・検索などのデータ操作を行う

    • shared_buffersパラメータで設定
    • デフォルト16384 * 8kB = 128MB
    • restartで設定反映
postgres=# SELECT name,setting,unit,context FROM pg_settings WHERE name='shared_buffers';
      name      | setting | unit |  context   
----------------+---------+------+------------
 shared_buffers | 16384   | 8kB  | postmaster
(1 row)

WALライタプロセス

  • これ
postgres    59     1  0 15:12 ?        00:00:00 postgres: walwriter   
  • WALバッファに書き込まれたWALを設定にしたがってWALファイルに書き出す
  • WAL: Write Ahead Log

    • データファイル(テーブルやインデックスがある)に変更を加える前に、ログを書き出そうというやつ
    • ディスク書き込みの数も著しく減らせる

      • トランザクションがコミットされたことを保証すためにログファイルだけがあればいい
      • データファイルに逐一書き込まなくていい
    • オンラインバックアップ、PITRにも使う

      • WALをリプレイすることで任意の時点を再現できる

        • roll forward
        • REDOとも

チェックポインタプロセス

  • これ
postgres    57     1  0 15:12 ?        00:00:00 postgres: checkpointer   
  • チェックポイントを設定に従い自動的に実行する
  • チェックポイント

    • すべてのダーティページをデータファイルに反映
    • 特殊なチェックポイントレコードをログファイルに書き込む
    • REDOの起点

自動バキュームランチャ/ワーカプロセス

  • ランチャはこれ
postgres    60     1  0 15:12 ?        00:00:00 postgres: autovacuum launcher   
  • autovacuum daemon
  • 大量の更新(insert, update, delete)のあったテーブルを検出し、VACUUMとANALYZEを自動実行
  • 検出には統計情報を用いるので、track_countstrueである必要がある
postgres=# SELECT name,setting,context FROM pg_settings WHERE name IN ('autovacuum','track_counts');
     name     | setting |  context  
--------------+---------+-----------
 autovacuum   | on      | sighup
 track_counts | on      | superuser
(2 rows)
  • autovacuum, track_countsともにデフォルトon
  • autovacuumsighup
  • track_countssuperuser
  • autovacuum launcher: ずっといる
  • autovacuum worker: launcherにより起動

統計情報コレクタ

  • これ
postgres    61     1  0 15:12 ?        00:00:00 postgres: stats collector   
  • 統計情報コレクタ
  • 稼働統計情報を収集

    • track_activities

      • 現在実行中のコマンドの監視
    • track_counts

      • テーブルやインデックスのアクセス統計情報を集めるか
    • track_functions

      • ユーザ定義関数の使用を追跡
    • track_io_timing

      • ブロックのRead/Writeの監視
postgres=#  SELECT name,setting,context FROM pg_settings WHERE name SIMILAR TO 'track\_%';
           name            | setting |  context   
---------------------------+---------+------------
 track_activities          | on      | superuser
 track_activity_query_size | 1024    | postmaster
 track_commit_timestamp    | off     | postmaster
 track_counts              | on      | superuser
 track_functions           | none    | superuser
 track_io_timing           | off     | superuser
(6 rows)

バックエンドプロセス

ps -efw | grep postgres
UID        PID  PPID  C STIME TTY          TIME CMD
postgres     1     0  0 15:12 ?        00:00:00 postgres
postgres    57     1  0 15:12 ?        00:00:00 postgres: checkpointer   
postgres    58     1  0 15:12 ?        00:00:00 postgres: background writer   
postgres    59     1  0 15:12 ?        00:00:00 postgres: walwriter   
postgres    60     1  0 15:12 ?        00:00:00 postgres: autovacuum launcher   
postgres    61     1  0 15:12 ?        00:00:00 postgres: stats collector   
postgres    62     1  0 15:12 ?        00:00:00 postgres: logical replication launcher   
postgres   201     0  0 16:08 pts/0    00:00:00 bash
postgres   206   201  0 16:08 pts/0    00:00:00 /usr/lib/postgresql/12/bin/psql
postgres   214     1  0 16:08 ?        00:00:00 postgres: postgres postgres [local] idle
postgres   221     0  0 16:09 pts/1    00:00:00 ps -efw
  • クライアント(psql)を実行するとマスタサーバプロセスからバックエンドプロセスがforkされる
postgres   214     1  0 16:08 ?        00:00:00 postgres: postgres postgres [local] idle
  • ユーザ名 データベース名 [接続] 状態 というプロセス名

    • 上のは、postgresユーザで、postgresデータベースにUnixドメインソケットで接続。idle状態

パラレルワーカプロセス

  • パラレルクエリ

    • マルチコアでのクエリ応答高速化
  • パラレルクエリが実行される際にバックエンドプロセスから起動される

    • PostgreSQL 9.6~