pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Brian Hirt <bhirt@mobygames.com>
Cc: Postgresql Performance <pgsql-performance@postgresql.org>
Subject: Re: query with timestamp not using index
Date: Wed, 01 Dec 2004 17:38:28 +0000
Message-ID: <41AE0194.9050705@archonet.com> (raw)
In-Reply-To: <95241C28-43B8-11D9-AD0F-000D93AD2E74@mobygames.com>
References: <95241C28-43B8-11D9-AD0F-000D93AD2E74@mobygames.com>

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



view thread (5+ messages)  latest in thread

Message-ID: <41AE0194.9050705@archonet.com>
Permalink:  ../41AE0194.9050705@archonet.com/
Also on:    postgresql.org/message-id/41AE0194.9050705@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: dev@archonet.com, bhirt@mobygames.com
  Subject: Re: query with timestamp not using index
  In-Reply-To: <41AE0194.9050705@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox