From: Jamie Strachan <jstrachan@counterpath.com>
To: 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: Re: BUG #15361: Add column if not exists create duplicate constraint
Date: Thu, 20 Sep 2018 17:27:13 -0400
Message-ID: <6ac7e958-1c93-934a-3656-67340134823b@counterpath.com> (raw)
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>
On 2018-09-02 12:38 PM, Tom Lane wrote:
> 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.
I would like to suggest that to find the bright line, we look at the "IF
NOT EXISTS" of the CREATE TABLE command:
|IF NOT EXISTS|
Do not throw an error if a relation with the same name already
exists. A notice is issued in this case. Note that there is no
guarantee that the existing relation is anything like the one
that would have been created.
So, that version doesn't automatically fix the table to match the new
specification.
Also, the IF NOT EXISTS of CREATE SEQUENCE:
|IF NOT EXISTS|
Do not throw an error if a relation with the same name already
exists. A notice is issued in this case. Note that there is no
guarantee that the existing relation is anything like the
sequence that would have been created - it might not even be a
sequence.
This command doesn't even guarantee the result is a sequence!
My (admittedly, unimportant) opinion is that this is a bug. For CREATE
TABLE and CREATE SEQUENCE, there are two possible states that the
database can be in. Either with the old table/sequence definition, or
with the new definition.
With the current behaviour of ADD COLUMN IF NOT EXISTS with
CONSTRAINT(s), you cannot know what the resulting state is, other than
that there will be one more constraint added to the column.
Furthermore, I submit the following interaction:
jstrachan=# create table test (foo integer);
CREATE TABLE
jstrachan=# alter table test add column if not exists foo boolean;
ALTER TABLE
jstrachan=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
foo | integer |
If the ADD COLUMN command should automatically fix the constraints of
the possibly-added column, then it should also automatically change the
datatype!
Thanks very much for all your work, and PostgreSQL is awesome.
Jamie Strachan
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: jstrachan@counterpath.com, 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: <6ac7e958-1c93-934a-3656-67340134823b@counterpath.com>
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> <6ac7e958-1c93-934a-3656-67340134823b@counterpath.com>
* 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