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

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