digoal
2019-12-04
PostgreSQL , drop database force
删除数据库时,需要确保这个数据库上没有其他session,如果有,drop database将失败。
https://www.postgresql.org/docs/devel/sql-dropdatabase.html
DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, ...] ) ]
where option can be:
FORCE
FORCE
Attempt to terminate all existing connections to the target database. It doesn't terminate if prepared transactions, active logical replication slots or subscriptions are present in the target database.
This will fail if the current user has no permissions to terminate other connections. Required permissions are the same as with pg_terminate_backend, described in Section 9.27.2. This will also fail if we are not able to terminate connections.
PG 13新增force接口,自动terminate 被删除数据库上的当前连接。但是注意:
1、当这个数据库有未结束的2pc事务,激活的logical replication slot,或者逻辑订阅时,不会kill session.
2、当没有terminate 权限时,报错。
force一定程度上简化了drop database因为有连接(例如心跳,客户端连接未断开等)造成的drop database失败的问题。
您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.