473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Recursive PLPGSQL function?

Hi everyone,

I'm trying to write a recursive plpgsql function in PostgreSQL 7.4.2
that given a tree node id (ictid) will return all the nodes below it in
the tree, one row per node. When I try and execute the function I get
the following error message:

CONTEXT: PL/pgSQL function "findsubcategories" line 15 at for over
select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
....repeated many many times...
Can anyone see where I am going wrong in my function? I found a
reference to "for over select rows" in pl_funcs.c but it appears to be
denoting a statement type? The code is given below:
CREATE OR REPLACE FUNCTION plpgsql.findsubcategories(int8) RETURNS SETOF
inventory.cattree AS '
DECLARE
_row inventory.cattree%ROWTYPE;
_nrow inventory.cattree%ROWTYPE;
_id ALIAS FOR $1;

BEGIN
-- Select the starting tree entry
FOR _row IN SELECT * FROM inventory.cattree WHERE parentictid =
_id LOOP

-- Return this category
RETURN NEXT _row;

-- Recurse for each child function
FOR _nrow IN SELECT * FROM
plpgsql.findsubcategories(_row.parentictid) LOOP
RETURN NEXT _nrow;
END LOOP;

END LOOP;

-- Return the entire set
RETURN;
END
' LANGUAGE 'plpgsql';
If this is not possible, can anyone else suggest a way of getting the
required result?
Many thanks,

Mark.

---

Mark Cave-Ayland
Webbased Ltd.
Tamar Science Park
Derriford
Plymouth
PL6 8BX
England

Tel: +44 (0)1752 764445
Fax: +44 (0)1752 764446
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender. You
should not copy it or use it for any purpose nor disclose or distribute
its contents to any other person.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #1
2 3795
"Mark Cave-Ayland" <m.***********@webbased.co.uk> writes:
I'm trying to write a recursive plpgsql function in PostgreSQL 7.4.2
that given a tree node id (ictid) will return all the nodes below it in
the tree, one row per node. When I try and execute the function I get
the following error message:


You've got an infinite recursion there --- the function is calling
itself back with the original argument value.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2
If I'm not mistaken you have an infinit recursion because you are always
pulling the same id (whatever _id starts at) throughout each function call.

Postgres is most likely killing the functions when it's hits some stack
or memory limit.

Mark Cave-Ayland wrote:
Hi everyone,

I'm trying to write a recursive plpgsql function in PostgreSQL 7.4.2
that given a tree node id (ictid) will return all the nodes below it in
the tree, one row per node. When I try and execute the function I get
the following error message:

CONTEXT: PL/pgSQL function "findsubcategories" line 15 at for over
select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
PL/pgSQL function "findsubcategories" line 15 at for over select rows
...repeated many many times...
Can anyone see where I am going wrong in my function? I found a
reference to "for over select rows" in pl_funcs.c but it appears to be
denoting a statement type? The code is given below:
CREATE OR REPLACE FUNCTION plpgsql.findsubcategories(int8) RETURNS SETOF
inventory.cattree AS '
DECLARE
_row inventory.cattree%ROWTYPE;
_nrow inventory.cattree%ROWTYPE;
_id ALIAS FOR $1;

BEGIN
-- Select the starting tree entry
FOR _row IN SELECT * FROM inventory.cattree WHERE parentictid =
_id LOOP

-- Return this category
RETURN NEXT _row;

-- Recurse for each child function
FOR _nrow IN SELECT * FROM
plpgsql.findsubcategories(_row.parentictid) LOOP
RETURN NEXT _nrow;
END LOOP;

END LOOP;

-- Return the entire set
RETURN;
END
' LANGUAGE 'plpgsql';
If this is not possible, can anyone else suggest a way of getting the
required result?
Many thanks,

Mark.

---

Mark Cave-Ayland
Webbased Ltd.
Tamar Science Park
Derriford
Plymouth
PL6 8BX
England

Tel: +44 (0)1752 764445
Fax: +44 (0)1752 764446
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender. You
should not copy it or use it for any purpose nor disclose or distribute
its contents to any other person.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Petr Bravenec | last post by:
I have found that when I use the RETURN NEXT command in recursive function, not all records are returned. The only records I can obtain from function are records from the highest level of...
6
by: Martin Marques | last post by:
We are trying to make some things work with plpgsql. The problem is that I built several functions that call one another, and I thought that the way of calling it was just making the assign: ...
1
by: Rajesh Kumar Mallah | last post by:
Hi, profile_row profile_master%ROWTYPE; in a plpgsql function gives the error below tradein_clients=# SELECT general.create_accounts(); WARNING: plpgsql: ERROR during compile of...
10
by: lnd | last post by:
After copied pg database from one PC to another -I could not find plpgsql function(s) in the copied database. -had to instal plpgsql language handler again -whilst tables and data moved...
14
by: Karl O. Pinc | last post by:
Hi, Thought perhaps some other eyes than mine can tell if I'm doing something wrong here or if there's a bug somewhere. I've never passed a ROWTYPE varaible to a function but I don't see where...
1
by: Thomas Schoen | last post by:
Hi, is it possible to use a parameter of a plpgsql-function to order a selection inside the function? What i would like to do is pass a column-name/alias-name to a plpgsql function and use...
2
by: David Boone | last post by:
I've been trying to create functions with postgres, but it seems that queries run within a function take wayyy too long to complete. The increased time seems to be in the actual queries, not...
9
by: Karl O. Pinc | last post by:
I want to return multiple values, but not a set, only a single row, from a plpgsql function and I can't seem to get it to work. (I suppose I'd be happy to return a set, but I can't seem to make...
1
by: Karl O. Pinc | last post by:
FYI, mostly. But I do have questions as to how to write code that will continue to work in subsequent postgresql versions. See code below. begintest() uses EXIT to exit a BEGIN block from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.