pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: scrawford@pinpointresearch.com
Cc: pgsql-general@postgresql.org
Subject: Re: recursion in plpgsql
Date: Wed, 07 Nov 2018 15:17:33 +0000
Message-ID: <87muqlyltd.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAMBRECD2w_+2rm87bYTLWQqyhmmOo2w7gVBU5Rk8R-_T4+ryrw@mail.gmail.com>
References: <CAMBRECDVhafLr-iY8i53jaRq3bsfcx3weEOKocYfWEckpvuVFg@mail.gmail.com>
	<17949.1541550596@sss.pgh.pa.us>
	<CAMBRECD2w_+2rm87bYTLWQqyhmmOo2w7gVBU5Rk8R-_T4+ryrw@mail.gmail.com>

>>>>> "David" == David Gauthier <davegauthierpg@gmail.com> writes:

 David> Tom: I seem to remember (but am probably wrong) that cursors
 David> were locally scoped (or could be made so). This was several
 David> years ago with an earlier v8 version. Was that sort of thing
 David> around back then ?

There are two distinct objects here being called "cursor": one is the
plpgsql variable, which is locally scoped, and the other is the actual
open portal, which must have a unique name within the session.

By default, plpgsql explicit bound cursors (but not plain "refcursor"
variables) take their portal name from the plpgsql variable name, and
hence don't work recursively by default. This is a convenience so that
code outside the function can use the same name to refer to the open
portal.

However, plpgsql cursor variables (whether declared bound or unbound)
can be assigned a text value or NULL _before_ being opened, and if so,
that value will be used for the portal name, or if NULL, a name of
"<unnamed portal N>" will be uniquely generated. (_After_ the open, the
variable's text value is the actually assigned portal name.) Unbound
refcursor variables default to NULL, so they are assigned unique portal
names on opening.

So in your example, adding

    child_node_curr := NULL;

immediately before the OPEN statement should be sufficient.

-- 
Andrew (irc:RhodiumToad)




view thread (8+ messages)  latest in thread

Message-ID: <87muqlyltd.fsf@news-spur.riddles.org.uk>
Permalink:  ../87muqlyltd.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87muqlyltd.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk, davegauthierpg@gmail.com, tgl@sss.pgh.pa.us, scrawford@pinpointresearch.com
  Subject: Re: recursion in plpgsql
  In-Reply-To: <87muqlyltd.fsf@news-spur.riddles.org.uk>
  References: <CAMBRECDVhafLr-iY8i53jaRq3bsfcx3weEOKocYfWEckpvuVFg@mail.gmail.com> <17949.1541550596@sss.pgh.pa.us> <CAMBRECD2w_+2rm87bYTLWQqyhmmOo2w7gVBU5Rk8R-_T4+ryrw@mail.gmail.com> <87muqlyltd.fsf@news-spur.riddles.org.uk>

* 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