The following documentation describes how to see the refcursor returned from a function, here, like this:
CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS '
BEGIN
OPEN $1 FOR SELECT col FROM test;
RETURN $1;
END;
' LANGUAGE plpgsql;
BEGIN;
SELECT reffunc('funccursor');
FETCH ALL IN funccursor;
COMMIT;
This works for me. However, if I want to keep the results on my screen, I have to keep the transaction open. When I execute COMMIT, my result set is discarded. When I execute both FETCH and COMMIT at the same time, the first result set is discarded.
Is there a way to commit the transaction but keep the result set? The version of PgAdmin is 1.18.1.
Best Answer
When a cursor is defined at the SQL level with DECLARE, there is an option
WITH HOLD
that makes it continue to exist after commiting the current transaction. Quoting the doc:On the other hand, a refcursor opened by a plpgsql function is closed at the end of the transaction. Quoting the plpgsql doc:
To create a cursor in a plpgsql function that may be used outside of its "parent" transaction, it's just a matter of syntax. You want the SQL implementation of a cursor, not the plpgsql variant. For this,
EXECUTE
must be used.As an example, here's the skeleton of a function similar to yours, but using SQL-level cursors that outlive the transaction:
Demo: