Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sqw1g-00EARG-7f for pgsql-general@arkaria.postgresql.org; Wed, 18 Sep 2024 14:51:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sqw1e-00BKOt-Qn for pgsql-general@arkaria.postgresql.org; Wed, 18 Sep 2024 14:51:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sqw1e-00BKOY-Ft for pgsql-general@lists.postgresql.org; Wed, 18 Sep 2024 14:51:26 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sqw1c-001pCe-3o for pgsql-general@lists.postgresql.org; Wed, 18 Sep 2024 14:51:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 48IEpMkB1811801; Wed, 18 Sep 2024 10:51:22 -0400 From: Tom Lane To: Christophe Pettus cc: Wizard Brony , pgsql-general@lists.postgresql.org Subject: Re: CREATE DATABASE command concurrency In-reply-to: References: <1E5F4E2E-EC7C-40E6-905F-F5EF6565F99A@gmail.com> Comments: In-reply-to Christophe Pettus message dated "Wed, 18 Sep 2024 07:21:06 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1811799.1726671082.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 18 Sep 2024 10:51:22 -0400 Message-ID: <1811800.1726671082@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Christophe Pettus writes: >> On Sep 17, 2024, at 14:52, Wizard Brony wrote: >> What are the concurrency guarantees of the CREATE DATABASE command? For= example, is the CREATE DATABASE command safe to be called concurrently su= ch that one command succeeds and the other reliably fails without corrupti= on? > The concern is that two different sessions issue a CREATE DATABASE comma= nd 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=3D# 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