BUG #5005: unique constraint considers nulls to be equal

From: "Dean Schulze" <dean(dot)w(dot)schulze(at)comcast(dot)net>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #5005: unique constraint considers nulls to be equal
Date: 2009-08-23 22:59:49
Message-ID: 200908232259.n7NMxnt4069286@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 5005
Logged by: Dean Schulze
Email address: dean(dot)w(dot)schulze(at)comcast(dot)net
PostgreSQL version: 8.3.7
Operating system: Win XP
Description: unique constraint considers nulls to be equal
Details:

When I apply this constraint it fails because there are records that will
violate the new constraint:

ALTER TABLE table1 ADD CONSTRAINT unique_county_year_idnumber UNIQUE
(county, year, idnumber);

Some of the records it fails on have null for idnumber. According to the
documentation section 5.3.3 nulls should not violate a unique constraint:

However, two null values are not considered equal in this comparison. That
means even in the presence of a unique constraint it is possible to store
duplicate rows that contain a null value in at least one of the constrained
columns.

Here are the duplicate records that violate this constraint:

select county, year, idnumber, count(0) from table1 group by county, year,
idnumber having (count(0) > 1) order by county, count(0);

"county","year","idnumber","count"
"AD",2009,"A57504",2
"AD",2009,"A58395",2
"AD",2009,"A58286",2
"AD",2009,"A58299",2
"AD",2009,"",5
"AR",2009,"2148.0",2
"AR",2009,"20.0",2
"BD",2009,"22497.0",2
"BD",2009,"22499.0",2
"BD",2009,"",5
"DS",2009,"1426",2
"DS",2009,"1443",2
"DV",2009,"3237",2
"DV",2009,"1775.0",2
"DV",2009,"3202",2
"DV",2009,"",3
"EL",2009,"",3
"JF",2009,"J01718",2
"LR",2009,"606.0",2
"LR",2009,"",4

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Alvaro Herrera 2009-08-24 00:57:01 Re: BUG #5005: unique constraint considers nulls to be equal
Previous Message Craig Ringer 2009-08-23 11:02:48 Re: BUG #4996: postgres.exe memory consumption keeps going up