pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: recursion in plpgsql
Date: Tue, 06 Nov 2018 19:29:56 -0500
Message-ID: <17949.1541550596@sss.pgh.pa.us> (raw)
In-Reply-To: <CAMBRECDVhafLr-iY8i53jaRq3bsfcx3weEOKocYfWEckpvuVFg@mail.gmail.com>
References: <CAMBRECDVhafLr-iY8i53jaRq3bsfcx3weEOKocYfWEckpvuVFg@mail.gmail.com>

David Gauthier <davegauthierpg@gmail.com> writes:
> I'm trying/failing to write a recursive plpgsql function where the function
> tries to operate on a hierary of records in a reflexive table.
> parent-child-grandchild type of recursion.
> I tried with a cursor, but got a "cursor already in use" error.  So that
> looks like scoping.

IIRC, the "portal" underlying a plpgsql cursor just gets the same name
as the cursor variable by default, so you'll get portal-name conflicts
with the coding style you show here.

It's possible to avoid that by ensuring that each cursor gets a different
portal name.  I'm too lazy to check the details right now, but at the
very least there's a way to do it by declaring the variable as "refcursor"
and assigning it a different name at each nesting depth.  There might be
some more elegant solution, too.

			regards, tom lane




view thread (8+ messages)  latest in thread

Message-ID: <17949.1541550596@sss.pgh.pa.us>
Permalink:  ../17949.1541550596@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/17949.1541550596@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, davegauthierpg@gmail.com
  Subject: Re: recursion in plpgsql
  In-Reply-To: <17949.1541550596@sss.pgh.pa.us>
  References: <CAMBRECDVhafLr-iY8i53jaRq3bsfcx3weEOKocYfWEckpvuVFg@mail.gmail.com> <17949.1541550596@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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