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 5E7DE11F6CAD for ; Thu, 13 Apr 2006 12:50:20 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: -2.344 X-Spam-Level: X-Spam-Status: No, score=-2.344 required=5 tests=[AWL=0.257, BAYES_00=-2.599, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id ssZGv1rU8cG2 for ; Thu, 13 Apr 2006 12:50:17 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 3A79011F6CA9 for ; Thu, 13 Apr 2006 12:50:17 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k3DFo8H2020559; Thu, 13 Apr 2006 11:50:08 -0400 (EDT) To: Cristian Veronesi cc: pgsql-performance@postgresql.org Subject: Re: index is not used if I include a function that returns current time in my query In-reply-to: <443E66F5.2010100@crpa.it> References: <443E66F5.2010100@crpa.it> Comments: In-reply-to Cristian Veronesi message dated "Thu, 13 Apr 2006 16:57:57 +0200" Date: Thu, 13 Apr 2006 11:50:08 -0400 Message-ID: <20558.1144943408@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200604/305 X-Sequence-Number: 18389 Cristian Veronesi writes: > If I try to use a function that returns the current time instead, a > sequential scan is always performed: > ... > Any suggestion? 1. Use something newer than 7.4 ;-) 2. Set up a dummy range constraint, ie select ... where ora_rif > localtimestamp and ora_rif < 'infinity'; The problem you have is that the planner doesn't know the value of the function and falls back to a default assumption about the selectivity of the '>' condition --- and that default discourages indexscans. (Note the very large estimate of number of rows returned.) In the range-constraint situation, the planner still doesn't know the value of the function, but its default assumption for a range constraint is tighter and it (probably) will choose an indexscan. Since PG 8.0, the planner understands that it's reasonable to pre-evaluate certain functions like localtimestamp to obtain better-than-guess values about selectivity, so updating would be a better fix. regards, tom lane