在事务内创建数据库 [英] CREATE DATABASE inside transaction
问题描述
根据 postgresql 文档;
According to postgresql docs;
CREATE DATABASE cannot be executed inside a transaction block.
这有技术原因吗?
推荐答案
当你尝试时,你得到错误:
When you try it, you get the error:
ERROR: CREATE DATABASE cannot run inside a transaction block
这来自 src/backend/access/transam/xact.c
(我的资料中的第 3023 行,但因版本而异),在 PreventTransactionChain(...)
.
This comes from src/backend/access/transam/xact.c
(line 3023 on my sources, but varies by version), in PreventTransactionChain(...)
.
那里的评论解释说:
此例程将由不得在内部运行的语句调用一个事务块,通常是因为它们具有不可回滚副作用或进行内部提交.
This routine is to be called by statements that must not run inside a transaction block, typically because they have non-rollback-able side effects or do internal commits.
对于CREATE DATABASE
,它是从src/backend/tcop/utility.c
在standard_ProcessUtility
中调用的T_CreatedbStmt
,但不幸的是,没有任何内容丰富的评论说明为什么 CREATE DATABASE
在事务中运行不安全.
For CREATE DATABASE
it's called from src/backend/tcop/utility.c
in standard_ProcessUtility
under the case for T_CreatedbStmt
, but unfortunately there isn't any informative comment that says why specifically CREATE DATABASE
isn't safe to run in a transaction.
查看消息来源,我可以看到,一方面它会强制设置检查点.
Looking at the sources, I can see that for one thing it forces a checkpoint.
不过,总的来说,我没有看到任何真正大喊我们不能以交易方式执行此操作"的内容.更多的是我们还没有实现以事务方式执行此操作的功能".
Overall, though, I don't see anything that really screams out "we can't do this transactionally". It's more "we haven't implemented the functionality to do this transactionally".
这篇关于在事务内创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!