pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andy Shellam <andy@andycc.net>
To: Chris Hoover <revoohc@gmail.com>
To: pgsql-admin@postgresql.org
Subject: Re: 8.1 Unique Index Issue/Bug???
Date: Thu, 13 Jul 2006 16:50:55 +0100
Message-ID: <44B66BDF.9000608@andycc.net> (raw)
In-Reply-To: <1d219a6f0607130740g7fe13967t48c77a64fde977aa@mail.gmail.com>
References: <1d219a6f0607130740g7fe13967t48c77a64fde977aa@mail.gmail.com>

PostgreSQL does not consider a NULL value to be an equal value, 
therefore it cannot be a duplicate. 

If you must have a blank value in a column and need it to be unique, 
you'd need to do something like an empty string, or a string/figure your 
application will know is a null value that makes sense.

It's also the same on multi-column indexes - if one column is NULL-able, 
the index won't be enforced against the null values.

 From http://www.postgresql.org/docs/8.1/static/indexes-unique.html:

"When an index is declared unique, multiple table rows with equal 
indexed values will not be allowed. Null values are not considered 
equal. A multicolumn unique index will only reject cases where all of 
the indexed columns are equal in two rows."

Andy.

Chris Hoover wrote:
> I am having what appears to be a bug with unique indexes on 8.1.3.
>
> I have created a new table.
>
> create table payer_835 (
>     payer_id         int8 not null default 
> nextval('payer_835_payer_id_seq'::regclass) primary key,
>     payer_name        varchar(50) not null,
>     payer_trn03        varchar(10) not null,
>     payer_trn04        varchar(30),
>     sku_id            int8 references skucode(sku_id) on delete 
> cascade on update cascade,
>     payer_billable        boolean not null default true,
>     create_timestamp    timestamp not null default now(),
>     last_mod_timestamp    timestamp,
>     expire_timestamp    timestamp
> );
>
> On this table, I have created a unique index on payer_trn03, 
> payer_trn04, and expire_timestamp.  However, since the 
> expire_timestamp is normally null, the unique index does not appear to 
> be working.  I have been able to enter two identical rows into this 
> table.
>
> Why is PostgreSQL not enforcing this index? This appears to be a 
> pretty major a bug?  It would seem that you could have a unique index 
> across columns that might have a null in them.
>
> Here is the data from the table:
>
> COPY payer_835 (payer_id, payer_name, payer_trn03, payer_trn04, 
> sku_id, payer_billable, create_timestamp, last_mod_timestamp, 
> expire_timestamp) FROM stdin;
> 1       CAHABA GBA-AL PART B    1630103830      \N      1       
> t       2006-07-13 09:57: 52.834631      \N      \N
> 2       FEP     123456789       00402   1       t       2006-07-10 
> 10:56:23     \N      \N
> 3       NC Medicaid     123123123       \N      1       t       
> 2006-07-10 10:56:41     \N      \N
> 4       CAHABA GBA-AL PART B    1630103830      \N      1       
> t       2006-07-11 16:13:43.808181      2006-07-12 10:09:46.066204      \N
> \.
>
>
> Notice records 1 and 4 have identical data (as far as my unique index 
> is concerned), and the index it not complaining.
>
> thanks,
>
> Chris
> RHAS 4.0
> PG 8.1.3 (from rpms)
> !DSPAM:14,44b66a2a34531616211146! 




view thread (10+ messages)  latest in thread

Message-ID: <44B66BDF.9000608@andycc.net>
Permalink:  ../44B66BDF.9000608@andycc.net/
Also on:    postgresql.org/message-id/44B66BDF.9000608@andycc.net

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: andy@andycc.net, revoohc@gmail.com
  Subject: Re: 8.1 Unique Index Issue/Bug???
  In-Reply-To: <44B66BDF.9000608@andycc.net>
  References: <1d219a6f0607130740g7fe13967t48c77a64fde977aa@mail.gmail.com> <44B66BDF.9000608@andycc.net>

* 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