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 1gKBjU-00089e-56 for pgsql-general@arkaria.postgresql.org; Wed, 07 Nov 2018 00:30:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gKBjS-0001a7-Kb for pgsql-general@arkaria.postgresql.org; Wed, 07 Nov 2018 00:30:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKBjS-0001a0-Bu for pgsql-general@lists.postgresql.org; Wed, 07 Nov 2018 00:30:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKBjM-0005hO-0l for pgsql-general@postgresql.org; Wed, 07 Nov 2018 00:30:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id wA70Tugv017950; Tue, 6 Nov 2018 19:29:56 -0500 From: Tom Lane To: David Gauthier cc: pgsql-general@postgresql.org Subject: Re: recursion in plpgsql In-reply-to: References: Comments: In-reply-to David Gauthier message dated "Tue, 06 Nov 2018 17:54:02 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17948.1541550596.1@sss.pgh.pa.us> Date: Tue, 06 Nov 2018 19:29:56 -0500 Message-ID: <17949.1541550596@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Gauthier 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