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 1nQ9vD-000317-Hp for pgsql-sql@arkaria.postgresql.org; Fri, 04 Mar 2022 15:32:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nQ9vC-0005Vu-Ea for pgsql-sql@arkaria.postgresql.org; Fri, 04 Mar 2022 15:32:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nQ9vC-0005Vf-3S for pgsql-sql@lists.postgresql.org; Fri, 04 Mar 2022 15:32:46 +0000 Received: from mx3.intershop.de ([195.110.60.3]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nQ9v9-0007Vh-2j for pgsql-sql@lists.postgresql.org; Fri, 04 Mar 2022 15:32:45 +0000 Received: from mymail.intershop.de (jenex19-2.ad.intershop.net [10.0.87.122]) by mx3.intershop.de (Postfix) with ESMTPS id DE2931002FC; Fri, 4 Mar 2022 16:32:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intershop.de; s=2017020601; t=1646407958; bh=RsOqtLzYduaNX3HBD40ACueMnFGFoJJvEZ4cEaoTqiI=; h=From:To:Subject:Date:References:In-Reply-To:From; b=O3fOvIzWpoRVZPknY7Bff6wOBLTa6X+pXL9HIxVirp1bEsWaLm2qO4xsujabUexeW u6Lj/nl1odgHvs3tr4XFH28HKzd4BqjT7Gp2jQ23EytF75GBy+aTldiCXMwk1eKsOo xnzm855JdX77sOb8zZiBxSHKfugPrxdEm+hY7RwnLVn+5nAgTDegzlWTaHh3OSAN9S NgqM2pU8t3ydWQ2m8Q7iPKOy+80MEtQQ/xlKErqXDc0+Tf1HteML5+qnKszCgbi/+P GSGnGdDi2ij2Y+fpyzX6jrSkGIbEa4YemCgS229pI8Q9TzYEzyZGKsmR7sp8fSWu13 z1pWHT/v/PTtaIyGVxwzyLULD38vHVcb15+txaeIh0croM5r6SGlCy+Wpmu4CZJMEh /AjZ94el2l7T5RclkQ4e20sSeg3TucAA7QGWzDQmXPlNZXkn9AbNn4FmGXsg1C/lQo mH5cizLBNBbFgjf72G7NlPxXoPC3EOjZXN+zn/ve0uJpB57cX9/P6BiuuuFvBMIjgv 8B1PdVS7MYGNFBJOBID5Hbf9wnkPPiRCD5/A0e46/qHY6QoruKefywml0M/3t6s4Il oOqy700C2yfezlwUUqT91QR4nbYsaa02jshrP0cs/t4/fBXbIsLGs8rxwAaEziCV54 oJDsxEI9CkZSCfHsJgmupPgY= Received: from JENEX19-1.ad.intershop.net (10.0.87.121) by JENEX19-2.ad.intershop.net (10.0.87.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.15; Fri, 4 Mar 2022 16:32:38 +0100 Received: from JENEX19-1.ad.intershop.net ([fe80::fca3:adda:19c9:b9ff]) by JENEX19-1.ad.intershop.net ([fe80::fca3:adda:19c9:b9ff%13]) with mapi id 15.02.0986.015; Fri, 4 Mar 2022 16:32:38 +0100 From: Marc Mamin To: "Voillequin, Jean-Marc" , "pgsql-sql@lists.postgresql.org" Subject: AW: unique index with several columns Thread-Topic: unique index with several columns Thread-Index: Adgv15Nf9+BSttqaTPmnX2Q0fyVmZAAAg5tgAADRWPE= Date: Fri, 4 Mar 2022 15:32:38 +0000 Message-ID: References: , In-Reply-To: Accept-Language: de-DE, en-US Content-Language: de-DE X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [195.110.60.24] x-tm-snts-smtp: 7AC4E6979E6385B028CCF2D1E964BBFECDFA66A955BCD16E3A98C0556C8CF3E22000:8 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.103.5 at mx3.intershop.de X-Virus-Status: Clean List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk you can use partial indexes: create unique index idx on t(coalesce(c1,''),coalesce(c2,'') ) where (co= alesce(c1, c2) is not null); but '' is considered equals to null here ... or more partial indexes which are more likely to be considered by the plan= er: create unique index idx_1 on t(c1) where (c2 is null ); create unique index idx_2 on t(c2) where (c1 is null); create unique index idx_3 on t(c1, c2) where (c1 is not null and c2 is not= null); hth, Marc ________________________________________ Von: Voillequin, Jean-Marc Gesendet: Freitag, 4. M=E4rz 2022 16:09:43 An: pgsql-sql@lists.postgresql.org Betreff: RE: unique index with several columns Sorry, the test case is: On PG: RRT=3D> create table t(c1 char, c2 char); CREATE TABLE RRT=3D> create unique index idx on t(c1,c2); CREATE INDEX RRT=3D> insert into t(c1,c2) values (null,null); INSERT 0 1 RRT=3D> insert into t(c1,c2) values (null,null); INSERT 0 1 RRT=3D> insert into t(c1,c2) values ('a',null); INSERT 0 1 RRT=3D> insert into t(c1,c2) values ('a',null); INSERT 0 1 On Oracle: SQL> create table t(c1 char, c2 char); Table created. SQL> create unique index idx on t(c1,c2); Index created. SQL> insert into t(c1,c2) values (null,null); 1 row created. SQL> insert into t(c1,c2) values (null,null); 1 row created. SQL> insert into t(c1,c2) values ('a',null); 1 row created. SQL> insert into t(c1,c2) values ('a',null); insert into t(c1,c2) values ('a',null) * ERROR at line 1: ORA-00001: unique constraint (RRT.IDX) violated The question is the same. From: Voillequin, Jean-Marc Sent: Friday, March 4, 2022 4:07 PM To: pgsql-sql@lists.postgresql.org Subject: unique index with several columns Hello, On PG: RRT=3D> create table t(c1 char, c2 char); ERROR: relation "t" already exists RRT=3D> create unique index idx on t(c1,c2); ERROR: relation "idx" already exists RRT=3D> insert into t(c1,c2) values (null,null); INSERT 0 1 RRT=3D> insert into t(c1,c2) values (null,null); INSERT 0 1 RRT=3D> insert into t(c1,c2) values ('a',null); INSERT 0 1 RRT=3D> insert into t(c1,c2) values ('a',null); INSERT 0 1 On Oracle: SQL> create table t(c1 char, c2 char); Table created. SQL> create unique index idx on t(c1,c2); Index created. SQL> insert into t(c1,c2) values (null,null); 1 row created. SQL> insert into t(c1,c2) values (null,null); 1 row created. SQL> insert into t(c1,c2) values ('a',null); 1 row created. SQL> insert into t(c1,c2) values ('a',null); insert into t(c1,c2) values ('a',null) * ERROR at line 1: ORA-00001: unique constraint (RRT.IDX) violated When one of the field is null, PG considers that the tuple is not the same: ('a',null) is not equal to ('a',null) So, the unique constraint is not violated in PG. But is there a way to have the same feature than Oracle? I already tried with: create unique index idx on t(coalesce(c1,''),coalesce(c2,'')) But in this case, I cannot insert several (null,null) without raising a dup= licate key error. Thanks & regards ----------------------------------------- Moody's monitors email communications through its networks for regulatory c= ompliance purposes and to protect its customers, employees and business and= where allowed to do so by applicable law. The information contained in thi= s e-mail message, and any attachment thereto, is confidential and may not b= e disclosed without our express permission. If you are not the intended rec= ipient or an employee or agent responsible for delivering this message to t= he intended recipient, you are hereby notified that you have received this = message in error and that any review, dissemination, distribution or copyin= g of this message, or any attachment thereto, in whole or in part, is stric= tly prohibited. If you have received this message in error, please immediat= ely notify us by telephone, fax or e-mail and delete the message and all of= its attachments. Every effort is made to keep our network free from viruse= s. You should, however, review this e-mail message, as well as any attachme= nt thereto, for viruses. We take no responsibility and have no liability fo= r any computer virus which may be transferred via this e-mail message. -----------------------------------------