Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3DCEC475D0F for ; Mon, 13 Jan 2003 05:39:34 -0500 (EST) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by postgresql.org (Postfix) with ESMTP id 65DC2475C9E for ; Mon, 13 Jan 2003 05:39:33 -0500 (EST) Received: from klaster.net (pe210.krakow.cvx.ppp.tpnet.pl [213.76.40.210]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id 4DA262B21E; Mon, 13 Jan 2003 11:36:51 +0100 (CET) Message-ID: <3E229858.4070509@klaster.net> Date: Mon, 13 Jan 2003 11:43:36 +0100 From: Tomasz Myrta User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.2b) Gecko/20021016 X-Accept-Language: en-us, en MIME-Version: 1.0 To: =?ISO-8859-1?Q?Jimmy_M=E4kel=E4?= Cc: "'pgsql-sql@postgresql.org'" Subject: Re: Unique indexes not unique? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/164 X-Sequence-Number: 11029 Jimmy Mäkelä wrote: > I found that Postgres isn't behaving like I thought when using a > unique index in > combination with NULL-values... > Is this a bug or specified in the SQL-standard? If its a bug, is it > fixed in a > recent version? We are using 7.2.3 > > This is the results I got: > > intranet=# create table foo (a varchar(10), b varchar(10)); > CREATE > intranet=# create unique index foo_idx on foo using btree(a, b); > CREATE > intranet=# insert into "foo" (a, b) values ('apa', 'banan'); > INSERT 26229704 1 > intranet=# insert into "foo" (a, b) values ('apa', 'banan'); > ERROR: Cannot insert a duplicate key into unique index foo_idx > intranet=# insert into "foo" (a, b) values ('apa', null); > INSERT 26229706 1 > intranet=# insert into "foo" (a, b) values ('apa', null); > INSERT 26229707 1 I'm not sure unique index works properly for null values. I can't explain, why. Maybe it comes from SQL standard - null i a special value and can't be compared using default operators to other non null values: 1>null =null 1 > > And another completely unrelated question... I have got a table with a > composite > index on A andBb and an index on A > which I query with something like this: > > SELECT * FROM "table" > WHERE (a = 1 OR a = 2 OR a = 3) AND b > 1232132 AND b < 123123123213123 > > Postgres then chooses to use the index for A three times, which is > really slow > on my table... > Then I rewrote the query like: > > SELECT * FROM "table" > WHERE a = 1 AND b > 1232132 AND b < 123123123213123 > UNION SELECT * FROM "table" > WHERE a = 2 AND b > 1232132 AND b < 123123123213123 > UNION SELECT * FROM "table" > WHERE a = 3 AND b > 1232132 AND b < 123123123213123 Try to rewrite your query to show postgres how to use index on AB: SELECT * FROM "table" WHERE (a = 1 AND b > 1232132 AND b < 123123123213123) or (a = 2 AND b > 1232132 AND b < 123123123213123) or (a = 3 AND b > 1232132 AND b < 123123123213123); Regards, Tomasz Myrta