From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Kluge <kluge@despammed.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: plpgsql recursion
Date: Tue, 20 May 2003 10:47:06 -0700 (PDT)
Message-ID: <20030520104342.L69760-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <01d001c31ef1$7a7fbfd0$0600a8c0@stefano>
On Tue, 20 May 2003, Kluge wrote:
> Greetings!
> I have a table like:
>
> node parent
> 1 2
> 2 3
> 3 4
>
> Since i traverse this table with a recursive function, i want to avoid
> infinite recursion loop. I have wrote a function to check that a new record
> does not create a circular dependency. The function i wrote is as follow:
>
> CREATE OR REPLACE FUNCTION dba_test(INTEGER,INTEGER) RETURNS TEXT AS '
> DECLARE
> traversing ALIAS FOR $1;
> testing ALIAS FOR $2;
> t_rec RECORD;
> BEGIN
> FOR t_rec IN SELECT node,parent FROM dba_test WHERE parent = traversing
> LOOP
> IF t_rec.node = testing THEN
> RETURN ''Circular'';
> ELSE
> PERFORM dba_test(t_rec.node,testing);
Here you're calling dba_test recursively and ignoring its result and going
on. I don't think that'll do what you want. If parent is unique, I think
you can just RETURN dba_test(t_rec.node, testing). If it's not, you'll
have to do some additional work to return the correct result.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sszabo@megazone23.bigpanda.com, kluge@despammed.com
Subject: Re: plpgsql recursion
In-Reply-To: <20030520104342.L69760-100000@megazone23.bigpanda.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox