Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKPaU-00077y-0f for pgsql-general@arkaria.postgresql.org; Wed, 07 Nov 2018 15:17:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gKPaS-0001xH-9Z for pgsql-general@arkaria.postgresql.org; Wed, 07 Nov 2018 15:17:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKPaR-0001xA-UQ for pgsql-general@lists.postgresql.org; Wed, 07 Nov 2018 15:17:44 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKPaO-0003Zh-Q4 for pgsql-general@postgresql.org; Wed, 07 Nov 2018 15:17:42 +0000 Received: from [192.168.127.1] (port=51258 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gKPaJ-00035U-EE; Wed, 07 Nov 2018 15:17:35 +0000 Received: from [127.0.0.1] (port=60865 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gKPaI-000Nzm-Hq; Wed, 07 Nov 2018 15:17:34 +0000 From: Andrew Gierth To: David Gauthier Cc: Tom Lane , scrawford@pinpointresearch.com, pgsql-general@postgresql.org Subject: Re: recursion in plpgsql In-Reply-To: (David Gauthier's message of "Wed, 7 Nov 2018 09:35:30 -0500") Message-ID: <87muqlyltd.fsf@news-spur.riddles.org.uk> References: <17949.1541550596@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Wed, 07 Nov 2018 15:17:33 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "David" == David Gauthier 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 "" 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)