PostgreSQL三种关闭方式的区别

69 阅读2分钟

1、使用smart参数关闭数据库:
smart:最为安全,但最慢,需要将所有连接都断开后,才会关库,默认关库模式。
smart: 等所有的连接中止后,关闭数据库。如果客户端连接不终止, 则无法关闭数据库。
[postgresql@master ~]pg_ctlstopDpg\_ctl stop -DPGDATA -m smart
waiting for server to shut down.... done
server stopped
[postgresql@master ~]$

使用smart参数关闭数据库的日志:
2025-09-06 19:40:20.479 CST [56233] LOG: received smart shutdown request
2025-09-06 19:40:20.480 CST [56233] LOG: background worker "logical replication launcher" (PID 56241) exited with exit code 1
2025-09-06 19:40:20.480 CST [56236] LOG: shutting down
2025-09-06 19:40:20.485 CST [56233] LOG: database system is shut down

2、使用fast参数关闭数据库:
fast:强制中断会话,而不管有操作有没有提交,在做系统维护(系统维护时一般应用都正常关闭了,或者不再会有事务操作。)时,需要这种模式来关闭数据库。
fast: 快速关闭数据库, 断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。
[postgresql@master ~]pg_ctlstopDpg\_ctl stop -DPGDATA -m fast
waiting for server to shut down.... done
server stopped
[postgresql@master ~]

使用fast参数关闭数据库的日志:
2025-09-06 19:40:59.477 CST [56258] LOG: received fast shutdown request
2025-09-06 19:40:59.478 CST [56258] LOG: aborting any active transactions
2025-09-06 19:40:59.478 CST [56258] LOG: background worker "logical replication launcher" (PID 56266) exited with exit code 1
2025-09-06 19:40:59.479 CST [56261] LOG: shutting down
2025-09-06 19:40:59.484 CST [56258] LOG: database system is shut down

3、使用immediate参数关闭数据库:
immediate:最暴力的方式,不管数据有没有落盘(POSGRE是遵循WAL机制),就直接关掉,待启动时进行实例恢复,如果在关库前有大量的事务没有写入磁盘,那这个恢复过程可能会非常的漫长。
immediate: 立即关闭数据库,立即停止数据库进程,直接退出,下次启动时会进行实例恢复。
[postgresql@master ~]pg_ctlstopDpg\_ctl stop -DPGDATA -m immediate
waiting for server to shut down.... done
server stopped
[postgresql@master ~]$

使用immediate参数关闭数据库的日志:
2025-09-06 19:41:40.311 CST [56278] LOG: received immediate shutdown request
2025-09-06 19:41:40.313 CST [56284] WARNING: terminating connection because of crash of another server process
2025-09-06 19:41:40.313 CST [56284] DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2025-09-06 19:41:40.313 CST [56284] HINT: In a moment you should be able to reconnect to the database and repeat your command.
2025-09-06 19:41:40.315 CST [56278] LOG: database system is shut down

4、数据目录:
[postgresql@master ~]echoechoPGDATA
/usr/local/postgresql/data

5、启动数据库:
[postgresql@master ~]pg_ctlstartDpg\_ctl start -DPGDATA
waiting for server to start....2025-09-06 19:40:40.221 CST [56258] LOG: redirecting log output to logging collector process
2025-09-06 19:40:40.221 CST [56258] HINT: Future log output will appear in directory "log".
done
server started
[postgresql@master ~]$

行业拓展

分享一个面向研发人群使用的前后端分离的低代码软件——JNPF

基于 Java Boot/.Net Core双引擎,它适配国产化,支持主流数据库和操作系统,提供五十几种高频预制组件,内置了常用的后台管理系统使用场景和实用模版,通过简单的拖拉拽操作,开发者能够高效完成软件开发,提高开发效率,减少代码编写工作。

JNPF基于SpringBoot+Vue.js,提供了一个适合所有水平用户的低代码学习平台,无论是有经验的开发者还是编程新手,都可以在这里找到适合自己的学习路径。

此外,JNPF支持全源码交付,完全支持根据公司、项目需求、业务需求进行二次改造开发或内网部署,具备多角色门户、登录认证、组织管理、角色授权、表单设计、流程设计、页面配置、报表设计、门户配置、代码生成工具等开箱即用的在线服务。