Function call with offset and limit - Mailing list pgsql-hackers

From REYNAUD Jean-Samuel
Subject Function call with offset and limit
Date
Msg-id 1135187074.1316.63.camel@jsr.elma.loc
Whole thread Raw
Responses Re: Function call with offset and limit  ("Jim C. Nasby" <jnasby@pervasive.com>)
List pgsql-hackers
Hi all,

We need to find a solution for a strange problem. 
We have a plpgsql FUNCTION which performs an heavy job (named
test_func). 

CREATE or replace function test_func(z int) returns integer as $$
declare
tst integer;
begin
--
-- Large jobs with z
--
tst :=  nextval('test_truc');
return tst;
end;
$$ LANGUAGE plpgsql;


So I made this test:

test=# select setval('test_truc',1);setval
--------     1
(1 row)

test=#  select currval('test_truc') ;currval
---------      1
(1 row)

test=# select *,test_func(idkeyword) from tag offset 5000 limit 1;idkeyword |   test_func
-----------+-------------     5001 |   5002
(1 row)

test=# select currval('test_truc') ;currval
---------   5002
(1 row)


This demonstrates that the function is called 5001 times though only one
row is returned. Problem is that this heavy job is performed much, much
more than needed.

But, If I do:
test=# select *,(select test_func(1)) from tag offset 5000 limit 1;
My function is called only once.

Is there any work around ?


Thanks
-- 
REYNAUD Jean-Samuel <reynaud@elma.fr>
Elma



pgsql-hackers by date:

Previous
From: "Andrew Dunstan"
Date:
Subject: pgxs/windows
Next
From: Dave Cramer
Date:
Subject: replicating tsearch2 across versions of postgres