X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E29383A5631 for ; Wed, 1 Dec 2004 17:38:41 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 97323-09 for ; Wed, 1 Dec 2004 17:38:32 +0000 (GMT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by svr1.postgresql.org (Postfix) with ESMTP id A344A3A5423 for ; Wed, 1 Dec 2004 17:38:32 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 4.42) id 1CZYQU-0008Sq-GE; Wed, 01 Dec 2004 17:38:02 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 87567167E7; Wed, 1 Dec 2004 17:38:28 +0000 (GMT) Message-ID: <41AE0194.9050705@archonet.com> Date: Wed, 01 Dec 2004 17:38:28 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 0.9 (X11/20041103) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Brian Hirt Cc: 'Postgresql Performance' Subject: Re: query with timestamp not using index References: <95241C28-43B8-11D9-AD0F-000D93AD2E74@mobygames.com> In-Reply-To: <95241C28-43B8-11D9-AD0F-000D93AD2E74@mobygames.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200412/20 X-Sequence-Number: 9395 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