CREATE DATABASE command concurrency

Started by Wizard Bronyover 1 year ago4 messagesgeneral
Jump to latest
#1Wizard Brony
wizardbrony@gmail.com

What are the concurrency guarantees of the CREATE DATABASE command? For example, is the CREATE DATABASE command safe to be called concurrently such that one command succeeds and the other reliably fails without corruption?

#2Christophe Pettus
xof@thebuild.com
In reply to: Wizard Brony (#1)
Re: CREATE DATABASE command concurrency

On Sep 17, 2024, at 14:52, Wizard Brony <wizardbrony@gmail.com> wrote:

What are the concurrency guarantees of the CREATE DATABASE command? For example, is the CREATE DATABASE command safe to be called concurrently such that one command succeeds and the other reliably fails without corruption?

The concern is that two different sessions issue a CREATE DATABASE command using the same name? In that case, it can be relied upon that one will succeed (unless it fails for some other reason, like lacking permissions), and the other will receive an error that the database already exists.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Christophe Pettus (#2)
Re: CREATE DATABASE command concurrency

Christophe Pettus <xof@thebuild.com> writes:

On Sep 17, 2024, at 14:52, Wizard Brony <wizardbrony@gmail.com> wrote:
What are the concurrency guarantees of the CREATE DATABASE command? For example, is the CREATE DATABASE command safe to be called concurrently such that one command succeeds and the other reliably fails without corruption?

The concern is that two different sessions issue a CREATE DATABASE command using the same name? In that case, it can be relied upon that one will succeed (unless it fails for some other reason, like lacking permissions), and the other will receive an error that the database already exists.

This is true, but it's possibly worth noting that the specific error
message you get could vary. Normally it'd be something like

regression=# create database postgres;
ERROR: database "postgres" already exists

but in a race condition it might look more like "duplicate key value
violates unique constraint". In the end we rely on the system
catalogs' unique indexes to detect and prevent race conditions of
this sort.

regards, tom lane

#4Muhammad Usman Khan
usman.k@bitnine.net
In reply to: Wizard Brony (#1)
Re: CREATE DATABASE command concurrency

Hi,
In PostgreSQL, it's safe to run CREATE DATABASE at the same time from
different places. If two commands try to create the same database, one will
succeed, and the other will safely fail without causing any problems or
incomplete database creation.

On Wed, 18 Sept 2024 at 19:08, Wizard Brony <wizardbrony@gmail.com> wrote:

Show quoted text

What are the concurrency guarantees of the CREATE DATABASE command? For
example, is the CREATE DATABASE command safe to be called concurrently such
that one command succeeds and the other reliably fails without corruption?