Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfiio-0003Cu-0Q for pgsql-general@arkaria.postgresql.org; Thu, 19 Nov 2020 12:07:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kfiiH-0002TY-EU for pgsql-general@arkaria.postgresql.org; Thu, 19 Nov 2020 12:06:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfiiH-0002TR-6h for pgsql-general@lists.postgresql.org; Thu, 19 Nov 2020 12:06:57 +0000 Received: from mail.verite.pro ([185.16.44.216]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kfiiE-0000RU-Ma for pgsql-general@lists.postgresql.org; Thu, 19 Nov 2020 12:06:56 +0000 Received: from genius-map.net (verite.pro [195.154.189.246]) by mail.verite.pro (Postfix) with ESMTP id B70AB8C0209; Thu, 19 Nov 2020 12:56:56 +0100 (CET) Received: by genius-map.net (Postfix, from userid 1001) id 1DD0A38A07E0; Thu, 19 Nov 2020 13:06:51 +0100 (CET) Content-Type: text/plain; charset="iso-8859-15" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: Multiple result set to be returned in procedure/function From: "Daniel Verite" To: "Thomas Kellerer" Cc: pgsql-general@lists.postgresql.org In-Reply-To: Date: Thu, 19 Nov 2020 13:06:50 +0100 Message-Id: X-Mailer: Manitou v1.7.3 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thomas Kellerer wrote: > arthur=3D> \set AUTOCOMMIT off Alternatively, start an explicit transaction block with BEGIN. The point is that the lifespan of the cursor is the transaction block in which it's instantiated. =20 > arthur=3D> select * from get_results(); > get_results > -------------------- > > Friendlier names may be used by assigning them in the function, i.e. plpgsql does support: declare c1 refcursor :=3D 'mycursorname'; Then the caller might simply hardcode the cursor names in the FETCH statements rather than building them dynamically at runtime. Also it allows to put the server-side code into an anymous DO block instead of creating a function, because it doesn't have to return any refcursor when the caller already knows the cursor names. BEGIN; DO $$ declare c refcursor :=3D 'c_pgclass'; begin open c for select relname from pg_class; end $$ LANGUAGE plpgsql; FETCH ALL from c_pgclass; ... COMMIT; This form might be closer to how inline blocks of code are written with some other db engines, when they produce resultsets=20 without an explicit cursor interface. Best regards, --=20 Daniel V=E9rit=E9 PostgreSQL-powered mailer: https://www.manitou-mail.org Twitter: @DanielVerite