Re: calling functions in select - Mailing list pgsql-general

From Tom Lane
Subject Re: calling functions in select
Date
Msg-id 12151.1019659681@sss.pgh.pa.us
Whole thread Raw
In response to Re: calling functions in select  (Jakub Ouhrabka <jouh8664@ss1000.ms.mff.cuni.cz>)
List pgsql-general
Jakub Ouhrabka <jouh8664@ss1000.ms.mff.cuni.cz> writes:
>> 1)
>> FOR lr_rec IN SELECT func(table.x, ...) FROM table,... WHERE ... LOOP
>> END LOOP;
>>
>> 2)
>> FOR lr_rec IN SELECT * FROM table,... WHERE ... LOOP
>> li_x := func(lr_rec.x, ...);
>> END LOOP;

> i'll answer myself, it depends on whether func is iscachable or not,
> right?

No, I don't believe so.  These should produce the same result except
possibly for the time at which func() is executed relative to what else
you might be doing inside that FOR loop.  plpgsql's FOR reads multiple
rows from the SELECT at a time, so if func() is in the SELECT then it
may get evaluated several rows ahead of where your FOR loop thinks it
is.  But in your case 2, func() is guaranteed to be executed in lockstep
with other actions taken in the FOR-loop body.

            regards, tom lane

pgsql-general by date:

Previous
From: David Link
Date:
Subject: Re: Why is outer Join way quicker?
Next
From: tony
Date:
Subject: how does NOT work?