Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
SQLSTATE 34000 (invalid_cursor_name) means PostgreSQL could not find a cursor with the name you gave to FETCH, MOVE or CLOSE. A normal cursor lives only inside the transaction that declared it, so it disappears at COMMIT or ROLLBACK.
pg_cursors shows the cursors currently open in your session. Below are the three cases we reproduced on PostgreSQL 17, each checked with pg_cursors and then run again with the fix.
01FETCH after COMMIT on a cursor declared without WITH HOLD
Inside BEGIN, DECLARE emp_cur CURSOR FOR … and the first FETCH 1 returned row 1 (Kim). After COMMIT, the next FETCH 1 FROM emp_cur failed with 34000.
pg_cursors showed why: while the transaction was open, emp_cur had is_holdable = f, and right after COMMIT the number of open cursors was 0.
Declared as CURSOR WITH HOLD, the same steps worked: row 1 before COMMIT, row 2 (Lee) after it, then CLOSE emp_cur.
Setup
CREATE TABLE emp (id int PRIMARY KEY, name text);
INSERT INTO emp VALUES (1, 'Kim'), (2, 'Lee'), (3, 'Park');CREATE TABLE
INSERT 0 3BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
FETCH 1 FROM emp_cur;
COMMIT;
FETCH 1 FROM emp_cur;BEGIN
DECLARE CURSOR
id | name
----+------
1 | Kim
(1 row)
COMMIT
ERROR: 34000: cursor "emp_cur" does not existBEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
SELECT name, is_holdable FROM pg_cursors;
COMMIT;
SELECT count(*) AS open_cursors FROM pg_cursors;BEGIN
DECLARE CURSOR
name | is_holdable
---------+-------------
emp_cur | f
(1 row)
COMMIT
open_cursors
--------------
0
(1 row)A WITH HOLD cursor stays open after COMMIT until you CLOSE it or the session ends, so always close it yourself.
BEGIN;
DECLARE emp_cur CURSOR WITH HOLD FOR SELECT id, name FROM emp ORDER BY id;
FETCH 1 FROM emp_cur;
COMMIT;
FETCH 1 FROM emp_cur;
CLOSE emp_cur;BEGIN
DECLARE CURSOR
id | name
----+------
1 | Kim
(1 row)
COMMIT
id | name
----+------
2 | Lee
(1 row)
CLOSE CURSOR02FETCH uses a different name than DECLARE
The cursor was declared as emp_cur, but the code ran FETCH 2 FROM emp_cursor. The error names the cursor it looked for: cursor "emp_cursor" does not exist.
Querying pg_cursors in the same transaction listed only emp_cur together with its DECLARE statement. With the correct name, FETCH 2 FROM emp_cur returned rows 1 and 2.
Setup
CREATE TABLE emp (id int PRIMARY KEY, name text);
INSERT INTO emp VALUES (1, 'Kim'), (2, 'Lee'), (3, 'Park');CREATE TABLE
INSERT 0 3BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
FETCH 2 FROM emp_cursor;
ROLLBACK;BEGIN
DECLARE CURSOR
ERROR: 34000: cursor "emp_cursor" does not exist
ROLLBACKBEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
SELECT name, statement FROM pg_cursors;
ROLLBACK;BEGIN
DECLARE CURSOR
name | statement
---------+------------------------------------------------------------------
emp_cur | DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
(1 row)
ROLLBACKThe name in the error is the one you asked for; compare it with pg_cursors.name.
BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
FETCH 2 FROM emp_cur;
COMMIT;BEGIN
DECLARE CURSOR
id | name
----+------
1 | Kim
2 | Lee
(2 rows)
COMMIT03A function returns a refcursor, but FETCH runs outside a transaction
The PL/pgSQL function open_emp(c refcursor) opens a cursor and returns its name. Running SELECT open_emp('emp_cur'); in autocommit mode returned emp_cur, but the following FETCH ALL FROM emp_cur failed with 34000. The SELECT was its own transaction, and the cursor was closed when that transaction ended.
pg_cursors confirmed it: inside BEGIN the cursor emp_cur was listed, and after COMMIT there were 0. Calling the function and fetching in the same BEGIN … COMMIT block returned all three rows.
Setup
CREATE TABLE emp (id int PRIMARY KEY, name text);
INSERT INTO emp VALUES (1, 'Kim'), (2, 'Lee'), (3, 'Park');
CREATE FUNCTION open_emp(c refcursor) RETURNS refcursor LANGUAGE plpgsql AS $$
BEGIN
OPEN c FOR SELECT id, name FROM emp ORDER BY id;
RETURN c;
END;
$$;CREATE TABLE
INSERT 0 3
CREATE FUNCTIONSELECT open_emp('emp_cur');
FETCH ALL FROM emp_cur; open_emp
----------
emp_cur
(1 row)
ERROR: 34000: cursor "emp_cur" does not existBEGIN;
SELECT open_emp('emp_cur');
SELECT name FROM pg_cursors;
COMMIT;
SELECT count(*) AS open_cursors FROM pg_cursors;BEGIN
open_emp
----------
emp_cur
(1 row)
name
---------
emp_cur
(1 row)
COMMIT
open_cursors
--------------
0
(1 row)The same applies in application code: if the driver is in autocommit mode, turn it off before calling a function that returns a refcursor.
BEGIN;
SELECT open_emp('emp_cur');
FETCH ALL FROM emp_cur;
COMMIT;BEGIN
open_emp
----------
emp_cur
(1 row)
id | name
----+------
1 | Kim
2 | Lee
3 | Park
(3 rows)
COMMITFAQ
Why does PostgreSQL say cursor does not exist after COMMIT?
A cursor declared without WITH HOLD is closed when its transaction ends. In our test pg_cursors showed is_holdable = f and 0 open cursors after COMMIT. Declare it WITH HOLD, or finish fetching before you commit.
How do I fetch from a refcursor returned by a PL/pgSQL function?
Call the function and run FETCH inside the same transaction: BEGIN; SELECT open_emp('emp_cur'); FETCH ALL FROM emp_cur; COMMIT;. In autocommit mode the cursor is already closed when FETCH runs.
How can I see which cursors are open in PostgreSQL?
Query pg_cursors. It lists the cursors open in your current session with name, statement and is_holdable.
Do I need to close a WITH HOLD cursor?
Yes. It survives COMMIT and stays open until CLOSE or the end of the session. Our fixed example ends with CLOSE emp_cur.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12