Re: return next in recursive function

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Petr Bravenec <pbravenec(at)solartec(dot)cz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: return next in recursive function
Date: 2003-10-02 15:03:00
Message-ID: 20031002080057.R62929@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 2 Oct 2003, Petr Bravenec wrote:

>
> I have found that when I use the RETURN NEXT command in recursive function,
> not all records are returned. The only records I can obtain from function
> are records from the highest level of recursion. Does exist some
> work-around?

> create or replace function foo (integer)
> returns setof foo as '
> declare pid alias for $1;
> declare rec RECORD;
> BEGIN
> FOR rec in select * from foo
> where foo.pid=pid LOOP
> return next rec;
> raise warning ''uid=% pid=%'',rec.uid,rec.pid;
> select into rec * from foo (rec.uid);

Shouldn't you be looping over these results in order to return them,
something like
FOR rec2 in select * from foo(rec.uid) LOOP
return next rec2;
END LOOP;

Otherwise, why should the results from the other call become results from
your call (what if you want to process them and not return all of them, or
what if it's a function with a different type?)

> END LOOP;
> return null;
> end;
> ' language 'plpgsql';

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-10-02 15:12:11 Re: return next in recursive function
Previous Message Tom Lane 2003-10-02 14:18:49 Re: BLOBs, pg_dump & pg_restore