pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedRecusrive Functions in 7.0.3
5+ messages / 3 participants
[nested] [flat]
* Recusrive Functions in 7.0.3
@ 2001-02-09 20:22 plpgsql@polar-digital.com
0 siblings, 1 reply; 5+ messages in thread
From: plpgsql@polar-digital.com @ 2001-02-09 20:22 UTC (permalink / raw)
To: pgsql-sql
Are recursive functions allowed in in 7.0.3 as I seem to be unable to get them to work in plpgsql, it just freezes and I have to do ctrl-alt-c.
ie calling the same function name from within some form of loop, possible to 4 or 5 levels?
Also is it possible to output any debugging info, all I really want to be able to do is print out a variable to the screen.
Final quesion for pure speed on functions does PL/PGSQL have special compilation options or would I get better performace for PL/C?
Regards
Mark
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Recusrive Functions in 7.0.3
@ 2001-02-10 03:29 Tom Lane <tgl@sss.pgh.pa.us>
parent: plpgsql@polar-digital.com
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2001-02-10 03:29 UTC (permalink / raw)
To: m.proctor@bigfoot.com; +Cc: pgsql-sql
<plpgsql@polar-digital.com> 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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Recusrive Functions in 7.0.3
@ 2001-02-10 08:42 Tom Lane <tgl@sss.pgh.pa.us>
parent: mark proctor <m.proctor@bigfoot.com>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2001-02-10 08:42 UTC (permalink / raw)
To: m.proctor@bigfoot.com; +Cc: pgsql-sql
mark proctor <m.proctor@bigfoot.com> writes:
> 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.
Hm. There may be a bug here, or maybe you're still confused... but I'm
not eager to reverse-engineer your table declarations and data from this
sketch. Would you mind providing a complete example, ie a SQL script to
reproduce the problem starting from an empty database?
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Recusrive Functions in 7.0.3
@ 2001-02-10 08:43 mark proctor <m.proctor@bigfoot.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: mark proctor @ 2001-02-10 08:43 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql
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:
> <plpgsql@polar-digital.com> 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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Recusrive Functions in 7.0.3
@ 2001-02-10 11:10 mark proctor <m.proctor@bigfoot.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 5+ messages in thread
From: mark proctor @ 2001-02-10 11:10 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql
Ahh I found what I was doing wrong, there was a rogue value being returned causing to infinite loop. Its fixed now.
Creating that script you recommended set my thinking process straight.
many thanks
Mark
On Saturday 10 February 2001 08:42, Tom Lane wrote:
> mark proctor <m.proctor@bigfoot.com> writes:
> > 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.
>
> Hm. There may be a bug here, or maybe you're still confused... but I'm
> not eager to reverse-engineer your table declarations and data from this
> sketch. Would you mind providing a complete example, ie a SQL script to
> reproduce the problem starting from an empty database?
>
> regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2001-02-10 11:10 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-02-09 20:22 Recusrive Functions in 7.0.3 plpgsql@polar-digital.com
2001-02-10 03:29 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-10 08:43 ` mark proctor <m.proctor@bigfoot.com>
2001-02-10 08:42 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-02-10 11:10 ` mark proctor <m.proctor@bigfoot.com>
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