Re: stored procedures and dynamic queries

From: Richard Huxton <dev(at)archonet(dot)com>
To: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: stored procedures and dynamic queries
Date: 2007-12-03 19:06:29
Message-ID: 475453B5.8090809@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ivan Sergio Borgonovo wrote:
> Any general rule about dynamically generated queries in stored
> procedures vs. performances?

It's the same decision as any with any prepared plan vs plan-each-time
trade-off.

A query built using EXECUTE will have to be planned each time. That
costs you something but means the plan will have all the information it
needs.

A pre-planned query saves planning time on the second,third etc runs but
the plan won't change with the query-parameters.

So:
A query that's going to be executed a million times in a loop with the
same plan each time implies you want a pre-planned query.

A query executed once, or where changing input parameters would benefit
from changing plans implies you want to re-plan each time.

If you don't have a good reason to think it matters one way or the
other, then it probably doesn't. If it's not worth testing then it's not
worth optimising either.

Of course, in your example the query wouldn't work at all - you'd need
to use the EXECUTE command in plpgsql.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pascal Cohen 2007-12-03 19:37:58 Re: HA and Replication - how to choose among all the available solutions
Previous Message Scott Marlowe 2007-12-03 18:42:27 Re: Transaction problem