RETURN NEXT on result set

From: Scott Ribe <scott_ribe(at)killerbytes(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: RETURN NEXT on result set
Date: 2007-09-26 23:00:54
Message-ID: C32040C6.81227%scott_ribe@killerbytes.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Feature request: allow some way to "return next" a set of values. Usage:
recursive stored procedures to walk a tree. Example: given a table, find all
tables that inherit from it.

Right now, as far as can tell, that requires a little extra effort to merge
the results from different levels of recursion:

create or replace function "DbGetDescendantTables" (oid) returns setof oid
as $$
declare parentid alias for $1;
curid1 oid; curid2 oid;
c1 refcursor;
c2 refcursor;
begin
return next parentid;
open c1 for select inhrelid from pg_inherits where inhparent = parentid;
while 1 loop
fetch c1 into curid1;
if found then
open c2 for select * from "DbGetDescendantTables"(curid1);
while 1 loop
fetch c2 into curid2;
if found then
return next curid2;
else
exit;
end if;
end loop;
close c2;
else
exit;
end if;
end loop;
close c1;
end;
$$ language 'plpgsql';

But if a query result could directly be added to the result set being
accumulated, this would become:

create or replace function "DbGetDescendantTables" (oid) returns setof oid
as $$
declare parentid alias for $1;
curid1 oid;
c1 refcursor;
begin
return next parentid;
open c1 for select inhrelid from pg_inherits where inhparent = parentid;
while 1 loop
fetch c1 into curid1;
if found then
return next (select * from "DbGetDescendantTables"(curid1));
else
exit;
end if;
end loop;
close c1;
end;
$$ language 'plpgsql';

Sure, some of this could be avoid by accumulating and returning an array,
but in my case it's convenient for the procedures to produce result sets.

--
Scott Ribe
scott_ribe(at)killerbytes(dot)com
http://www.killerbytes.com/
(303) 722-0567 voice

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Smith 2007-09-26 23:02:29 Re: decode access privileges
Previous Message TUC Solutions 2007-09-26 22:27:12 Arabic Language