Re: Constraint on multicolumn index

From: "Stuart Brooks" <stuartb(at)cat(dot)co(dot)za>
To: <pgsql-sql(at)postgresql(dot)org>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Constraint on multicolumn index
Date: 2006-11-14 12:50:49
Message-ID: 025a01c707eb$83a1b370$cc22a8c0@STU95
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

> > But if I want the next item following t=(a=10,b=100,c=1000):
>
> > select * from T
> > where (a=10 AND b=100 AND c>1000) OR (a=10 AND b>100) OR (a>10)
> > order by a,b,c;
>
> The correct way to handle this is to use a SQL-spec row comparison:
>
> where (a,b,c) > (10,100,1000)
>
> Unfortunately, that syntax does not work per-spec in any existing
> Postgres release. It will work properly (and use the index) in
> PG 8.2, for what that's worth.
>
> Not sure if there's any reasonable workaround in PG <= 8.1.
> You might want to check this old thread:
> http://archives.postgresql.org/pgsql-performance/2004-07/msg00188.php
> (note that none of the first few responses got the point :-() Also
> http://archives.postgresql.org/pgsql-hackers/2006-02/msg00209.php

Thanks for the response. PG 8.2 looks like a good option when it is
finalized (I see it is beta 3 so shouldn't be too long should it?), but
I also need to have a closer look at the row constructor - this is a new
one for me as I have been using MySQL up til now.

The best solution I could come up with for my problem was to do a
union - something like:

(SELECT * from T WHERE (a=10 AND b=100 AND c>1000) ORDER BY a,b,c LIMIT
10)
UNION
(SELECT * from T WHERE (a=10 AND b>100) ORDER BY a,b,c LIMIT 10)
UNION
(SELECT * from T WHERE (a>10) ORDER BY a,b,c LIMIT 10)
ORDER BY a,b,c LIMIT 10;

which would use an index for each of the selects and then have to merge,
sort and limit the results. This seemed to work although it gets clumsy
if there are a whole lot of extra criteria.

Thanks again for the help,
Stuart

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message T E Schmitz 2006-11-14 15:02:58 Re: max (timestamp,timestamp)
Previous Message Rodrigo Sakai 2006-11-13 19:13:48 RES: Another question about composite types