Chapter 26. Postgres のシグナル

Note: Contributed by Massimo Dal Zotto

Massimo Dal Zotto氏による寄稿。

Postgres uses the following signals for communication between the postmaster and backends:

Postgres では、以下のシグナルを使用し て、postmaster とバックエンドの間の通信を行ないます。

Table 26-1. Postgres Signals

シグナルpostmaster の動作サーバの動作
SIGHUPkill(*,sighup) を実行します。 read_pg_options() を実行します。
SIGINT終了します。問い合わせを取り止めます。
SIGQUITkill(*,sigterm) を実行します。handle_warn() を実行します。
SIGTERMkill(*,sigterm)、kill(*,9) を実行し、その後終了します。die() を実行します。
SIGPIPE無視します。die() を実行します。 (訳注: 6.5.3 の postgres.c では、SIG_IGN、つまり、無視しています。)
SIGUSR1kill(*,sigusr1) を実行し、その後終了します。quickdie() を実行します。
SIGUSR2kill(*,sigusr2) を実行します。非同期の通知を扱います。(SI キャッシュの書き出しも行ないます。)
SIGCHLDreaper()を実行します。無視します。(試験のためまだコードには残っています。) (訳注: 6.5.3 の postgres.c ではSIG_IGN、つまり無視しています。)
SIGTTIN無視します。 
SIGTTOU無視します。 
SIGCONTdumpstatus を実行します。 (訳注: 6.5.3 の postmaster.c では SIGWINCH に dumpstatus ハンドラを割 り当てています。 SIGCONT は SIG_DFL ハンドラのままでした。例えば、Solaris2.6 では無視さ れることになります。 
SIGFPE FloatExceptionHandler() を実行します。

Note: "kill(*,signal)" means sending a signal to all backends.

"kill(*,signal)" は、全てのバックエンドに引数 signal を送 ることを意味します。

The main changes to the old signal handling are the use of SIGQUIT instead of SIGHUP to handle warns, SIGHUP to re-read the pg_options file and the redirection to all active backends of SIGHUP, SIGTERM, SIGUSR1 and SIGUSR2 sent to the postmaster. In this way these signals sent to the postmaster can be sent automatically to all the backends without need to know their pids. To shut down postgres one needs only to send a SIGTERM to postmaster and it will stop automatically all the backends.

シグナルの取り扱いに関する旧版との大きな違いは、警告を取り扱うため に SIGHUP ではなく SIGQUIT を使用するようにしたこと、pg_options フ ァイルを再読み込みするために SIGHUP を使用するようにしたこと、そし て、postmaster に送られた SIGHUP、SIGTERM、SIGUSR1、SIGUSR2 シグナ ルを全ての有効なバックエンドに再送するようにしたことです。この方法 によって、postmaster に送られたシグナルは、プロセス番号を調査するこ となく、全バックエンドに自動的に送られるようになりました。postgres を停止するためにすることは、postmaster に SIGTERM を送るだけです。こ れにより全てのバックエンドは自動的に停止します。

The SIGUSR2 signal is also used to prevent SI cache table overflow which happens when some backend doesn't process SI cache for a long period. When a backend detects the SI table full at 70% it simply sends a signal to the postmaster which will wake up all idle backends and make them flush the cache.

SIGUSR2 シグナルは、バックエンドが長期間 SI キャッシュを処理しない時 に発生する可能性がある、SI キャッシュテーブルのオーバーフローを防ぐた めにも使われます。あるバックエンドが SI テーブルの使用状況が 70% に達 したことを検出すると、バックエンドは postmaster にシグナルを送ります。 シグナルを受け取った postmaster は、待ち状態になっている全てのバックエ ンドを活性化し、キャッシュを書き出します。

The typical use of signals by programmers could be the following:

# stop postgres
kill -TERM $postmaster_pid
# kill all the backends
kill -QUIT $postmaster_pid
# kill only the postmaster
kill -INT $postmaster_pid
# change pg_options
cat new_pg_options > $DATA_DIR/pg_options
kill -HUP $postmaster_pid
# change pg_options only for a backend
cat new_pg_options > $DATA_DIR/pg_options
kill -HUP $backend_pid
cat old_pg_options > $DATA_DIR/pg_options

以下のものは、よくプログラマが行なうシグナルの使用方法です。

# postgres を停止する。
kill -TERM $postmaster_pid
# 全てのバックエンドを停止する。
kill -QUIT $postmaster_pid
# postmaster のみを停止する。
kill -INT $postmaster_pid
# pg_options を変更する。
cat new_pg_options > $DATA_DIR/pg_options
kill -HUP $postmaster_pid
# バックエンド用の pg_options のみを変更する。
cat new_pg_options > $DATA_DIR/pg_options
kill -HUP $backend_pid
cat old_pg_options > $DATA_DIR/pg_options