Re: Unique Index

From: Dawid Kuroczko <qnex42(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: Alex <alex(at)meerkatsoft(dot)com>
Subject: Re: Unique Index
Date: 2005-01-20 08:27:19
Message-ID: 758d5e7f0501200027ee5d7d8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 20 Jan 2005 15:20:26 +1100, Alex <alex(at)meerkatsoft(dot)com> wrote:
> I actually just wanted to know if there is a way around this problem.
> Obviously it is implemented that way for whatever reason.

Well, if you really need it, partial indexes are your friends! :)

For clarity, let's say you have:
CREATE TABLE foo (
a int,
b int,
c int,
);
And an INDEX:
CREATE UNIQUE INDEX foo_abc_index ON foo (a,b,c);

Now, you want to make sure a and b are UNIQUE, when c is null; just do:
CREATE UNIQUE INDEX foo_abN_index ON foo (a,b) WHERE c IS NULL;

Or even, to make b UNIQUE when a and c are null:
CREATE UNIQUE INDEX foo_NbN_index ON foo (b) WHERE a IS NULL AND c IS NULL;

You need to create such partial indexes for each set of columns
you want to be unique-with-null.

Don't worry about "index bloat". These additional indexes will be used
only when your main (foo_abc_index) is not used, so there won't be
any duplicate data in them.

Isn't PostgreSQL great? :)

Regards,
Dawid

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lakshmi Narayanan 2005-01-20 08:47:55 Help in Replication
Previous Message Niederland 2005-01-20 08:19:36 Re: pg_restore