Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1klEuL-0005cy-8e for pgsql-bugs@arkaria.postgresql.org; Fri, 04 Dec 2020 17:30:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1klEuJ-0007f3-UT for pgsql-bugs@arkaria.postgresql.org; Fri, 04 Dec 2020 17:30:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1klEuJ-0007ej-OC for pgsql-bugs@lists.postgresql.org; Fri, 04 Dec 2020 17:30:11 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1klEuH-0000zD-6u for pgsql-bugs@lists.postgresql.org; Fri, 04 Dec 2020 17:30:10 +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 0B4HU6LO2169265; Fri, 4 Dec 2020 12:30:06 -0500 From: Tom Lane To: gatekeeper.mail@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists" In-reply-to: <16763-a1c5f839345c4e80@postgresql.org> References: <16763-a1c5f839345c4e80@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 04 Dec 2020 17:08:29 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2169263.1607103006.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2020 12:30:06 -0500 Message-ID: <2169264.1607103006@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > Our application is run with a number of simultaneous instances each of t= hem > emits both DDL and DML to the DB. > For example, at the very start they ensure base table structure exists s= o > the emit queries like: > CREATE TABLE IF NOT EXISTS tblname (...); > During runtime they also emit queries to create new partitions to existi= ng > partitioned tables. Since both startup and/or runtime DDL could be emite= d > simultaneously they suddenly are. This leads to errors like this: > 2020-12-04 16:38:45.785 GMT [18814] ERROR: relation > "tblname_short_60_2657_3" already exists IF NOT EXISTS doesn't attempt to be bulletproof: it just checks at the start of the command to see if the object name is already there. So it's not sufficient to guard concurrent creations. You can call that a bug if you like, but it's quite unlikely to change anytime soon. You might consider using advisory locks [1] to keep your various sessions from trying to do this at the exact same time. regards, tom lane [1] https://www.postgresql.org/docs/11/functions-admin.html#FUNCTIONS-ADVI= SORY-LOCKS