I have a query of the form:
SELECT * FROM .... WHERE (now()-date1) > 'interval 1 day';
..i.e. all rows 'older' than 1 day. This could be efficiently
processed using the index on date1, but sadly pg doesn't know this ;-(
This transforms an operation which should be O(1) to O(rows)....
More worryingly, when I investigated the above, I find it doesn't even
use the index for
SELECT * FROM ....
WHERE date1 > '2000-09-11 00:00:00'::datetime - '1 hour'::interval;
...so it doesn't realise that constant-constant is constant,
notwithstanding the more complex issues that now() is pseudo-constant.
This could be fixed by constant folding, I guess; any plans for that?
Jules
I checked the ansi SQL'99 docs, and CURRENT as a date special constant
is not a part of the standard (although CURRENT is a keyword: it is
used in the context of cursors)
Ross
--
Ross J. Reedstrom, Ph.D., <reed...@rice.edu>
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St., Houston, TX 77005
Following up to myself:
Ah, I had forgotten that CURRENT is a magic value, like 'infinity'.
The standard does specify in section 6.19:
CURRENT_DATE, CURRENT_TIME, LOCALTIME, CURRENT_TIMESTAMP, and LOCALTIMESTAMP
as <datetime value function>
Which are currently implemented as generating the special value 'CURRENT',
which then get's stored in the column. This strikes me as _not_ standards
compliant. What do other DB's do with these? I think that they should
be equivalent to now(), returning a static date that is stored.
I do find the timestamp special values 'infinity' and '-infinity' very
useful, but have never found a use for 'current'.
Yes, actually something like this occurred to me after I sent the
above email. I had forgotten about the (rather pretty) extensible
type system; I can see that makes spotting optimisations such as the
above much more difficult. Seems like it might make a nice subject for
a paper, actually.
>
> Between that and the runtime that would be wasted during typical queries
> (IMHO searching for rearrangeable clauses would usually be fruitless),
> I really doubt that this is a good goal to pursue in Postgres.
I'm afraid I can't buy that second argument ;-) The time it takes to
optimise a query is asymptotically irrelevant, after all...
Jules