SoFunction
Updated on 2024-11-10

Three Modes of Shutting Down Database Services with PostgreSQL

PostgreSQL provides three different ways to shut down a database service, all of which culminate in sending a shutdown signal to the postgres master service process.

Intelligent Shutdown Mode

Smart Shutdown mode sends a SIGTERM signal to the postgres master process. The server does not allow new client connections and waits for existing sessions to complete their work. The service is shut down when all sessions have voluntarily terminated their connections. If a shutdown command is sent while the database service is executing a recovery operation, both the recovery operation and stream replication wait for all regular sessions to terminate before stopping.

The command to shut down the database service using the pg_ctl utility is as follows:

$ pg_ctl stop -m smart

where the -m parameter is used to specify the shutdown mode and smart indicates smart mode.

The PostgreSQL smart shutdown mode is similar to the shutdown normal mode in Oracle databases.

Quick Close Mode

The signal for Fast Shutdown mode is SIGINT, in which the server does not allow new client connections, sends a SIGTERM signal to all service processes, rolls back ongoing transactions and forcibly disconnects all clients, and then shuts down the database.

The command to quickly shut down the database service using the pg_ctl utility is as follows:

$ pg_ctl stop -m fast

$ pg_ctl stop

where fast means fast mode, which is also the default mode.

The PostgreSQL fast shutdown mode is similar to the shutdown immediate mode in Oracle databases.

Immediate shutdown mode

The third mode is Immediate Shutdown, which corresponds to the system signal SIGQUIT.

The master server process sends a SIGQUIT signal to all child processes, and if the child processes do not terminate within 5 seconds, it continues to send a SIGKILL signal for immediate termination. When all child processes exit, the master server process terminates immediately and does not perform the regular database shutdown process. This mode will result in the need to perform a recovery operation (redoing the WAL logs) the next time the database service is started, and is recommended only in emergency situations.

The command to shut down the database service immediately using the pg_ctl utility is as follows:

$ pg_ctl stop -m immediate

where immediate indicates immediate mode.

The PostgreSQL fast shutdown mode is similar to the shutdown abort mode in Oracle databases.

caveat

Of the three shutdown modes, smart mode is the safest and ensures data integrity and consistency; however, this mode can be time-consuming because it requires waiting for the client to actively disconnect. Fast mode may lead to the interruption of transactions, but does not lead to data inconsistency, the advantage is that it is faster, and is generally recommended to use this mode. Immediate mode is the fastest, but may lead to data inconsistency, and can be restored to a consistent state by WAL log playback when restarted, and is recommended only in emergencies or when other modes cannot be turned off.

Operating systems other than Windows can also use the kill command to signal a database shutdown directly, for example:

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

file stores the PID of the master service process (postgres).

It is not recommended to use the SIGKILL signal to shut down a service, as this approach prevents the service from freeing shared memory and signal volumes. Also, this method terminates the master server process without sending information to the child processes, so you will also need to manually shut down each child process.

Oracle Database also supports a shutdown transactional mode, which does not allow new clients to connect, but waits for the ongoing transaction to complete its commit, disconnects the client, and then shuts down the database.PostgreSQL does not have a corresponding shutdown mode.

This article about PostgreSQL shutdown database service of the three modes of the article is introduced to this, more related to PostgreSQL shutdown database service content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!