Database/PostgreSQL

7. PostgreSQL Server Shutdown

겸겸사 2017. 11. 6. 15:04

이번 장에서 다룰 내용은 간단한 PostgreSQL 데몬을 Shutdown 하는 방법에 대한 가이드 내용입니다.


https://postgresql.kr/docs/9.6/server-shutdown.html

https://www.postgresql.org/docs/10/static/server-shutdown.html


위의 2개의 문서를 참조 하시면 됩니다. 


Shutdown 을 위한 3가지 SIG 이 있습니다. 


SIGTERM, SIGINT, SIGQUIT 각각의 방식이 어떻게 동작하는지에 대해 정리를 하도록 하겠습니다. 


[SIGTERM]

- Smart Shutdown mode 입니다. 

- SIGTERM 을 수신한 후 서버는 새로운 connection 에 대해서 연결을 허용하지 않습니다. 하지만 기존의 연결은 정상종료를 허용 합니다. 

- Shutdown 은 모든 세션이 terminate 가 된 후 진행 됩니다. 

- Server 가 online backup mode 인 경우 online backup mode 가 작동하지 않을때까지 대기 합니다. 

- Backup mode 인경우 새로운 연결은 superusers 에게 연결이 됩니다. (online backup mode 의 연결을 허용하기 위해 이러한 예외가 허용)

- 서버 복구 진행중 smart shutdown 을 요청할 경우, 모든  regular sessions 이 terminate 된 이후 복구 및 streaming replication 이 stop 됩니다.

 

[SIGINT]

- Fast Shutdown mode 입니다. 

- 모든 신규 연결을 허용하지 않고, SIGTERM 을 전송합니다. current transactions 이 중단되고 즉시 종료 됩니다. 

- 모든 서버 프로세스가 종료된 후 Shutdown 됩니다. 

- online backup mode 인경우 backup useless 가 표시되고, backup mode 가 terminate 됩니다. 


[SIGQUIT]

- Immediate Shutdown mode 입니다. (즉시 shutdown)

- 서버에 SIGQUIT 가 전송되면 해당 SIG 를 자식 프로세스에 전송하고 자식 프로세스가 종료될때가지 대기합니다. 

- 5초 이내에 종료되지 않은 프로세스는 마스터 postgres 프로세스가 다시 SIGKILL 을 보내 바로 종료를하며, 자기자신 postgres 도 즉시중지됩니다.

- 다음 서버 시작시에 복구 작업을 진행합니다. (WAL log replaying)

- 비상시에만 사용하기를 권장 합니다. 



pg_ctl 명령어는 이러한 server shutdown 을 위한 편리한 인터페이스를 제공합니다. 


[postgres@ip-10-90-10-49 ~]$ /usr/local/pgsql/bin/pg_ctl --help

pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.


Usage:

  pg_ctl init[db] [-D DATADIR] [-s] [-o OPTIONS]

  pg_ctl start    [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]

                  [-o OPTIONS] [-p PATH] [-c]

  pg_ctl stop     [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]

  pg_ctl restart  [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]

                  [-o OPTIONS] [-c]

  pg_ctl reload   [-D DATADIR] [-s]

  pg_ctl status   [-D DATADIR]

  pg_ctl promote  [-D DATADIR] [-W] [-t SECS] [-s]

  pg_ctl kill     SIGNALNAME PID


Common options:

  -D, --pgdata=DATADIR   location of the database storage area

  -s, --silent           only print errors, no informational messages

  -t, --timeout=SECS     seconds to wait when using -w option

  -V, --version          output version information, then exit

  -w, --wait             wait until operation completes (default)

  -W, --no-wait          do not wait until operation completes

  -?, --help             show this help, then exit

If the -D option is omitted, the environment variable PGDATA is used.


Options for start or restart:

  -c, --core-files       allow postgres to produce core files

  -l, --log=FILENAME     write (or append) server log to FILENAME

  -o, --options=OPTIONS  command line options to pass to postgres

                         (PostgreSQL server executable) or initdb

  -p PATH-TO-POSTGRES    normally not necessary


Options for stop or restart:

  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"


Shutdown modes are:

  smart       quit after all clients have disconnected

  fast        quit directly, with proper shutdown (default)

  immediate   quit without complete shutdown; will lead to recovery on restart


Allowed signal names for kill:

  ABRT HUP INT QUIT TERM USR1 USR2



Windows 가 아닌 시스템에서는 kill 을 이용해서 직접 SIG 를 전송할수도 있습니다. 

$ kill -INT `head -1 /usr/local/pgsql/data/postmaster.pid`

SIGKILL 은 사용하지 않는것이 최선 입니다. 사용할 경우 서버에서 shared memory 및 semaphores  해제 되지 않아서, 새로 구동할 경우 수동으로 해지를 해야하는 상황이 생길수도 있습니다. 
SIGKILL 은 하위 프로세스로 신호를 전달하지 않고 postgres 만 kill 하기에 하위 프로세스들에게 직접 kill 을 해줘야하는 불편함이 생깁니다.