I want to give out result from a PostgreSQL database sequentially like a python Generator. When the function get_data is called every time I want to return a new 10 rows from the rows.
I defined a sequence called an_iter which is increased by 10:
create sequence an_iter
increment by 10;
My plan is to create a function that
- increases the sequence by 10
- return the rows which are bigger than sequence's current value (limit to 10)
My function definition:
CREATE OR REPLACE FUNCTION public.get_data()
RETURNS SETOF ppdb
LANGUAGE plpgsql AS
$func$
declare
val integer := 0;
BEGIN
select nextval('an_iter') into val;
Return Query SELECT * from ppdb where i<= val+10 limit 10;
END
$func$;
Though I called the function the same result set is returned.
select nextval('an_iter') into val;can be simplified toval := nextval('an_iter');(and that's a bit more efficient as well)