X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id E021611F6B4E for ; Thu, 13 Apr 2006 12:21:38 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: -2.464 X-Spam-Level: X-Spam-Status: No, score=-2.464 required=5 tests=[BAYES_00=-2.599, FORGED_RCVD_HELO=0.135] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id m5XnDLGlDpPU for ; Thu, 13 Apr 2006 12:21:30 -0300 (ADT) X-Greylist: delayed 00:22:36.544925 by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 63A1811F6ABC for ; Thu, 13 Apr 2006 12:21:16 -0300 (ADT) Received: from mail.crpa.it (marconi.crpa.it [212.22.134.249]) by svr4.postgresql.org (Postfix) with ESMTP id C99615AF8EF for ; Thu, 13 Apr 2006 14:58:47 +0000 (GMT) Received: from galois.langroup.crpa.it (galois [212.22.134.9]) by mail.crpa.it (8.13.4/8.13.3/1) with ESMTP id k3DExsQV029814 for ; Thu, 13 Apr 2006 16:59:54 +0200 Received: from crpa.it ([212.22.134.85]) by galois.langroup.crpa.it (8.13.3/8.13.3/1) with ESMTP id k3DF2j78009328 for ; Thu, 13 Apr 2006 17:02:45 +0200 Message-ID: <443E66F5.2010100@crpa.it> Date: Thu, 13 Apr 2006 16:57:57 +0200 From: Cristian Veronesi User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040114 X-Accept-Language: it, en-us MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: index is not used if I include a function that returns current time in my query Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.52 on 212.22.134.50 X-Archive-Number: 200604/302 X-Sequence-Number: 18386 Hello, postgresql 7.4.8 on SuSE Linux here. I have a table called DMO with a column called ORA_RIF defined as "timestamp without time zone" ; I created an index on this table based on this column only. If I run a query against a text literal the index is used: > explain select * from dmo where ora_rif>'2006-01-01'; QUERY PLAN ----------------------------------------------------------------------------------------- Index Scan using dmo_ndx02 on dmo (cost=0.00..1183.23 rows=736 width=156) Index Cond: (ora_rif > '2006-01-01 00:00:00'::timestamp without time zone) If I try to use a function that returns the current time instead, a sequential scan is always performed: > explain select * from dmo where ora_rif>localtimestamp; QUERY PLAN ------------------------------------------------------------------------------ Seq Scan on dmo (cost=0.00..1008253.22 rows=2703928 width=156) Filter: (ora_rif > ('now'::text)::timestamp(6) without time zone) > explain select * from dmo where ora_rif>localtimestamp::timestamp without time zone; QUERY PLAN ------------------------------------------------------------------------------ Seq Scan on dmo (cost=0.00..1008253.22 rows=2703928 width=156) Filter: (ora_rif > ('now'::text)::timestamp(6) without time zone) ... etc. ... (tried with all datetime functions with and without cast) I even tried to write a function that explicitly returns a "timestamp without time zone" value: create or replace function f () returns timestamp without time zone as ' declare x timestamp without time zone ; begin x := ''2006-01-01 00:00:00''; return x ; end ; ' language plpgsql ; But the result is the same: > explain select * from dmo ora_rif>f(); QUERY PLAN ----------------------------------------------------------------------------- Seq Scan on dmo (cost=0.00..987973.76 rows=2703928 width=156) Filter: (ora_rif > f()) Any suggestion? Kind regards, -- Cristian Veronesi - C.R.P.A. S.p.A. - Reggio Emilia, Italy The first thing you need to learn about databases is that they are not just a fancy file system for storing data.