Postgresql 中文操作指南

19.5. Shutting Down the Server #

有几种方法可以关闭数据库服务器。在底层,它们都缩减为向 postgres 进程发送一个信号。

There are several ways to shut down the database server. Under the hood, they all reduce to sending a signal to the supervisor postgres process.

如果您使用的是 PostgreSQL 的预打包版本,并且您使用了其启动服务器的配置,那么您还应该使用其配置来停止服务器。有关详细信息,请查阅包级别文档。

If you are using a pre-packaged version of PostgreSQL, and you used its provisions for starting the server, then you should also use its provisions for stopping the server. Consult the package-level documentation for details.

直接管理服务器时,可以通过向 postgres 进程发送不同的信号来控制关闭类型:

When managing the server directly, you can control the type of shutdown by sending different signals to the postgres process:

  • SIGTERM

    • This is the Smart Shutdown mode. After receiving SIGTERM, the server disallows new connections, but lets existing sessions end their work normally. It shuts down only after all of the sessions terminate. If the server is in recovery when a smart shutdown is requested, recovery and streaming replication will be stopped only after all regular sessions have terminated.

  • SIGINT

    • This is the Fast Shutdown mode. The server disallows new connections and sends all existing server processes SIGTERM, which will cause them to abort their current transactions and exit promptly. It then waits for all server processes to exit and finally shuts down.

  • SIGQUIT

    • This is the Immediate Shutdown mode. The server will send SIGQUIT to all child processes and wait for them to terminate. If any do not terminate within 5 seconds, they will be sent SIGKILL. The supervisor server process exits as soon as all child processes have exited, without doing normal database shutdown processing. This will lead to recovery (by replaying the WAL log) upon next start-up. This is recommended only in emergencies.

pg_ctl 程序提供了一个便利的界面,用于发送这些信号关闭服务器。对于非-Windows 系统,您也可以使用 kill 直接发送信号。可以使用 ps 程序查找 postgres 进程的 PID,或从数据目录中的 postmaster.pid 文件中查找。例如,要进行快速关机:

The pg_ctl program provides a convenient interface for sending these signals to shut down the server. Alternatively, you can send the signal directly using kill on non-Windows systems. The PID of the postgres process can be found using the ps program, or from the file postmaster.pid in the data directory. For example, to do a fast shutdown:

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

Important

最好不要使用 SIGKILL 关闭服务器。这样做将阻止服务器释放共享内存和信号。此外,SIGKILL 会在不将其转发给其子进程的情况下杀死 postgres 进程,因此可能还需要手动杀死各个子进程。

It is best not to use SIGKILL to shut down the server. Doing so will prevent the server from releasing shared memory and semaphores. Furthermore, SIGKILL kills the postgres process without letting it relay the signal to its subprocesses, so it might be necessary to kill the individual subprocesses by hand as well.

要终止单个会话,同时允许其他会话继续,使用 pg_terminate_backend()(参见 Table 9.90),或给与该会话关联的子进程发送 SIGTERM 信号。

To terminate an individual session while allowing other sessions to continue, use pg_terminate_backend() (see Table 9.90) or send a SIGTERM signal to the child process associated with the session.