PostgreSQL Server Shutdown modes

In this article, we will different PostgreSQL Server Shutdown modes.

Like other RDMS, in PostgreSQL also there are threes modes in which a shutdown can be performed.

1. Smart Mode
2. Fast Mode
3. Immediate Mode

1. Smart Mode: This is the default mode used to shutdown a PostgreSQL server. If we use this method, Postgres waits for all the connections to exit before shutting it down completely and it does not allow new connections but waits for existing connections.

In this way, all the committed data has been written to disk(data files) and PostgreSQL does not need to perform any crash recovery upon restart.

Example:

pg_ctl -D $PGDATA stop

or

pg_ctl -D $PGDATA stop -ms

2. Fast Mode:

When we shutdown PostgreSQL using fast mode, it terminates all open transactions by setting SIGTERM and does not allow new connections any more. However, upon terminating all the connections, it performs a CHECKPOINT, similar to smart mode to avoid crash recovery upon restart.

Example:

pg_ctl -D $PGDATA stop -mf

3. Immediate Mode:

An immediate mode does an abnormal or forced shutdown that requires crash recovery upon restart. PostgreSQL sends SIQUIT to all processes in order to exit immediately. As it does not perform a clean CHECKPOINT before the shutdown, all the transaction logs(WALs) since the last checkpoint are important upon restart. This is because, upon restart, PostgreSQL reads the WALS from the last checkpoint until latest committed transaction to apply changes to disk(datafiles). This mode is preferred in emergencies because it is quick.

Example:

pg_ctl -D $PGDATA stop -mi