From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stephen Frost <sfrost@snowman.net>
Cc: postgresql@noetika.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15361: Add column if not exists create duplicate constraint
Date: Sun, 02 Sep 2018 12:38:03 -0400
Message-ID: <8930.1535906283@sss.pgh.pa.us> (raw)
In-Reply-To: <20180902114515.GB4184@tamriel.snowman.net>
References: <153582193030.31898.16505654463153733444@wrigleys.postgresql.org>
<8965.1535826302@sss.pgh.pa.us>
<9734.1535827326@sss.pgh.pa.us>
<20180902114515.GB4184@tamriel.snowman.net>
Stephen Frost <sfrost@snowman.net> writes:
> * Tom Lane (tgl@sss.pgh.pa.us) wrote:
>> The thing I was half-remembering was probably this bug report about
>> the unique-constraint aspect of the same problem:
>> https://www.postgresql.org/message-id/flat/152509815280.19803.16118194452213577808%40wrigleys.postgr...
>> wherein it was argued that if the IF NOT EXISTS fires, it should prevent
>> all side-effects of the ADD COLUMN clause it's attached to, whether or not
>> any of the subsidiary objects exist (in some form) already. If you buy
>> that theory then there's a sketch for a fix there. Nobody's done anything
>> about it AFAIK.
> For my 2c, at least, I continue to be of the opinion (as it seems the OP
> is also..) that IF NOT EXISTS means "ONLY DO THIS IF THE OBJECT
> REFERENCED DOESN'T EXIST".
The problem is precisely that it's very fuzzy exactly what THIS is.
The case at hand, with an FK constraint, is maybe even a better
demonstration of that than the cases we considered previously.
Currently, if the column exists but lacks an FK constraint, the code
will make sure one gets added; with what you propose, it won't.
You can argue all day about which semantics are more useful, but I don't
see that there's a bright line dividing right from wrong here ---
especially since, AFAICS, there's not currently any way for a user to
get the other behavior if she doesn't like the one we choose.
The latter point could be addressed if there were also an IF NOT EXISTS
syntax for constraints. Then the useful aspect of the current behavior
could be duplicated with
ALTER TABLE t
ADD COLUMN IF NOT EXISTS c1 int,
ADD CONSTRAINT IF NOT EXISTS c1_fk FOREIGN KEY (c1) REFERENCES othert(c2);
I'd imagine that we'd address the "what is it that doesn't exist" issue
by saying that you have to attach IF NOT EXISTS to named-constraint
syntax, and what's checked is just the existence of a constraint by that
name, not whether its properties are the same. So this isn't an exact
replacement for what happens now, but it's probably close enough to
cover the real use-cases.
> I still consider the current behavior to be
> a bug, but I could see an argument for not back-patching it out of
> concern about breaking scripts in a back-patch and because the code
> looks like it'd need to be whacked around some.
It's definitely not going to be a back-patchable change. But I could
get behind a change that addresses the confusion without taking away
arguably-useful functionality.
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, sfrost@snowman.net, postgresql@noetika.com
Subject: Re: BUG #15361: Add column if not exists create duplicate constraint
In-Reply-To: <8930.1535906283@sss.pgh.pa.us>
References: <153582193030.31898.16505654463153733444@wrigleys.postgresql.org> <8965.1535826302@sss.pgh.pa.us> <9734.1535827326@sss.pgh.pa.us> <20180902114515.GB4184@tamriel.snowman.net> <8930.1535906283@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