Thread: Are cached IMMUTABLE values persistent between sessions

Are cached IMMUTABLE values persistent between sessions

From
"Donald Fraser"
Date:
Is the returned value of a function defined as IMMUTABLE cached globally? In other words could postgresql potentially return a cached value obtained from one client session to a different client session?
 
Thanks in advance
Donald Fraser 

Re: Are cached IMMUTABLE values persistent between sessions

From
Alvaro Herrera
Date:
On Thu, Jun 23, 2005 at 02:26:23PM +0100, Donald Fraser wrote:
> Is the returned value of a function defined as IMMUTABLE cached
> globally? In other words could postgresql potentially return a cached
> value obtained from one client session to a different client session?

Return values from functions are never cached at all, not even for
immutable functions.  The mutability flag (currently) is only used for
deciding the "optimizability" of the function within the query's
execution.

--
Alvaro Herrera (<alvherre[a]surnet.cl>)
"El que vive para el futuro es un iluso, y el que vive para el pasado,
un imbécil" (Luis Adler, "Los tripulantes de la noche")

Re: Are cached IMMUTABLE values persistent between sessions

From
Tom Lane
Date:
"Donald Fraser" <postgres@kiwi-fraser.net> writes:
> Is the returned value of a function defined as IMMUTABLE cached =
> globally?

No, in fact it isn't cached at all.  IMMUTABLE tells the planner that
it's OK to fold a function call with constant inputs to a constant
result value at plan time.  Nothing more.

            regards, tom lane

Re: Are cached IMMUTABLE values persistent between sessions

From
Michael Fuhr
Date:
On Thu, Jun 23, 2005 at 02:26:23PM +0100, Donald Fraser wrote:
>
> Is the returned value of a function defined as IMMUTABLE cached
> globally?  In other words could postgresql potentially return a
> cached value obtained from one client session to a different client
> session?

You could experiment with this by adding a debugging RAISE statement
to an IMMUTABLE PL/pgSQL function to see when it's called.  I suspect
there's no global caching; if there is then I haven't been able to
make it happen in tests.  There doesn't even appear to be any caching
between disparate queries in the same session unless a prepared
plan is involved (e.g., a plan made with PREPARE or a cached plan
from a PL/pgSQL function) and the result of the IMMUTABLE function
could be determined at planning time.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Re: Are cached IMMUTABLE values persistent between sessions

From
"Donald Fraser"
Date:
> "Donald Fraser" writes:
>> Is the returned value of a function defined as IMMUTABLE cached =
>> globally?

"Tom Lane" writes:
> No, in fact it isn't cached at all.  IMMUTABLE tells the planner that
> it's OK to fold a function call with constant inputs to a constant
> result value at plan time.  Nothing more.

Ok I'm sort of clutching at straws here...
What about this scenario: I'm aware that sql statements within PL/pgSQL
functions get cached. Therefore if the query planner can fold a function with a
constant result, could that query plan get cached by PL/pgSQL with a folded
value?

Regards
Donald Fraser