Tom Lane wrote:
> "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com> writes:
>> Shouldn't PG make all efforts to not execute something when the result is
>> already known?
>
> Not if said effort would cost more than is saved, which would be by far
> the most likely result if we tried to cache all function results.
>
> regards, tom lane
>
In this case, with the function being called over and over again inside
a nested loop, it would be worth the effort. I'm not even talking about
caching the result for the whole transaction block, just replacing the
function call before continuing the query.
Perhaps I can get around this problem by creating a temp table inside
the function that is dropped on transaction commit.