Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id EBF691337DFB for ; Thu, 16 Sep 2010 12:14:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 60959-10 for ; Thu, 16 Sep 2010 15:14:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id AA4661337C01 for ; Thu, 16 Sep 2010 12:14:36 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o8GFEZeq013511; Thu, 16 Sep 2010 11:14:35 -0400 (EDT) To: Alanoly Andrews cc: "'pgsql-admin@postgresql.org'" Subject: Re: Bidirectional index traversal In-reply-to: <09B23E7BF70425478C1330D893A722C602FEC01A2D@MailSVR.invera.com> References: <09B23E7BF70425478C1330D893A722C602FEC01A2C@MailSVR.invera.com> <12592.1284648990@sss.pgh.pa.us> <09B23E7BF70425478C1330D893A722C602FEC01A2D@MailSVR.invera.com> Comments: In-reply-to Alanoly Andrews message dated "Thu, 16 Sep 2010 11:02:07 -0400" Date: Thu, 16 Sep 2010 11:14:35 -0400 Message-ID: <13510.1284650075@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201009/89 X-Sequence-Number: 35633 Alanoly Andrews writes: > To expand on that question: > Suppose I have a table with the following schema: > tab1(col1 decimal(3,0), col2 char(3)). > There is an index defined on it as : create index tab1ind1 on tab1(col1) > Now, if I have a query as: "select * from tab1 order by col1", I > expect the Optimizer to use the index tab1ind1. But if I have a query > like: "select * from tab1 order by col1 desc", does the Postgres > Optimizer use the same index as above (but in the reverse direction) Yes, it will, as you could easily find by reading the manual: http://www.postgresql.org/docs/8.4/static/indexes-ordering.html or by experimentation: regression=# create table tab1(col1 decimal(3,0), col2 char(3)); CREATE TABLE regression=# create index tab1ind1 on tab1(col1); CREATE INDEX regression=# explain select * from tab1 order by col1; QUERY PLAN -------------------------------------------------------------------------- Index Scan using tab1ind1 on tab1 (cost=0.00..70.20 rows=1730 width=17) (1 row) regression=# explain select * from tab1 order by col1 desc; QUERY PLAN ----------------------------------------------------------------------------------- Index Scan Backward using tab1ind1 on tab1 (cost=0.00..70.20 rows=1730 width=17) (1 row) (Now, whether the optimizer will prefer an index over seqscan-and-sort depends on a lot of factors. But backwards scan isn't a problem.) regards, tom lane