From: Tom Lane <tgl@sss.pgh.pa.us>
To: gatekeeper.mail@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists"
Date: Fri, 04 Dec 2020 12:30:06 -0500
Message-ID: <2169264.1607103006@sss.pgh.pa.us> (raw)
In-Reply-To: <16763-a1c5f839345c4e80@postgresql.org>
References: <16763-a1c5f839345c4e80@postgresql.org>
PG Bug reporting form <noreply@postgresql.org> writes:
> Our application is run with a number of simultaneous instances each of them
> emits both DDL and DML to the DB.
> For example, at the very start they ensure base table structure exists so
> the emit queries like:
> CREATE TABLE IF NOT EXISTS tblname (...);
> During runtime they also emit queries to create new partitions to existing
> partitioned tables. Since both startup and/or runtime DDL could be emited
> 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-ADVISORY-LOCKS
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, gatekeeper.mail@gmail.com, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists"
In-Reply-To: <2169264.1607103006@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox