Thread: Recursive function

Recursive function

From
Juan Daniel Santana Rodés
Date:
Hi everyone...
I have a problem. I am programming a recursive function in plpgsql
language. This function use a cursor and when the function try to call
the same function throw a exception that it say me that the cursor is using.
My friends how I can to resolve this problem.
Regards!!


Re: Recursive function

From
David Johnston
Date:
Juan Daniel Santana Rodés wrote
> Hi everyone...
> I have a problem. I am programming a recursive function in plpgsql
> language. This function use a cursor and when the function try to call
> the same function throw a exception that it say me that the cursor is
> using.
> My friends how I can to resolve this problem.
> Regards!!

You probably will have to avoid solving your problem in this way and try
something different.

If you share the problem and your failed solution attempt others probably
will chime in and provide suggestions.

It is possible that a "refcursor" will work, one where you simply call FETCH
and never attempt to re-open it, will meet your needs though.  I am not that
experienced in that particular programming paradigm though.

David J.





--
View this message in context: http://postgresql.1045698.n5.nabble.com/Recursive-function-tp5779882p5779886.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: Recursive function

From
Alban Hertroys
Date:
On 22 Nov 2013, at 21:56, Juan Daniel Santana Rodés <jdsantana@estudiantes.uci.cu> wrote:

> Hi everyone...
> I have a problem. I am programming a recursive function in plpgsql language. This function use a cursor and when the
functiontry to call the same function throw a exception that it say me that the cursor is using. 
> My friends how I can to resolve this problem.

Getting that error means that you are effectively trying to run multiple queries simultaneously in the same session. I
don’tthink that’s possible. 

You didn’t tell what you’re trying to achieve, but it sounds to me that you either:
1. forgot to close the cursor for a short (non-recursive) lookup query before recursing to the same/next function,
2. or that you are actually trying to perform  a recursive query.

In case 1 the solution is simple; close the cursor before recursing.
For case 2, you may be able to rewrite your function to not recurse, but instead to use the results of a recursive CTE
toachieve recursion. 

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.