pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Norbert Poellmann <np@ibu.de>
To: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Nikhil Ingale <niks.bgm@gmail.com>
Cc: pgsql-admin@lists.postgresql.org
Subject: Re: Disable unique constraint in Postgres
Date: Fri, 25 Nov 2022 14:00:06 +0100
Message-ID: <Y4C8Vph9AS4IbFdp@mail.ibu.de> (raw)
In-Reply-To: <41b12ce3422cf41b050bcb359e45cc453d753c8e.camel@cybertec.at>
References: <CALXkTpyQXcT5m7cYttWjLv3n8xGC+MFYVah-q9Lxz-8FKBoMmg@mail.gmail.com>
	<41b12ce3422cf41b050bcb359e45cc453d753c8e.camel@cybertec.at>

On Fri, Nov 25, 2022 at 08:46:22AM +0100, Laurenz Albe wrote:
> On Fri, 2022-11-25 at 12:48 +0530, Nikhil Ingale wrote:
> > We have an alter command to disable any constraints in the oracle db. Similarly do we have any
> > command to disable the UNIQUE constraint in postgres. Most of the postgres db forum suggests
> > dropping the UNIQUE constraint. But, I don't want to drop the constraint here instead just
> > disable the unique constraint.
> > 
> > Need your inputs on the same.
> 
> The db forums were right: you cannot disable a unique constraint in PostgreSQL.

Hi, 

hope, that I've understood the problem correctly...
At least since v13.x  dropping a unique constraint seems to work while 
dropping the related index:

-- two constraints added:
np=#  create table a (i integer unique constraint hu check(i<100));
CREATE TABLE
np=# \d+ a
                                     Table "public.a"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
--------+---------+-----------+----------+---------+---------+--------------+-------------
 i      | integer |           |          |         | plain   |              | 
Indexes:
    "a_i_key" UNIQUE CONSTRAINT, btree (i)
Check constraints:
    "hu" CHECK (i < 100)
Access method: heap

np=# insert into a values(1);
INSERT 0 1
np=# insert into a values(1);
ERROR:  duplicate key value violates unique constraint "a_i_key"
DETAIL:  Key (i)=(1) already exists.
np=# insert into a values(2);
INSERT 0 1
np=# insert into a values(101);
ERROR:  new row for relation "a" violates check constraint "hu"
DETAIL:  Failing row contains (101).

-- So far, so good. It works as expected.
-- Now:

np=#  alter table a drop constraint a_i_key;
ALTER TABLE
np=# \d+ a
                                     Table "public.a"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
--------+---------+-----------+----------+---------+---------+--------------+-------------
 i      | integer |           |          |         | plain   |              | 
Check constraints:
    "hu" CHECK (i < 100)
Access method: heap

-- only constraint i<100 left:

np=# insert into a values(1);
INSERT 0 1
np=# insert into a values(101);
ERROR:  new row for relation "a" violates check constraint "hu"
DETAIL:  Failing row contains (101).

np=# select * from a;
 i 
---
 1
 2
 1

-- have to check that against different postgresql versions and docs...

cheers

/np

> 
> Yours,
> Laurenz Albe
> 





view thread (9+ messages)  latest in thread

Message-ID: <Y4C8Vph9AS4IbFdp@mail.ibu.de>
Permalink:  ../Y4C8Vph9AS4IbFdp@mail.ibu.de/
Also on:    postgresql.org/message-id/Y4C8Vph9AS4IbFdp@mail.ibu.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: np@ibu.de, laurenz.albe@cybertec.at, niks.bgm@gmail.com, pgsql-admin@lists.postgresql.org
  Subject: Re: Disable unique constraint in Postgres
  In-Reply-To: <Y4C8Vph9AS4IbFdp@mail.ibu.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox