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 1fwVOF-0006oH-Qf for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Sep 2018 16:38:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fwVOD-0005iN-MQ for pgsql-bugs@arkaria.postgresql.org; Sun, 02 Sep 2018 16:38:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fwVOD-0005iG-GE for pgsql-bugs@lists.postgresql.org; Sun, 02 Sep 2018 16:38:17 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fwVO5-0001qu-8R for pgsql-bugs@lists.postgresql.org; Sun, 02 Sep 2018 16:38:17 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w82Gc3VU008931; Sun, 2 Sep 2018 12:38:03 -0400 From: Tom Lane To: Stephen Frost cc: postgresql@noetika.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15361: Add column if not exists create duplicate constraint 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> Comments: In-reply-to Stephen Frost message dated "Sun, 02 Sep 2018 07:45:15 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8929.1535906283.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 02 Sep 2018 12:38:03 -0400 Message-ID: <8930.1535906283@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Stephen Frost 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.161181944= 52213577808%40wrigleys.postgresql.org >> wherein it was argued that if the IF NOT EXISTS fires, it should preven= t >> 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 anyth= ing >> 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