According to postgresql docs;
CREATE DATABASE cannot be executed inside a transaction block.
Is there a technical reason for this?
When you try it, you get the error:
ERROR: CREATE DATABASE cannot run inside a transaction block
This comes from src/backend/access/transam/xact.c
(line 3023 on my sources, but varies by version), in PreventTransactionChain(...)
.
The comment there explains that:
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With