Re: question about unique indexes

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Jonathan Vanasco <postgres(at)2xlp(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: question about unique indexes
Date: 2010-05-10 10:29:14
Message-ID: 487ECA43-FCD1-4C76-9CDB-5764FE306274@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10 May 2010, at 2:09, Jonathan Vanasco wrote:

> i was given a unique index on
> (country_id, state_id, city_id, postal_code_id)

> in the two records below, only country_id and state_id are assigned ( aside from the serial )
>
> geographic_location_id | coordinates_latitude | coordinates_longitude | country_id | state_id | city_id | postal_code_id
> ------------------------+----------------------+-----------------------+------------+----------+---------+----------------
> 312 | | | 233 | 65 | |
> 443 | | | 233 | 65 | |
>
> i was under the expectation that the unique constraint would apply in this place.
>
> from the docs:
> When an index is declared unique, multiple table rows with equal indexed values are not allowed. Null values are not considered equal. A multicolumn unique index will only reject cases where all indexed columns are equal in multiple rows.

As the docs state and as others already mentioned, "Null values are not considered equal".

You're about to encounter an interesting problem. You have several optional foreign keys, so they have to be declared nullable, yet your constraints are such that you can't enforce uniqueness because nulls can't be compared.

You could (as mentioned by Al Rumman) create a unique index using coalesce() for each nullable column, but that index wouldn't be usable for normal queries - your query expressions won't match the indexes expressions ('WHERE city_id=7' vs. 'COALESCE(city_id, 0)=7'). I don't think the query planner sees the similarity between those expressions.

It is possible to add another index over those columns, without the coalesces, but it would take another time that amount of disk/memory space and the planner likely wouldn't be aware of the uniqueness of the data in it and hence not plan for that.

You could also change all your queries to use coalesce() for each of those columns. Hiding that in a view (maybe an updatable one so that CRUD operations can use the index too) would be a solution.

An alternative solution is to actually use 0 for those foreign keys and create a special record for that in the related tables. It depends on the table definitions how easy that is to do, you don't want to end up with all kinds of dummy-data in your client application, but you also don't want to remove any constraints that guarantee sensibility of the data in those tables.

None of these solutions are pretty. It should be quite a common problem though, how do people normally solve this?

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.

!DSPAM:737,4be7e01210416358213314!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Voras 2010-05-10 11:40:49 hstore problem with UNION?
Previous Message Alban Hertroys 2010-05-10 10:04:46 Re: Query that produces index information for a Table