Re: index not always used when selecting on a date field - Mailing list pgsql-general

From Greg Stark
Subject Re: index not always used when selecting on a date field
Date
Msg-id 87mzxsjgo4.fsf@stark.xeocode.com
Whole thread Raw
In response to Re: index not always used when selecting on a date field  (Russell Smith <mr-russ@pws.com.au>)
List pgsql-general
Russell Smith <mr-russ@pws.com.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

pgsql-general by date:

Previous
From: Marek Lewczuk
Date:
Subject: Re: how to use COPY within plperl
Next
From: "Bob Powell"
Date:
Subject: Backend disconnect problems