Re: Why is there no ADD CONSTRAINT IF NOT EXISTS ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stephen Carboni <stephen(dot)entropy(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Why is there no ADD CONSTRAINT IF NOT EXISTS ?
Date: 2021-04-21 14:08:00
Message-ID: 757478.1619014080@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Stephen Carboni <stephen(dot)entropy(at)gmail(dot)com> 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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Pavel Tide 2021-04-23 18:48:39 suppress empty archive_command warning message
Previous Message Stephen Carboni 2021-04-21 12:13:19 Why is there no ADD CONSTRAINT IF NOT EXISTS ?