Re: Recursion working in 7.3?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: jeff_patterson(at)agilent(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Recursion working in 7.3?
Date: 2003-03-05 21:44:15
Message-ID: 27853.1046900655@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

jeff_patterson(at)agilent(dot)com writes:
> create function level (INTEGER,INTEGER)
> RETURNS INTEGER AS'
> DECLARE
> post ALIAS FOR $1;
> parent ALIAS for $2;
> mygd INTEGER; --my grandfather
> mydad INTEGER;
> BEGIN
> IF parent = 0 THEN
> RETURN 1;
> ELSE
> select post,parent into mydad,mygd from threads where threads.post=parent; --get parent's info
> RETURN level(mydad,mygd)+1; --my level is my parent's level +1
> END IF;
> END;
> ' LANGUAGE 'plpgsql';

You are confusing yourself by using the names 'post' and 'parent' both
as table fields and as PLPGSQL variables. I think the SELECT in fact
returns exactly the function's arguments here, regardless of what row
the WHERE condition chooses. Presto, infinite recursion.

BTW, you should also have some defense against the possibility that
the passed-in parent ID is bogus, so that the SELECT finds no row ...

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jan Wieck 2003-03-05 21:52:01 Re: My employment with PeerDirect
Previous Message eric soroos 2003-03-05 21:14:42 MacRoman character set