PostgreSQL 13新特性:drop database force

在PostgreSQL中,当我们删除删除数据库时,如果该库上还存在会话连接,那么将无法删除,你可能会看到这样的报错:

bill=# drop database db1;
ERROR:  database "db1" is being accessed by other users
DETAIL:  There is 1 other session using the database.

PostgreSQL13中drop database新增了force选项:即使数据库中仍存在连接,也可以强制删除数据库并断开连接。

Improve the performance when replaying DROP DATABASE commands when many tablespaces are in use (Fujii Masao)

但是需要注意:
1、当这个数据库有未结束的2pc事务,激活的logical replication slot,或者逻辑订阅时,不会kill session。
2、当没有terminate 权限时,报错。

例子:

bill=# drop database db1 with ( force );
DROP DATABASE

当我们再在原先的连接上进行操作时,就会收到连接已经断开的提示:

FATAL:  terminating connection due to administrator command
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

参考链接:
https://www.postgresql.org/docs/13/sql-dropdatabase.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章