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 1oyYJT-0007xl-TS for pgsql-admin@arkaria.postgresql.org; Fri, 25 Nov 2022 13:00:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oyYJS-0004cA-F3 for pgsql-admin@arkaria.postgresql.org; Fri, 25 Nov 2022 13:00:14 +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 1oyYJS-0004c1-4I for pgsql-admin@lists.postgresql.org; Fri, 25 Nov 2022 13:00:14 +0000 Received: from mail.ibu.de ([136.243.18.157] helo=ibu.de) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oyYJM-0000Xf-W8 for pgsql-admin@lists.postgresql.org; Fri, 25 Nov 2022 13:00:13 +0000 Received: from mail.ibu.de (localhost [127.0.0.1]) by ibu.de (8.16.1/8.16.1) with ESMTPS id 2APD06SE098398 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Fri, 25 Nov 2022 14:00:06 +0100 (CET) (envelope-from np@ibu.de) Received: (from np@localhost) by mail.ibu.de (8.16.1/8.16.1/Submit) id 2APD06WZ098397; Fri, 25 Nov 2022 14:00:06 +0100 (CET) (envelope-from np@ibu.de) X-Authentication-Warning: mail.your-server.de: np set sender to np@ibu.de using -f Date: Fri, 25 Nov 2022 14:00:06 +0100 From: Norbert Poellmann To: Laurenz Albe Cc: Nikhil Ingale , pgsql-admin@lists.postgresql.org Subject: Re: Disable unique constraint in Postgres Message-ID: References: <41b12ce3422cf41b050bcb359e45cc453d753c8e.camel@cybertec.at> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <41b12ce3422cf41b050bcb359e45cc453d753c8e.camel@cybertec.at> "From: np@ibu.de" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 >