Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 7B0006325EF for ; Thu, 27 May 2010 18:21:43 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 49198-10 for ; Thu, 27 May 2010 21:21:36 +0000 (UTC) X-Greylist: delayed 00:07:40.574663 by SQLgrey-1.7.6 Received: from mail-pw0-f46.google.com (mail-pw0-f46.google.com [209.85.160.46]) by mail.postgresql.org (Postfix) with ESMTP id 06E146325E4 for ; Thu, 27 May 2010 18:21:35 -0300 (ADT) Received: by pwi7 with SMTP id 7so251618pwi.19 for ; Thu, 27 May 2010 14:21:34 -0700 (PDT) Received: by 10.141.2.8 with SMTP id e8mr389733rvi.205.1274994833211; Thu, 27 May 2010 14:13:53 -0700 (PDT) Received: from Craig-Jamess-MacBook-Pro.local (wsip-70-167-124-126.sd.sd.cox.net [70.167.124.126]) by mx.google.com with ESMTPS id l29sm1409303rvb.4.2010.05.27.14.13.51 (version=SSLv3 cipher=RC4-MD5); Thu, 27 May 2010 14:13:52 -0700 (PDT) Message-ID: <4BFEE08E.2040804@emolecules.com> Date: Thu, 27 May 2010 14:13:50 -0700 From: Craig James User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Does FILTER in SEQSCAN short-circuit AND? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201005/240 X-Sequence-Number: 38824 On 5/18/10 3:28 PM, Carlo Stonebanks wrote: > Sample code: > > SELECT * > FROM MyTable > WHERE foo = 'bar' AND MySlowFunc('foo') = 'bar' > > Let's say this required a SEQSCAN because there were no indexes to > support column foo. For every row where foo <> 'bar' would the filter on > the SEQSCAN short-circuit the AND return false right away, or would it > still execute MySlowFunc('foo') ? I asked a similar question a few years back, and the answer is that the planner just makes a guess and applies it to all functions. It has no idea whether your function is super fast or incredibly slow, they're all assigned the same cost. In this fairly simple case, the planner might reasonably guess that "foo = 'bar'" will always be faster than "AnyFunc(foo) = 'bar'". But for real queries, that might not be the case. In my case, I have a function that is so slow that it ALWAYS is good to avoid it. Unfortunately, there's no way to explain that to Postgres, so I have to use other tricks to force the planner not to use it. select * from (select * from MyTable where foo = 'bar' offset 0) where MySlowFunc(foo) = 'bar'; The "offset 0" prevents the planner from collapsing this query back into your original syntax. It will only apply MySlowFunc() to rows where you already know that foo = 'bar'. It would be nice if Postgres had a way to assign a cost to every function. Until then, you have to use convoluted SQL if you have a really slow function. Craig