“Smart” mode waits for all active clients to disconnect and any online backup to finish.
    If the server is in hot standby, recovery and streaming replication will be terminated once all clients have disconnected.

    “Fast” mode (the default) does not wait for clients to disconnect and will terminate an online backup in progress.
    All active transactions are rolled back and clients are forcibly disconnected, then the server is shut down.

    “Immediate” mode will abort all server processes immediately, without a clean shutdown.
    This choice will lead to a crash-recovery cycle during the next server start.

    smart :等用户进程自然退出。最后做检查点。

    fast : 主动断开用户进程。最后做检查点。

    除了用户进程,还有归档进程、walsender进程。smart,fast停库时,这些进程又会如何处理呢?

    发起最后一次archive周期,将所有.ready的wal进行归档,除非中间archive_command遇到错误,否则要等所有的.ready文件都触发并执行完成archive_command。

    如果有walsender进程存在(例如有standby,有pg_basebackup,有pg_receivewal等利用流复制协议的客户端就有walsender进程),那么要等这个walsender将所有未发送完的wal日志都发送给下游。

    src/backend/postmaster/postmaster.c

    注释如下

    src/backend/postmaster/pgarch.c

    归档所有未归档日志,直到全部的.ready对应调度wal都归档完成,或者报错

    1、如果walsender有很多很多的wal没有发送完,则停库可能要很久很久(因为要等walsender发完)

    2、同样的道理,如果有很多很多文件没有归档,并且归档过程中没有报错,则一个归档周期会非常漫长,也会导致停库可能要很久很久。

    immediate模式停库没有影响,但是immediate停库不写检查点,启动数据库时需要进入recovery模式恢复数据库。