Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:54:32+00:00 2026-06-10T04:54:32+00:00

I have a PostgresQL function that returns multiple resultsets. I can extract these resultsets

  • 0

I have a PostgresQL function that returns multiple resultsets. I can extract these resultsets in .net without a problem (so I know my function works correctly), but I am having trouble doing so with node-postgres.

The result object returns an array of 7 items which matches the number of datasets returned.

In Node, the each of the 7 rows simply contains a string of <unnamed portal 1>.

connection.query("BEGIN");
connection.query({text: "SELECT getoperationaldatasetmodel($1)", values : [clientid]}, function(err, results) {


  if (err) {
    connection.query("COMMIT");
    self.pool.release(connection);
    callback(err);
  }
  else {
    var opsDataset = null;
    var rows = results.rows;
    // this returns 7 rows but the rows do not contain data but rather the name of the dataset.
  }

So: does node-postgres support multiple result sets and if yes, any suggestions on how to extract?

EDIT: Here is the code I used with node-postgres should someone else need to use it in the future.

// must wrap in a transaction otherwise won't be able to see the multiple sets.
connection.query("BEGIN");
connection.query({text: "SELECT myfunction($1)", values : [clientid]}, function(err, results) {

  if (err) {

     // handle error here
     connection.query("COMMIT;");
  }
  else {

    connection.query('FETCH ALL FROM "<unnamed portal 1>"',  function(err, r1) {
        // r1.rows will contain the data for the first refcursor
    });
    connection.query('FETCH ALL FROM "<unnamed portal 2>"',  function(err, r2) {
        // r2.rows will contain the data for the second refcursor
    });

    // remember to handle the closure of the transaction

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:54:34+00:00Added an answer on June 10, 2026 at 4:54 am

    UPDATE: See this excellent tutorial for an explanation of how to fetch and manage refcursors.


    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.

    You can FETCH from a refcursor via SQL-level cursor commands SQL-level cursor commands, though the documentation for it is miserable. You don’t need to use PL/PgSQL cursor handling to do it. Just:

    FETCH ALL FROM "<unnamed portal 1>";
    

    Note the double quotes, which are important. Subtitute the refcursor name returned from your function for <unnamed portal 1>.

    Note also that the transaction that created the refcursor must still be open unless the cursor was created WITH HOLD. Non-HOLD cursors are closed when the transaction commits or rolls back.

    For example, given the dummy refcursor-returning function:

    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';
    

    … which returns a set of cursors, you can get the results by passing the portal names to FETCH, eg:

    regress=# BEGIN;
    BEGIN
    regress=# SELECT dummy_cursor_returning_fn();
     dummy_cursor_returning_fn 
    ---------------------------
     <unnamed portal 7>
     <unnamed portal 8>
    (2 rows)
    
    regress=# FETCH ALL FROM "<unnamed portal 7>";
     generate_series 
    -----------------
                   1
                   2
                   3
                   4
    (4 rows)
    
    regress=# FETCH ALL FROM "<unnamed portal 8>";
     generate_series 
    -----------------
                   5
                   6
                   7
                   8
    (4 rows)
    
    regress=# 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function in PostgreSQL (PLPGSQL) that returns an array containing two elements.
I have this function in PostgreSQL, but I don't know how to return the
I have an PostgreSQL Function which returns me an XML where the whole infos
I am using Postgresql 8.3 and have the following simple function that will return
I have this Trigger in Postgresql that I can't just get to work (does
I have a Postgresql function which returns a composite type defined as (location TEXT,
How can I execute a function named Test1 that is stored in PostgreSQL from
I have postgresql (in perlu) function getTravelTime(integer, timestamp), which tries to select data for
I have a postgreSQL Server with some databases. Every user can only connect to
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.