Re: calling functions in select

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Jakub Ouhrabka <jouh8664(at)ss1000(dot)ms(dot)mff(dot)cuni(dot)cz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: calling functions in select
Date: 2002-04-24 14:48:01
Message-ID: 12151.1019659681@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Jakub Ouhrabka <jouh8664(at)ss1000(dot)ms(dot)mff(dot)cuni(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message tony 2002-04-24 15:19:19 how does NOT work?
Previous Message David Link 2002-04-24 14:45:11 Re: Why is outer Join way quicker?