Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id AE6B2B5DECA for ; Tue, 1 Nov 2011 23:21:44 -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 87582-02-5 for ; Wed, 2 Nov 2011 02:21:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-vx0-f174.google.com (mail-vx0-f174.google.com [209.85.220.174]) by mail.postgresql.org (Postfix) with ESMTP id 36B57B5DEBE for ; Tue, 1 Nov 2011 23:21:28 -0300 (ADT) Received: by vcge1 with SMTP id e1so6293840vcg.19 for ; Tue, 01 Nov 2011 19:21:24 -0700 (PDT) Received: by 10.52.109.102 with SMTP id hr6mr2460106vdb.44.1320200484854; Tue, 01 Nov 2011 19:21:24 -0700 (PDT) Received: from ayaki.localdomain (58-7-142-223.dyn.iinet.net.au. [58.7.142.223]) by mx.google.com with ESMTPS id p7sm1269039vdh.9.2011.11.01.19.21.22 (version=SSLv3 cipher=OTHER); Tue, 01 Nov 2011 19:21:24 -0700 (PDT) Message-ID: <4EB0A920.1010209@ringerc.id.au> Date: Wed, 02 Nov 2011 10:21:20 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1 MIME-Version: 1.0 To: Sabin Coanda CC: "pgsql-performance@postgresql.org" Subject: Re: procedure takes much more time than its query statement References: <864BF5C35722CC4C880BE29B65FD396B4DBB059BEF@exchg1-bv.drm-bv.deuromedia.com> In-Reply-To: <864BF5C35722CC4C880BE29B65FD396B4DBB059BEF@exchg1-bv.drm-bv.deuromedia.com> 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 X-Spam-Level: X-Archive-Number: 201111/8 X-Sequence-Number: 45519 On 11/01/2011 10:01 PM, Sabin Coanda wrote: > Hi there, > > I have the function: > CREATE OR REPLACE FUNCTION "Test"( ... ) > RETURNS SETOF record AS > $BODY$ > BEGIN > RETURN QUERY > SELECT ...; > END; > $BODY$ > LANGUAGE 'plpgsql' STABLE > > The function call takes about 5 minute to proceed, but using directly its > query statement, after replacing the arguments with the same values, it > takes just 5 seconds ! > > I repeat the test several times and the duration is the same. > > What is wrong ? > Is it also slow if, outside PL/PgSQL in a regular psql session, you PREPARE the query, then EXECUTE it? If so, you're being bitten by a generic query plan. The server does a better job when it knows what parameter is used when it's planning the statement. To work around it, you can use the PL/PgSQL 'EXECUTE ... USING ...' statement to force a re-plan of the statement for every time it's run. -- Craig Ringer