Re: Query timing increased from 3s to 55s when used as function instead of select - Mailing list pgsql-performance

From Craig Ringer
Subject Re: Query timing increased from 3s to 55s when used as function instead of select
Date
Msg-id 4BFE90B2.2050604@postnewspapers.com.au
Whole thread Raw
In response to Query timing increased from 3s to 55s when used as function instead of select  (Tyler Hildebrandt <tyler@campbell-lange.net>)
Responses Re: Query timing increased from 3s to 55s when used as function instead of select
List pgsql-performance
On 21/05/2010 9:54 PM, Tyler Hildebrandt wrote:
> We're using a function that when run as a select statement outside of the
> function takes roughly 1.5s to complete whereas running an identical
> query within a function is taking around 55s to complete.
>
> We are lost as to why placing this query within a function as opposed to
> substituting the variables in a select statement is so drastically different.

This is a frequently asked question. It's the same issue as with
prepared queries, where the planner has to pick a more general plan when
it doesn't know the value of a parameter. The short answer is "work
around it by using EXECUTE ... USING to invoke your query dynamically".

( Oddly, this FAQ doesn't seem to be on the FAQ list at
http://wiki.postgresql.org/wiki/FAQ )

--
Craig Ringer


pgsql-performance by date:

Previous
From: Cédric Villemain
Date:
Subject: Re: shared_buffers advice
Next
From: Konrad Garus
Date:
Subject: Re: shared_buffers advice