Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: See <a href="http://www.sqlines.com/postgresql/how-to/return_result_set_from_stored_procedure">this excellent tutorial</a> for an explanation of how to fetch and manage refcursors.</p> <hr> <p>Since node-postgres isn't recognising the refcursors you're returning as result set handles, it seems likely that it doesn't support multiple result sets from PostgreSQL. That's fair enough as PostgreSQL doesn't really support multiple result sets either, they're just emulated with refcursors.</p> <p>You can <code>FETCH</code> from a <code>refcursor</code> via SQL-level cursor commands <a href="http://www.postgresql.org/docs/9.1/static/sql-fetch.html">SQL-level cursor commands</a>, though the documentation for it is miserable. You don't need to use <a href="http://www.postgresql.org/docs/current/static/plpgsql-cursors.html">PL/PgSQL</a> cursor handling to do it. Just:</p> <pre><code>FETCH ALL FROM "&lt;unnamed portal 1&gt;"; </code></pre> <p>Note the double quotes, which are important. Subtitute the refcursor name returned from your function for <code>&lt;unnamed portal 1&gt;</code>.</p> <p>Note also that the transaction that created the refcursor must still be open unless the cursor was created <code>WITH HOLD</code>. Non-<code>HOLD</code> cursors are closed when the transaction commits or rolls back.</p> <p>For example, given the dummy refcursor-returning function:</p> <pre><code>CREATE OR REPLACE FUNCTION dummy_cursor_returning_fn() RETURNS SETOF refcursor AS $$ DECLARE curs1 refcursor; curs2 refcursor; BEGIN OPEN curs1 FOR SELECT generate_series(1,4); OPEN curs2 FOR SELECT generate_series(5,8); RETURN NEXT curs1; RETURN NEXT curs2; RETURN; END; $$ LANGUAGE 'plpgsql'; </code></pre> <p>... which returns a set of cursors, you can get the results by passing the portal names to <code>FETCH</code>, eg:</p> <pre><code>regress=# BEGIN; BEGIN regress=# SELECT dummy_cursor_returning_fn(); dummy_cursor_returning_fn --------------------------- &lt;unnamed portal 7&gt; &lt;unnamed portal 8&gt; (2 rows) regress=# FETCH ALL FROM "&lt;unnamed portal 7&gt;"; generate_series ----------------- 1 2 3 4 (4 rows) regress=# FETCH ALL FROM "&lt;unnamed portal 8&gt;"; generate_series ----------------- 5 6 7 8 (4 rows) regress=# </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload