Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 11FFBB5DCC7 for ; Wed, 2 Nov 2011 05:21:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 20737-10 for ; Wed, 2 Nov 2011 08:21:01 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 92E47B5DCBB for ; Wed, 2 Nov 2011 05:21:01 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id pA28L0iu027947 for ; Wed, 2 Nov 2011 05:21:00 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id pA28GDq3024202 for pgsql-performance@postgresql.org; Wed, 2 Nov 2011 05:16:13 -0300 (ADT) (envelope-from news) From: "Sabin Coanda" X-Newsgroups: pgsql.performance Subject: Re: procedure takes much more time than its query statement Date: Wed, 2 Nov 2011 10:15:45 +0200 Organization: Hub.Org Networking Services Lines: 48 Message-ID: References: <864BF5C35722CC4C880BE29B65FD396B4DBB059BEF@exchg1-bv.drm-bv.deuromedia.com> <4EB0A920.1010209@ringerc.id.au> X-Complaints-To: usenet@news.hub.org X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6109 X-RFC2646: Format=Flowed; Response To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.897 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201, TO_NO_BRKTS_MSFT=0.204 X-Spam-Level: X-Archive-Number: 201111/10 X-Sequence-Number: 45521 Excelent ! You are right Thanks a lot Sabin "Craig Ringer" wrote in message news:4EB0A920.1010209@ringerc.id.au... > 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 > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance >