Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 45753475A9E for ; Fri, 27 Dec 2002 17:06:07 -0500 (EST) Received: from shire.ontko.com (shire.ontko.com [199.164.165.1]) by postgresql.org (Postfix) with ESMTP id AB76D475461 for ; Fri, 27 Dec 2002 17:06:06 -0500 (EST) Received: from shire.ontko.com (rayo@localhost [127.0.0.1]) by shire.ontko.com (8.12.6/8.12.6/Debian-6Woody) with ESMTP id gBRM67pq014144; Fri, 27 Dec 2002 17:06:07 -0500 Received: (from rayo@localhost) by shire.ontko.com (8.12.6/8.12.6/Debian-6Woody) id gBRM67HR014142; Fri, 27 Dec 2002 17:06:07 -0500 From: Ray Ontko Date: Fri, 27 Dec 2002 17:06:07 -0500 To: pgsql-admin@postgresql.org Cc: Nick Fankhauser Subject: now() and ::date Message-ID: <20021227220607.GB11723@ontko.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.28i X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/313 X-Sequence-Number: 6808 Howdy, I'm having a little trouble understanding the query optimizer related to a timestamp with time zone column. I have a table called "event" with a not null column called "event_date_time" of type "timestamp with time zone" that has been recently analyzed. There are about 500,000 rows in the table and about 10,000 distinct values. My basic questions are: Why does "now()" disqualify use of the index? Why does "::date" disqualify use of the index? 1) This works: explain select count(*) from event where event_date_time >= '2002-12-25'::timestamp with time zone - '1 month'::interval and event_date_time < '2002-12-25'::timestamp with time zone ; NOTICE: QUERY PLAN: Aggregate (cost=4647.02..4647.02 rows=1 width=0) -> Index Scan using event_date_time on event (cost=0.00..4643.95 rows=1227 w idth=0) EXPLAIN 2) This fails to use the index when I cast the literals as "date". Why? explain select count(*) from event where event_date_time >= '2002-12-25'::date - '1 month'::interval and event_date_time < '2002-12-25'::date ; NOTICE: QUERY PLAN: Aggregate (cost=21479.33..21479.33 rows=1 width=0) -> Seq Scan on event (cost=0.00..21337.66 rows=56665 width=0) EXPLAIN 3) This fails to use the index when I try to use "now()" instead of a literal date. Why? explain select count(*) from event where event_date_time >= now()::timestamp with time zone - '1 month'::interval and event_date_time < now()::timestamp with time zone ; NOTICE: QUERY PLAN: Aggregate (cost=21479.33..21479.33 rows=1 width=0) -> Seq Scan on event (cost=0.00..21337.66 rows=56665 width=0) EXPLAIN Ray ---------------------------------------------------------------------- Ray Ontko rayo@ontko.com Phone 1.765.935.4283 Fax 1.765.962.9788 Ray Ontko & Co. Software Consulting Services http://www.ontko.com/