X-Original-To: pgsql-sql@postgresql.org Received: from localhost (unknown [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id 2EA649250BD for ; Tue, 20 May 2003 12:59:56 -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 23822-03 for ; Tue, 20 May 2003 12:59:50 -0400 (EDT) Received: from smtp1.cp.tin.it (vsmtp1.tin.it [212.216.176.221]) by developer.postgresql.org (Postfix) with ESMTP id 2EBDC9250C8 for ; Tue, 20 May 2003 12:59:49 -0400 (EDT) Received: from stefano (80.116.83.30) by smtp1.cp.tin.it (6.7.016) id 3EC8A8BB00083D75 for pgsql-sql@postgresql.org; Tue, 20 May 2003 18:59:47 +0200 Message-ID: <01d001c31ef1$7a7fbfd0$0600a8c0@stefano> From: "Kluge" To: Subject: plpgsql recursion Date: Tue, 20 May 2003 19:01:38 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/393 X-Sequence-Number: 13558 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); END IF; END LOOP; RETURN ''ok'' || testing::text; END; ' LANGUAGE 'plpgsql'; I would use this function BEFORE inserting the new row. But if i try SELECT dba_test(4,1); i don't have the result i expect. Can i you give me an hint where am i wrong? Thank you! Stefano Vita Finzi kluge@despammed.com