Why isn't there a IF NOT EXISTS clause on constraint creation?

From: richard coleman <rcoleman(dot)ascentgl(at)gmail(dot)com>
To: Pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Why isn't there a IF NOT EXISTS clause on constraint creation?
Date: 2023-08-30 20:38:53
Message-ID: CAGA3vBvFJuPjPK6K2L7htCmRKHGm4vm7YPpEsZaynP30Ps+UxQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

I find myself having to check a couple of hundred tables and add the
primary keys back in, if they don't exist.

While there's a IF NOT EXISTS clause on the DROP statement to drop a
constraint, like a primary key, inexplicably there isn't one on the ALTER
TABLE statement that creates them.

The internet is littered with people asking about this, some going back
years. The responses range from manually checking first, using a DROP IF
EXISTS statement first, to writing various blocks of PL/pgSQL code.

Why doesn't ALTER TABLE ... ADD CONSTRAINT not come with an ALTER TABLE
... ADD CONSTRAINT IF NOT EXISTS variant?

Could one be added?

Just wondering,
rik.

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Tom Lane 2023-08-30 21:26:17 Re: Why isn't there a IF NOT EXISTS clause on constraint creation?
Previous Message Nikhil Shetty 2023-08-30 14:14:05 Logical Replication hung: logical_decoding_work_mem over utilised