Re: BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists"

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: gatekeeper(dot)mail(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists"
Date: 2020-12-04 17:30:06
Message-ID: 2169264.1607103006@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andy S 2020-12-04 17:43:38 Re: BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists"
Previous Message PG Bug reporting form 2020-12-04 17:08:29 BUG #16763: CREATE TABLE IF NOT EXISTS fails with "relation exists"