From: Tom Lane <tgl@sss.pgh.pa.us>
To: postgresql@noetika.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15361: Add column if not exists create duplicate constraint
Date: Sat, 01 Sep 2018 14:25:02 -0400
Message-ID: <8965.1535826302@sss.pgh.pa.us> (raw)
In-Reply-To: <153582193030.31898.16505654463153733444@wrigleys.postgresql.org>
References: <153582193030.31898.16505654463153733444@wrigleys.postgresql.org>
=?utf-8?q?PG_Bug_reporting_form?= <noreply@postgresql.org> writes:
> I have a patching script that is supposed to add column if not existing :
> ALTER TABLE myschem.table1
> ADD COLUMN IF NOT EXISTS col1 VARCHAR(254) REFERENCES
> myschem.table2(col2)
> When col1 already exists, I expected that nothing would happen.
> [ but actually, it creates an FK constraint anyway ]
As I've said many times before, I hate CREATE IF NOT EXISTS with a
passion, because its semantics are so squishy. This is a perfect example
of that: it's impossible to make a principled decision whether this is a
bug or not, or what the correct behavior is if you think it's a bug.
Should the command do nothing at all if col1 exists, regardless of
whether there's an FK constraint or not? Should it avoid creating
a duplicate constraint, and if so how picky are we to be about what
"duplicate" means? What happens if myschem.table2(col2) doesn't exist?
Not to mention whether we should change the behavior for other secondary
objects that might be shown in the command, such as UNIQUE or CHECK
constraints. Right now all of those get added, possibly redundantly,
just like FK constraints.
I believe this exact issue was debated when ADD COLUMN IF NOT EXISTS
was added, and the camp that wanted it thought this behavior was fine.
Even if we were now to conclude that this is a bug and agree on what'd be
better semantics, there would be a pretty strong backwards-compatibility
argument against changing it; some people's scripts might expect the
constraint(s) to get added.
The short answer is that IF NOT EXISTS gives you no guarantees whatsoever
about the subsequent properties of the object, only that something by
that name will exist. If you don't like that, don't use IF NOT EXISTS.
regards, tom lane
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, postgresql@noetika.com
Subject: Re: BUG #15361: Add column if not exists create duplicate constraint
In-Reply-To: <8965.1535826302@sss.pgh.pa.us>
References: <153582193030.31898.16505654463153733444@wrigleys.postgresql.org> <8965.1535826302@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