X-Original-To: pgsql-performance@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 97221476393 for ; Thu, 8 May 2003 12:13:29 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id B9435474E4F for ; Thu, 8 May 2003 12:13:24 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id B5869D605; Thu, 8 May 2003 09:13:29 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id AB36C5C0A; Thu, 8 May 2003 09:13:29 -0700 (PDT) Date: Thu, 8 May 2003 09:13:29 -0700 (PDT) From: Stephan Szabo To: David Olbersen Cc: Subject: Re: Type casting and indexes In-Reply-To: Message-ID: <20030508090101.K43697-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=-26.0 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200305/106 X-Sequence-Number: 1885 On Thu, 8 May 2003, David Olbersen wrote: > Anyway, I've found a (bug|feature|standard?) with type casting and index usage. > > I've got a table with a column that's a timestamp with time zone. This > column is indexed. If I issue the "normal" query of: > > SELECT count(*) FROM foo WHERE bar > '2003-05-05':;timestamp > > I get the following EXPLAIN ANALYZE output: > > urldb=> explain select count(*) from foo where bar > '2003-05-05'::timestamp; > QUERY PLAN > ------------------------------------------------------------------------ > Aggregate (cost=89960.75..89960.75 rows=1 width=0) (actual time= > 56706.58..56706.58 rows=1 loops=1) > -> Seq Scan on urlinfo (cost=0.00..87229.45 rows=1092521 width=0) (actual > time=25.37..56537.86 rows=27490 loops=1) > Filter: (ratedon > ('2003-05-05 00:00:00'::timestamp without time > zone)::timestamp with time zone) > Total runtime: 56706.67 msec > > So it seems that the type conversion is killing the use of the index, > even though the type conversion has to happen for the condition to be > tested. IIRC, timestamp->timestamptz is not considered to give a constant value (ie, is not stable) probably since it depends on timezone settings which could be changed (for example by a function) during the query, so for each row the conversion from '2003-05-05 00:00:00'::timestamp without time zone to a timestamp with time zone can potentially give a different answer.