Re: Optimization with dates - Mailing list pgsql-sql

From Tom Lane
Subject Re: Optimization with dates
Date
Msg-id 4596.1005705832@sss.pgh.pa.us
Whole thread Raw
In response to Optimization with dates  (Jean-Christophe Boggio <cat@thefreecat.org>)
Responses Re: Optimization with dates  (Jean-Christophe Boggio <cat@thefreecat.org>)
ago()  (Haller Christoph <ch@rodos.fzk.de>)
List pgsql-sql
Jean-Christophe Boggio <cat@thefreecat.org> writes:
> I have tried :
>   where dategain>=(select now()-30);
>   where dategain+30>='now'
>   where date(dategain)>=date('now')-30
>   and many other, syntactically absurd :-)

Trydategain >= ago(30)

where "ago" is a function that computes "date(now()) - n" and is
marked "iscachable".  This is a cheat but it keeps the planner from
being distracted by the noncachable nature of "now()".  You can find
past discussions of this if you search the archives for "iscachable",
eg
http://fts.postgresql.org/db/mw/msg.html?mid=1037521
http://fts.postgresql.org/db/mw/msg.html?mid=60584
http://fts.postgresql.org/db/mw/msg.html?mid=97823

As several other people pointed out, there's also a question of whether
the system *should* use the index --- you haven't told us how many
rows you expect the query to visit.  But until you express the WHERE
condition in the form "column >= something-that-can-be-reduced-to-
a-constant", you're unlikely to get the system to even try.
        regards, tom lane


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Optimization with dates
Next
From: "Tille, Andreas"
Date:
Subject: Re: Design Tool for postgresql