pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Daniel Verite <daniel@manitou-mail.org>
To: Thomas Kellerer <shammat@gmx.net>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Multiple result set to be returned in procedure/function
Date: Thu, 19 Nov 2020 13:06:50 +0100
Message-ID: <b1ba389d-d266-45f5-a7b7-e4a6ababa2ba@manitou-mail.org> (raw)
In-Reply-To: <e1ec41df-2689-7cc9-da85-6bb79f5d21b4@gmx.net>

	Thomas Kellerer wrote:

>    arthur=> \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.
 
>    arthur=> select * from get_results();
>        get_results
>    --------------------
>     <unnamed portal 1>
>     <unnamed portal 2>

Friendlier names may be used by assigning them in the function,
i.e. plpgsql does support:

 declare
  c1 refcursor := '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 := '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 
without an explicit cursor interface.


Best regards,
-- 
Daniel Vérité
PostgreSQL-powered mailer: https://www.manitou-mail.org
Twitter: @DanielVerite





view thread (4+ messages)  latest in thread

Message-ID: <b1ba389d-d266-45f5-a7b7-e4a6ababa2ba@manitou-mail.org>
Permalink:  ../b1ba389d-d266-45f5-a7b7-e4a6ababa2ba@manitou-mail.org/
Also on:    postgresql.org/message-id/b1ba389d-d266-45f5-a7b7-e4a6ababa2ba@manitou-mail.org

 · 

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: daniel@manitou-mail.org, shammat@gmx.net
  Subject: Re: Multiple result set to be returned in procedure/function
  In-Reply-To: <b1ba389d-d266-45f5-a7b7-e4a6ababa2ba@manitou-mail.org>
  References: <e1ec41df-2689-7cc9-da85-6bb79f5d21b4@gmx.net> <b1ba389d-d266-45f5-a7b7-e4a6ababa2ba@manitou-mail.org>

* 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