Thread: IMMUTABLE STABLE functions, daily updates

IMMUTABLE STABLE functions, daily updates

From
Sorin Dudui
Date:

Hi,

 

I have some functions that select data from tables which are daily or monthly updated.  My functions are marked as STABLE. I am wondering if they perform better if I mark they as IMMUTABLE?

 

Thank you,

Sorin

 

Re: IMMUTABLE STABLE functions, daily updates

From
Thom Brown
Date:
On 10 November 2011 13:05, Sorin Dudui <sd@wigeogis.com> wrote:
> Hi,
>
>
>
> I have some functions that select data from tables which are daily or
> monthly updated.  My functions are marked as STABLE. I am wondering if they
> perform better if I mark they as IMMUTABLE?

No.  IMMUTABLE is only appropriate when there is no access to table
data from within the function.  An example of IMMUTABLE functions
would be  mathematical operations, where only the inputs and/or
function constants are used to produce a result.

--
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

Re: IMMUTABLE STABLE functions, daily updates

From
Craig Ringer
Date:


On Nov 10, 2011 9:26 PM, "Thom Brown" <thom@linux.com> wrote:
>
> On 10 November 2011 13:05, Sorin Dudui <sd@wigeogis.com> wrote:
> > Hi,
> >
> >
> >
> > I have some functions that select data from tables which are daily or
> > monthly updated.  My functions are marked as STABLE. I am wondering if they
> > perform better if I mark they as IMMUTABLE?
>
> No.  IMMUTABLE is only appropriate when there is no access to table
> data from within the function

Sure it can be faster - the same way defining "fibonacci(int)" to always return 42 is faster, just incorrect.

You can sometimes kinda get away with it if you are willing to reindex, drop prepared statements, reload functions, etc when the result changes. I would not recommend it.