Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE DATABASE inside transaction

Tags:

postgresql

According to postgresql docs;

CREATE DATABASE cannot be executed inside a transaction block.

Is there a technical reason for this?

like image 692
muriithi Avatar asked Dec 25 '22 10:12

muriithi


1 Answers

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".

like image 90
Craig Ringer Avatar answered Feb 22 '23 10:02

Craig Ringer