Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 2871263222E for ; Thu, 27 May 2010 12:56:20 -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 11041-06 for ; Thu, 27 May 2010 15:56:12 +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 B409B632B59 for ; Thu, 27 May 2010 12:56:12 -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 C1E259F50; Thu, 27 May 2010 23:56:05 +0800 (WST) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 1B02FAEB0E; Thu, 27 May 2010 23:54:57 +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 DmhJaKm1dOsj; Thu, 27 May 2010 23:54:51 +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 0B57AAEB0B; Thu, 27 May 2010 23:54:51 +0800 (WST) Message-ID: <4BFE95E1.4070701@postnewspapers.com.au> Date: Thu, 27 May 2010 23:55:13 +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> <4BFE90B2.2050604@postnewspapers.com.au> In-Reply-To: <4BFE90B2.2050604@postnewspapers.com.au> 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/233 X-Sequence-Number: 38817 On 27/05/2010 11:33 PM, Craig Ringer wrote: > 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 ) Added as: http://wiki.postgresql.org/wiki/FAQ#Why_is_my_query_much_slower_when_run_as_a_prepared_query.3F and the subsequent entry too. Comments, edits, clarification appreciated. I know it's not as well written as it could be, could use archive links, etc; it's just pass 1. -- Craig Ringer