From: John Arbash Meinel <john@arbash-meinel.com>
To: KeithW@narrowpathinc.com
Cc: PostgreSQL Perform <pgsql-performance@postgresql.org>
Subject: Re: Dynamic query perormance
Date: Wed, 30 Mar 2005 11:57:49 -0600
Message-ID: <424AE89D.6050501@arbash-meinel.com> (raw)
In-Reply-To: <20050330170418.M61319@narrowpathinc.com>
References: <20050330170418.M61319@narrowpathinc.com>
Keith Worthington wrote:
>Hi All,
>
>I am developing a simple set returning function as my first step towards more
>complicated processes. I would like to understand the implications of using
>the dynamic query capability.
>
>I have built two forms of an identically performing function. The first uses
>a simple IF-THEN-ELSIF-THEN-ELSE structure to choose which query to run. The
>second builds the query dynamically using the FOR-IN-EXECUTE structure and a
>CASE statement.
>
>The documentation
>(http://www.postgresql.org/docs/8.0/interactive/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERA...)
>indicates that a dynamic query (EXECUTE) is replanned for every LOOP iteration.
>
> This is like the previous form, except that the source
> SELECT statement is specified as a string expression,
> which is evaluated and replanned on each entry to the
> FOR loop. This allows the programmer to choose the speed
> of a preplanned query or the flexibility of a dynamic
> query, just as with a plain EXECUTE statement.
>
>That seems like a potential performance problem. I don't understand why the
>query would be planned for every LOOP iteration when the LOOP is over the
>record set.
>
>
>
Reading the documentation and looking at the example, I don't think
you're query will be re-planned for each entry in the loop.
I think it will be planned each time the FOR loop is started.
If you have the EXECUTE *inside* the LOOP, then it would be re-planned
for each entry.
At least that is the case for a normal EXECUTE without any for loop.
Each time the function is called, the statement is re-planned. Versus
without EXECUTE when the planning is done at function declaration time.
I would guess that the FOR .. IN EXECUTE .. LOOP runs the EXECUTE one
time, and generates the results which it then loops over. Because that
is what FOR .. IN SELECT .. LOOP does (you don't re-evaluate the SELECT
for each item in the result set).
On the other hand, I don't know of any way to test this, unless you have
a query that you know takes a long time to plan, and can compare the
performance of FOR IN EXECUTE versus FOR IN SELECT.
John
=:->
>Your comments are appreciated.
>
>Kind Regards,
>Keith
>
>
>
Attachments:
signature.asc (application/pgp-signature, 252B) download
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: john@arbash-meinel.com, KeithW@narrowpathinc.com
Subject: Re: Dynamic query perormance
In-Reply-To: <424AE89D.6050501@arbash-meinel.com>
* 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