Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g36sY-0001yl-0B for pgsql-bugs@arkaria.postgresql.org; Thu, 20 Sep 2018 21:52:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g36sW-0002Ia-9A for pgsql-bugs@arkaria.postgresql.org; Thu, 20 Sep 2018 21:52:52 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g36Tx-0006nj-AR for pgsql-bugs@lists.postgresql.org; Thu, 20 Sep 2018 21:27:29 +0000 Received: from exchange.counterpath.com ([162.255.250.65]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g36Tp-0003SW-HM for pgsql-bugs@lists.postgresql.org; Thu, 20 Sep 2018 21:27:26 +0000 Received: from CounterpathCAS.cp.local (192.168.60.9) by CounterPathMB1.cp.local (192.168.60.10) with Microsoft SMTP Server (TLS) id 15.0.1365.1; Thu, 20 Sep 2018 14:27:19 -0700 Received: from jamies.cp.local (209.90.156.69) by CounterpathCAS.cp.local (192.168.60.9) with Microsoft SMTP Server (TLS) id 15.0.1365.1 via Frontend Transport; Thu, 20 Sep 2018 14:27:18 -0700 Subject: Re: Re: BUG #15361: Add column if not exists create duplicate constraint To: Tom Lane , Stephen Frost CC: , 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> From: Jamie Strachan Message-ID: <6ac7e958-1c93-934a-3656-67340134823b@counterpath.com> Date: Thu, 20 Sep 2018 17:27:13 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: <8930.1535906283@sss.pgh.pa.us> Content-Type: multipart/alternative; boundary="------------B6DB82C979C76E87DBE0DF34" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --------------B6DB82C979C76E87DBE0DF34 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit 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 --------------B6DB82C979C76E87DBE0DF34 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 8bit 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


--------------B6DB82C979C76E87DBE0DF34--