Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id BD712632904 for ; Thu, 27 May 2010 12:34:11 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 76573-01 for ; Thu, 27 May 2010 15:34:04 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.4.227.11]) by mail.postgresql.org (Postfix) with ESMTP id 930466324C3 for ; Thu, 27 May 2010 12:34:04 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id 59C049F50; Thu, 27 May 2010 23:33:56 +0800 (WST) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 5C1CCAEB08; Thu, 27 May 2010 23:32:49 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id nLoCGogkHGPd; Thu, 27 May 2010 23:32:44 +0800 (WST) Received: from [10.1.1.7] (124-168-255-216.dyn.iinet.net.au [124.168.255.216]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id E575CAEB06; Thu, 27 May 2010 23:32:43 +0800 (WST) Message-ID: <4BFE90B2.2050604@postnewspapers.com.au> Date: Thu, 27 May 2010 23:33:06 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.9) Gecko/20100317 Thunderbird/3.0.4 MIME-Version: 1.0 To: Tyler Hildebrandt CC: pgsql-performance@postgresql.org, Sebastian Ritter , rory Subject: Re: Query timing increased from 3s to 55s when used as function instead of select References: <20100521135415.GA3445@campbell-lange.net> In-Reply-To: <20100521135415.GA3445@campbell-lange.net> 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=0.79 tagged_above=-10 required=5 tests=BAYES_50=0.8, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201005/230 X-Sequence-Number: 38814 On 21/05/2010 9:54 PM, Tyler Hildebrandt wrote: > We're using a function that when run as a select statement outside of the > function takes roughly 1.5s to complete whereas running an identical > query within a function is taking around 55s to complete. > > We are lost as to why placing this query within a function as opposed to > substituting the variables in a select statement is so drastically different. This is a frequently asked question. It's the same issue as with prepared queries, where the planner has to pick a more general plan when it doesn't know the value of a parameter. The short answer is "work around it by using EXECUTE ... USING to invoke your query dynamically". ( Oddly, this FAQ doesn't seem to be on the FAQ list at http://wiki.postgresql.org/wiki/FAQ ) -- Craig Ringer