Received: from mail.postgresql.org (webmail.postgresql.org [216.126.85.28]) by mail.postgresql.org (8.11.1/8.11.1) with SMTP id f1CKepx56787 for ; Mon, 12 Feb 2001 15:40:51 -0500 (EST) (envelope-from pgsql-sql-owner@postgresql.org) Received: from gadolinium.btinternet.com (gadolinium.btinternet.com [194.73.73.111]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f1A8Z1x11861 for ; Sat, 10 Feb 2001 03:35:03 -0500 (EST) (envelope-from m.proctor@bigfoot.com) Received: from [213.122.183.196] (helo=localhost.localdomain) by gadolinium.btinternet.com with smtp (Exim 3.03 #83) id 14RVUf-0004Z4-00; Sat, 10 Feb 2001 08:34:57 +0000 From: mark proctor Reply-To: m.proctor@bigfoot.com To: Tom Lane Subject: Recusrive Functions in 7.0.3 Date: Sat, 10 Feb 2001 08:43:04 +0000 X-Mailer: KMail [version 1.1.99] Content-Type: text/plain; charset="iso-8859-1" Cc: pgsql-sql@postgresql.org References: <01020920225704.05071@localhost.localdomain> <28783.981775759@sss.pgh.pa.us> In-Reply-To: <28783.981775759@sss.pgh.pa.us> MIME-Version: 1.0 Message-Id: <01021008430408.05071@localhost.localdomain> Content-Transfer-Encoding: 8bit X-Archive-Number: 200102/229 X-Sequence-Number: 1420 If I remove the line calling PERFORM it works without problems moving the children node for the passed integer into test and then exiting. I can then repeat the process going through the output to test iteratively and do not have any problems. However if I put the PERFORM line back in to create a recursive function it just goes on forever, and I only have 6 nodes. CREATE FUNCTION get_children (integer) RETURNS integer AS ' DECLARE pnode_parent ALIAS FOR $1; rec RECORD; BEGIN FOR rec IN SELECT * FROM tree_adjacency_matrix WHERE node_parent = pnode_parent LOOP INSERT INTO test (node1, node2) VALUES(rec.node_child, rec.node_parent); PERFORM get_children(rec.node_child); END LOOP; RETURN 0; END; 'LANGUAGE 'plpgsql' Mark On Saturday 10 February 2001 03:29, Tom Lane wrote: > writes: > > Are recursive functions allowed in in 7.0.3 > > Sure. > > play=> create function myfactorial(int) returns int as ' > play'> begin > play'> if $1 > 1 then > play'> return $1 * myfactorial($1 - 1); > play'> end if; > play'> return $1; > play'> end;' language 'plpgsql'; > CREATE > play=> select myfactorial(1); > myfactorial > ------------- > 1 > (1 row) > > play=> select myfactorial(10); > myfactorial > ------------- > 3628800 > (1 row) > > play=> > > I get a stack overflow crash at about myfactorial(7500), but that seems > like a sufficient level of recursion depth for normal purposes ... > > > as I seem to be unable to > > get them to work in plpgsql, > > Are you sure you aren't asking for infinite recursion, eg by invoking > the same function with the same argument? > > regards, tom lane