Re: Backwards index scan

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Dmitry Tkach <dmitry(at)openratings(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Backwards index scan
Date: 2003-07-08 15:12:02
Message-ID: 20030708080258.H95890-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general pgsql-hackers

On Tue, 8 Jul 2003, Dmitry Tkach wrote:

> Stephan Szabo wrote:
>
> >If you make an opclass that orders in the reverse order you can use that
> >opclass in creating the index (which effectively can give you an index
> >like x, y desc by using the new opclass on y). There was some talk
> >recently about whether we should provide such opclasses as builtins or
> >contrib items.
> >
> >
> Actually, I just thought, it is not exactly equivalent, unless I am
> missing something.
> If I create this opclass and the index, and then make a query like
> select * from huge_table where x=10 order by x,y desc
>
> ... it won't know to use the index for sorting, will it?

I don't know the mechanics (haven't looked) but it seems to know
based on the way the operators are assigned to the opclass. I've
done some minimal tests and for queries like
select * from tab order by a, b desc
and then gotten effectively
Index scan using <index> on <table>
as the plan with no sort steps.

> In this situation, it will work... But it may be a problem when the
> query is (a lot) more complicated, with several joins and a bunch of
> different paths available to the planner - how can I guarantee then that
> it will always choose this index and return the results in the right order?

You can't guarantee that it'll always choose this index because it's
possible that the index is more expensive for a particular query, but
it should consider the index.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Segree, Gareth 2003-07-08 20:32:11 Error Compiling Postgres
Previous Message Dmitry Tkach 2003-07-08 14:15:57 Re: Backwards index scan

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-07-08 15:33:34 Re: SQL Functions and plan time
Previous Message Dmitry Tkach 2003-07-08 14:15:57 Re: Backwards index scan

Browse pgsql-hackers by date

  From Date Subject
Next Message Maksim Likharev 2003-07-08 16:34:33 Re: PG crash on simple query, story continues
Previous Message Jenny - 2003-07-08 14:41:48 running transactions in postgresql