Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lZDWR-0003gM-UB for pgsql-novice@arkaria.postgresql.org; Wed, 21 Apr 2021 14:08:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lZDWQ-00064k-Ro for pgsql-novice@arkaria.postgresql.org; Wed, 21 Apr 2021 14:08:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lZDWQ-00064d-Lt for pgsql-novice@lists.postgresql.org; Wed, 21 Apr 2021 14:08:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lZDWO-0006mo-M8 for pgsql-novice@postgresql.org; Wed, 21 Apr 2021 14:08:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13LE80Mw757479; Wed, 21 Apr 2021 10:08:00 -0400 From: Tom Lane To: Stephen Carboni cc: pgsql-novice@postgresql.org Subject: Re: Why is there no ADD CONSTRAINT IF NOT EXISTS ? In-reply-to: References: Comments: In-reply-to Stephen Carboni message dated "Wed, 21 Apr 2021 13:13:19 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <757477.1619014080.1@sss.pgh.pa.us> Date: Wed, 21 Apr 2021 10:08:00 -0400 Message-ID: <757478.1619014080@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Stephen Carboni writes: > I can drop a constraint if it exists, but cannot simply add one if it does > not exist. Is there something especially difficult about supporting this? Probably some combination of (1) nobody got around to it yet and (2) it's not entirely clear what the semantics ought to be. Should we match on name, or contents of the constraint, and how picky would a content match be? There's a general problem with CREATE IF NOT EXISTS that you don't really have any guarantee afterwards as to what are the details of the object's definition. Personally I prefer CREATE OR REPLACE semantics, where you do know what the object looks like afterwards. However, C.O.R. doesn't work that well unless there's a very clear unique identity (e.g. name) for the object. Since constraints are often made without specifying a name, it's hard to be sure what you're supposed to replace. I'm not sure whether C.I.N.E. would have as bad of a problem with that, but it seems possible. regards, tom lane