Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 7ECD51337B58 for ; Wed, 13 Oct 2010 05:12:09 -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 44414-10 for ; Wed, 13 Oct 2010 08:12:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 0ED591337B55 for ; Wed, 13 Oct 2010 05:12:02 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id EF0F3AE3A7; Wed, 13 Oct 2010 16:11:58 +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 MLMv8wYxXITk; Wed, 13 Oct 2010 16:11:52 +0800 (WST) Received: from [10.1.1.8] (124-148-209-200.dyn.iinet.net.au [124.148.209.200]) (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 6E7ACAFDC6; Wed, 13 Oct 2010 16:11:52 +0800 (WST) Message-ID: <4CB569C4.7010100@postnewspapers.com.au> Date: Wed, 13 Oct 2010 16:11:48 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.12) Gecko/20100914 Thunderbird/3.0.8 MIME-Version: 1.0 To: "Reuven M. Lerner" CC: pgsql-performance@postgresql.org Subject: Re: SQL functions vs. PL/PgSQL functions References: <5549b04ab43625e39f74a859a2801ec3.squirrel@squirrelmail.lerner.co.il> In-Reply-To: <5549b04ab43625e39f74a859a2801ec3.squirrel@squirrelmail.lerner.co.il> Content-Type: text/plain; charset=UTF-8; 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 X-Spam-Level: X-Archive-Number: 201010/197 X-Sequence-Number: 40616 On 13/10/2010 3:30 PM, Reuven M. Lerner wrote: > My question is whether this is somehow to be expected. Under what > conditions will SQL functions be slower than PL/PgSQL functions? The main cases I can think of: - Where the SQL function is inlined (PL/PgSQL functions can't be inlined, some SQL functions can) and the inlining turns out to be a performance loss rather than a gain. - Where the PL/PgSQL function was constructing queries dynamically for EXECUTE ... USING, so each query contained its parameters directly. If converted to an SQL function (or a PL/PgSQL function using SELECT / PERFORM instead of EXECUTE ... USING) the planner will make more generic choices because it doesn't have stats on specific parameter values. These choices are sometimes not all that great. Beyond that, I'd have to wait to hear from someone who has more real knowledge than my hand-waving can provide. -- Craig Ringer Tech-related writing at http://soapyfrogs.blogspot.com/