X-Original-To: pgsql-sql@postgresql.org Received: from localhost (unknown [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id 7E25A92514E for ; Tue, 20 May 2003 13:47:23 -0400 (EDT) Received: from developer.postgresql.org ([64.117.224.193]) by localhost (developer.postgresql.org [64.117.224.193:10024]) (amavisd-new) with ESMTP id 38462-05 for ; Tue, 20 May 2003 13:47:09 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by developer.postgresql.org (Postfix) with ESMTP id 4074B92514F for ; Tue, 20 May 2003 13:47:09 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 0A068D610; Tue, 20 May 2003 10:47:06 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id EB9E55C0C; Tue, 20 May 2003 10:47:06 -0700 (PDT) Date: Tue, 20 May 2003 10:47:06 -0700 (PDT) From: Stephan Szabo To: Kluge Cc: Subject: Re: plpgsql recursion In-Reply-To: <01d001c31ef1$7a7fbfd0$0600a8c0@stefano> Message-ID: <20030520104342.L69760-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/397 X-Sequence-Number: 13562 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.