Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 9606EB5DBFA for ; Thu, 18 Aug 2011 13:15:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 62482-06 for ; Thu, 18 Aug 2011 16:15:37 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id E0119B5DBF9 for ; Thu, 18 Aug 2011 13:15:36 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p7IGFXuQ005484; Thu, 18 Aug 2011 12:15:33 -0400 (EDT) To: Pavel Stehule cc: "Kidd, David M" , "pgsql-sql@postgresql.org" Subject: Re: Cursor names in a self-nested function In-reply-to: References: <92F7EFF74482D04B8548581D147F6AE70403DF@icexch-m3.ic.ac.uk> Comments: In-reply-to Pavel Stehule message dated "Thu, 18 Aug 2011 17:36:07 +0200" Date: Thu, 18 Aug 2011 12:15:33 -0400 Message-ID: <5483.1313684133@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.037 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.137 X-Spam-Level: X-Archive-Number: 201108/74 X-Sequence-Number: 35742 Pavel Stehule writes: > you can use a refcursor type > http://developer.postgresql.org/pgdocs/postgres/plpgsql-cursors.html It would suffice to explicitly set mycursor to null before doing the OPEN, thus instructing the system to assign a unique cursor name. CREATE FUNCTION test(id integer) RETURNS TEXT AS $BODY$ DECLARE mycursor CURSOR FOR SELECT * FROM int4_tbl WHERE f1 > id; newid INTEGER; out TEXT; BEGIN out := id::text || ' '; mycursor := null; OPEN mycursor; raise notice 'mycursor = %', mycursor; -- debug LOOP FETCH mycursor INTO newid; EXIT WHEN newid IS NULL; out := out || test (newid); END LOOP; RETURN out; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; regards, tom lane