Brian Hirt wrote:
> I have a query that fetches information from a log, based on an indexed
> column. The timestamp in the table is with time zone, and the server
> time zone is not GMT. However, i want all of the timestamps for a
> particular day in GMT. If i simply use a date constant, the index is
> used, but the incorrect rows are fetched, since the date is converted
> to a timestamp in the server's time zone. When i cast that date to a
> GMT date, the index is no longer used. Is there some better way to
> write the query so that the planner will use the index? I have
> simplied the queries below to demonstrate the problem i'm having.
> Thanks for any advice.
>
>
> SLOW:
> basement=# select count(*) from redir_log
> basement-# where redir_timestamp >= '10/14/2004'::timestamp without
> time zone at time zone 'GMT';
Not quite what's wanted. Try keeping things as a timestamp with timezone
(you can add a timestamp to a date):
SELECT count(*) FROM redir_log
WHERE redir_timestamp BETWEEN '2004-10-14+00'::timestamptz AND
CURRENT_TIMESTAMP;
Putting two bounds on the range can also help index usage.
In actual fact, since you're comparing to a timestamp and not a date,
I'd personally supply a valid timestamptz: '2004-10-14 00:00:00+00'
--
Richard Huxton
Archonet Ltd