agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Marc Mamin <M.Mamin@intershop.de>
To: Voillequin, Jean-Marc <Jean-Marc.Voillequin@moodys.com>
To: pgsql-sql@lists.postgresql.org <pgsql-sql@lists.postgresql.org>
Subject: AW: unique index with several columns
Date: Fri, 4 Mar 2022 15:32:38 +0000
Message-ID: <d931301d59d14c25a1a41e416ac4adf1@intershop.de> (raw)
In-Reply-To: <MN2PR20MB2735677B2B4E5D3C4EA71B5EBE059@MN2PR20MB2735.namprd20.prod.outlook.com>
References: <MN2PR20MB2735507A85A89B7279AB6A2CBE059@MN2PR20MB2735.namprd20.prod.outlook.com>
<MN2PR20MB2735677B2B4E5D3C4EA71B5EBE059@MN2PR20MB2735.namprd20.prod.outlook.com>
you can use partial indexes:
create unique index idx on t(coalesce(c1,''),coalesce(c2,'') ) where (coalesce(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 planer:
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 <Jean-Marc.Voillequin@moodys.com>
Gesendet: Freitag, 4. März 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=> create table t(c1 char, c2 char);
CREATE TABLE
RRT=> create unique index idx on t(c1,c2);
CREATE INDEX
RRT=> insert into t(c1,c2) values (null,null);
INSERT 0 1
RRT=> insert into t(c1,c2) values (null,null);
INSERT 0 1
RRT=> insert into t(c1,c2) values ('a',null);
INSERT 0 1
RRT=> 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=> create table t(c1 char, c2 char);
ERROR: relation "t" already exists
RRT=> create unique index idx on t(c1,c2);
ERROR: relation "idx" already exists
RRT=> insert into t(c1,c2) values (null,null);
INSERT 0 1
RRT=> insert into t(c1,c2) values (null,null);
INSERT 0 1
RRT=> insert into t(c1,c2) values ('a',null);
INSERT 0 1
RRT=> 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 duplicate key error.
Thanks & regards
-----------------------------------------
Moody's monitors email communications through its networks for regulatory compliance purposes and to protect its customers, employees and business and where allowed to do so by applicable law. The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately 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 viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.
-----------------------------------------
Message-ID: <d931301d59d14c25a1a41e416ac4adf1@intershop.de>
Permalink: ../d931301d59d14c25a1a41e416ac4adf1@intershop.de/
Also on: postgresql.org/message-id/d931301d59d14c25a1a41e416ac4adf1@intershop.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-sql@postgresql.org
Cc: M.Mamin@intershop.de, Jean-Marc.Voillequin@moodys.com, pgsql-sql@lists.postgresql.org
Subject: Re: AW: unique index with several columns
In-Reply-To: <d931301d59d14c25a1a41e416ac4adf1@intershop.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox