Fun with Cursors- how to rewind a cursor - Mailing list pgsql-general

From Postgres User
Subject Fun with Cursors- how to rewind a cursor
Date
Msg-id b88c3460703011629o1dacf88bt670f42e5184be70e@mail.gmail.com
Whole thread Raw
Responses Re: Fun with Cursors- how to rewind a cursor  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hi,
I'm opening a refcursor in Postgres to return a dataset to the client.
 However, before returning the cursor, I'd like to iterate thru the
rows.  Here's the code:

  DECLARE
     ref_entry  refcursor;
     rec  record;
     i  integer = 0;
     v_list varchar = '';

  BEGIN
     OPEN ref_entry FOR
     SELECT * FROM big_select_statement;

     LOOP
       FETCH ref_entry INTO rec;
       EXIT WHEN NOT FOUND;

       i = i + 1;
       IF v_list != '' THEN
          v_list = v_list || ', ';
       END IF;
       v_list = v_list || rec.entry_id::varchar;

     END LOOP;

     Return next ref_entry;

  END;

There's one slight and obvious problem- the cursor returns nothing to
the client because I've already fetched all the rows.  (Remove the
LOOP, and the cursor returns all rows as expected).

Is there any way to 'rewind' the cursor to the first row?  I realize
that I can simply execute the full query and open another cursor to
return to the client, but I'll take a performance hit that I'd like to
avoid.

I've tried a few FETCH BACKWARD and other statements but only get
syntax errors returned by the comiler.  Anyone done this before?

pgsql-general by date:

Previous
From: Jorge Godoy
Date:
Subject: Re: rpm containing pgdump
Next
From: Demian Lessa
Date:
Subject: Constraint validation