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-03 05:58:38
Message-ID: 20031002225211.V85561@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 3 Oct 2003, Petr Bravenec wrote:

> Stephan Szabo napsal(a):
>
> >>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?)
>
> Yes, I understand but I expect that the Postgres will try to do what I
> wrote.

AFAICS it is doing exactly what you wrote. It's giving you a row in rec
from the resultset of the recursive call which you do nothing with and
then overwrite back at the top of the loop.

It's not determining it's called recursively or anything of the sort, each
function call has its own result set, there's no implicit union or any
such going on. That'd be doing something other than what you wrote.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alex 2003-10-03 07:19:11 pg_restore takes ages
Previous Message Dennis Gearon 2003-10-03 05:43:23 anyone give me some good liniks to ...