Re: best way to fetch next/prev record based on index - Mailing list pgsql-performance

From Tom Lane
Subject Re: best way to fetch next/prev record based on index
Date
Msg-id 29727.1090944038@sss.pgh.pa.us
Whole thread Raw
In response to best way to fetch next/prev record based on index  ("Merlin Moncure" <merlin.moncure@rcsonline.com>)
List pgsql-performance
I said:
> Oh, wait, you're right --- I'm mis-visualizing the situation.
> Hmm, it sure seems like there ought to be an easy way to do this...

The problem is that a multi-column index doesn't actually have the
semantics you want.  If you are willing to consider adding another
index (or replacing the existing 3-column guy), how about

create index ti on t((array[a,b,c]));

select * from t where array[a,b,c] >= array[a1,b1,c1]
order by array[a,b,c]
limit 1 offset 1;

This seems to do the right thing in 7.4 and later.  It does require that
all three columns have the same datatype though; you weren't specific
about the data model ...

            regards, tom lane

pgsql-performance by date:

Previous
From: "Merlin Moncure"
Date:
Subject: best way to fetch next/prev record based on index
Next
From: Markus Schaber
Date:
Subject: Re: best way to fetch next/prev record based on index