From: Craig Ringer <ringerc@ringerc.id.au>
To: Sabin Coanda <s.coanda@deuromedia.com>
Cc: pgsql-performance@postgresql.org <pgsql-performance@postgresql.org>
Subject: Re: procedure takes much more time than its query statement
Date: Wed, 02 Nov 2011 10:21:20 +0800
Message-ID: <4EB0A920.1010209@ringerc.id.au> (raw)
In-Reply-To: <864BF5C35722CC4C880BE29B65FD396B4DBB059BEF@exchg1-bv.drm-bv.deuromedia.com>
References: <864BF5C35722CC4C880BE29B65FD396B4DBB059BEF@exchg1-bv.drm-bv.deuromedia.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: ringerc@ringerc.id.au, s.coanda@deuromedia.com
Subject: Re: procedure takes much more time than its query statement
In-Reply-To: <4EB0A920.1010209@ringerc.id.au>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox