Re: index not always used when selecting on a date field

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: index not always used when selecting on a date field
Date: 2004-11-08 19:15:39
Message-ID: 87mzxsjgo4.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Russell Smith <mr-russ(at)pws(dot)com(dot)au> writes:

> now() and CURRENT_DATE, are and cannot be planned as constants.
> So the planner cannot use an index for them.

It's not that it cannot use an index, but that it doesn't know it should use
an index. The planner knows that it can't count on now() to be constant so it
doesn't use the value it has. As far as it's concerned you're comparing
against an unknown value. And in general the postgres optimizer assumes single
sided inequalities with unknown constants aren't selective enough to justify
an index scan.

The easiest work-around is probably just putting in a bogus second inequality
to make it a range. The planner generally assumes ranges are selective enough
to justify index scans.

--
greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bob Powell 2004-11-08 19:20:37 Backend disconnect problems
Previous Message Marek Lewczuk 2004-11-08 19:14:22 Re: how to use COPY within plperl