#author("2017-12-31T04:45:06+00:00","default:haikikyou","haikikyou")
#author("2017-12-31T14:48:13+00:00","default:haikikyou","haikikyou")
[[PostgreSQL/解析]]

* Postmasterプロセスの処理 [#z2586f99]

** PostmasterMain関数 [#uacaf4f6]

以下は、[[PostmasterMain関数>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l575]]([[postmaster/postmaster.c>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE]])の処理概要である。

+ PostmasterContextの生成&メモリコンテキストのスイッチ
+ シグナルの設定
+ GUCオプションの初期化
+ コマンドラインオプション解析
+ 設定ファイルの読み込み
++ postgresql.conf
++ postgresql.auto.conf
++ pg_hba.conf
++ pg_ident.conf
+ &code(){-C <gucname>};指定があれば出力して終了
-- Ex: &code(postgres -C work_mem);
+ Data directoryの検証
+ WorkingDirectoryの変更( &code(chdir(DataDir) );)
+ GUCオプションの組み合わせのチェック、以下であること
-- &code(superuser_reserved_connection); < &code(max_connections);
-- &code(max_wal_senders); < &code(max_connections);
-- &code(archive_mode);が有効時、&code(wal_level); != &code(minimal);
-- &code(max_wal_senders); > 0 && &code(wal_level); != &code(minimal); 
+ Date token tableのチェック(文字列長、順序)
-- &label(warn){参考}; [[CheckDateTokenTables()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/datetime.c;h=a3d7dc3697a85563397c0ed9a0b296cbf912f775;hb=refs/heads/REL_10_STABLE#l4463]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
+ Lockfile(postmaster.pid)の作成、以下が記録される
++ Postmaster pid
++ Date directory path
++ MyStartTime(起動時刻)
++ Port番号
++ Unix socket directory
++ First listen_address
++ Shared memory key
++ Postmaster status(starting / stopping / ready / standby)
+ SSL有効の場合、SSLライブラリの初期化
+ Logical replication launcherの登録(バックグラウンドワーカ)
+ shared_preload_librariesのプレロード
+ バックエンドプロセス数の初期値設定
-- &code(){max_connections + autovacuum_max_workers + 1 + max_worker_processes};
+ Serverソケット作成
+ Unixドメインソケット作成
+ 共有メモリとセマフォのセットアップ
+ File Descriptor数の利用可能な最大値を計算
-- &label(rel){関連パラメータ};&code(max_files_per_process);
-- 最大数 = &code(){Min(利用可能なFD数, max_files_per_process - オープン済FD数) - NUM_RESERVED_FDS(予約FD数))};
+ スタック深さチェックで使用するベースとなるポインタをセット
-- 深い再帰となる関数呼び出しでスタック深さのチェックが行われる
-- &label(rel){関連パラメータ};&code(max_stack_depth);
+ postmaster監視用のpipeを初期化
+ postmaster.optsファイルの作成
-- ファイルには実行ファイルのパスが書き込まれる。ex: /path/to/postgres
+ &code(){external_pid_file};指定があれば、postmasterのプロセスIDを書き込む
+ 一時ファイルを削除
-- base/pgsql_tmp、一時テーブル
+ スタンバイ昇格リクエストのシグナルファイルを削除
-- &code(){promote};
-- &code(){fallback_promote};
+ ログのメタファイルを削除
-- &code(){current_logfiles};
-- &label(rel){関連};[[update_metainfo_datafile()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/syslogger.c;h=3255b42c7d23ef67ee17f7d71c9ecf9e11f4b4d0;hb=refs/heads/REL_10_STABLE#l1353]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
+ 統計情報コレクタプロセスの初期化
-- UDPで通信、Portは初期化時に自動決定される(getsocknameで取得)
-- &label(warn){参考};[[pgstat_init()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/pgstat.c;h=1f75e2e97d054ea82ce29a3afbe971a30197531a;hb=refs/heads/REL_10_STABLE#l345]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
+ 自動バキュームの設定チェック
-- パラメータ設定のミスがあればwarningが表示される
-- &code(){autovacuum = on};の場合、&code(){track_counts = on};であること
+ &code(){pg_hba.conf};のロード
+ &code(){pg_ident.conf};のロード
+ マルチスレッドが否かのチェック、マルチスレッドでpostmasterが実行されているとFATALで終了する
+ postmasterの起動タイムスタンプを保存
+ ロックファイル(&code(){postmaster.pid};)にpostmasterのステータス&code(){starting};を書き込む
+ StartupProcessの起動
-- &label(warn){参考};[[StartChildProcess()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l5311]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
-- fork()してpostmasterの子プロセスとして起動する。処理の終了と終了コードはシグナルでpostmasterに通知される(SIGCHILD)。
-- 
+ StartupStatusを&code(){STARTUP_RUNNING};にする
-- &code(){STARTUP_NOT_RUNNING --> STARTUP_RUNNING};
+ postmasterのステータスを&code(){PM_STARTUP};にする
-- &label(warn){参考};[[PMState>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l322]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
+ 起動すべきバックグラウンドワーカがあれば起動
-- &label(warn){参考};[[maybe_start_bgworkers()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l5837]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
+ ServerLoop()の呼び出し

* ServerLoop関数 [#f048e33e]

+ selectシステムコールのディスクリプタ集合のreadmaskを初期化
+ pmStateがPM_WAIT_DEAD_ENDの場合(selectをコールしない、子プロセスのshutdown待ち状態である)
++ シグナルをアンブロック
++ 100msecのスリープ
++ シグナルをブロック
+ pmStateがPM_WAIT_DEAD_ENDでない場合
++ ServerLoopのスリープ時間の決定
++ シグナルをアンブロック
++ selectシステムコールでソケットの監視
++ シグナルをブロック
+ リクエストを処理
++ accept
++ バックエンドの起動 -> &label(warn){参考};[[BackendStartup()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l4002]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};

''postmasterの続き''

+ Log Collectorが有効で且つ動作していない場合、起動
+ CheckPointerが動作していない場合、起動
+ BackgroundWriterが動作していない場合、起動
+ WALWriterが動作していない場合、起動
+ autovacuum launcherが動作していない場合、起動
+ stats collectorが動作していない場合、起動
+ archiverが動作していない場合、起動
+ auto vacuum launcherにシグナル(SIGUSR2)を送信する必要があれば、送信する
+ WALReceiverを起動する必要があれば、起動
+ 起動すべきバックグラウンドワーカ(クラッシュ含め)があれば、起動
+ shutdownが遅い子プロセスに対し、シグナル(SIGKILL)を送信。
-- この以前にpostmasterはシグナル(SIGQUIT)を受信後、子プロセスにシグナル(SIGQUIT)を送信している
+ Data Directoryのロックファイル(postmaster.pid)が不正な場合、postmasterにシグナル(SIGQUIT)(immediate shutdown)が送信される
-- ファイルが読み取れること、pidが一致していること
-- 前回チェック時から60sec以上の間隔で実施
+ ソケットとロックファイルのアクセス時刻と修正時刻を更新する(&code(){utime};)
-- 前回チェック時から58sec以上の間隔で実施


* 参考リンク [#d19a863b]

- [[postmaster/postmaster.c>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
- [[PostmasterMain()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l571]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};
- [[ServerLoop()>https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/postmaster/postmaster.c;h=a8e19c92381dc0de643b1837149ff148026ce175;hb=refs/heads/REL_10_STABLE#l1670]] - &size(11){&color(gray){https://git.postgresql.org/gitweb/};};


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP