PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 34000: cursor does not exist

The cursor you FETCH from is not open in this session: it was closed at COMMIT, never opened under that name, or closed when the transaction ended.

PostgreSQL 17.12
34000: cursor "emp_cur" does not exist

Jump to a cause

  1. 01FETCH after COMMIT on a cursor declared without WITH HOLD
  2. 02FETCH uses a different name than DECLARE
  3. 03A function returns a refcursor, but FETCH runs outside a transaction

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
Setuprun as lab_a
CREATE TABLE emp (id int PRIMARY KEY, name text);
INSERT INTO emp VALUES (1, 'Kim'), (2, 'Lee'), (3, 'Park');
OUTPUT
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
FETCH 1 FROM emp_cur;
COMMIT;
FETCH 1 FROM emp_cur;
✕ ERROR
BEGIN
DECLARE CURSOR
 id | name
----+------
  1 | Kim
(1 row)

COMMIT
ERROR:  34000: cursor "emp_cur" does not exist
Checkrun as lab_a
BEGIN;
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;
OUTPUT
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.

After the fixrun as lab_a
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;
✓ SUCCESS
BEGIN
DECLARE CURSOR
 id | name
----+------
  1 | Kim
(1 row)

COMMIT
 id | name
----+------
  2 | Lee
(1 row)

CLOSE CURSOR

02FETCH 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
Setuprun as lab_a
CREATE TABLE emp (id int PRIMARY KEY, name text);
INSERT INTO emp VALUES (1, 'Kim'), (2, 'Lee'), (3, 'Park');
OUTPUT
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
FETCH 2 FROM emp_cursor;
ROLLBACK;
✕ ERROR
BEGIN
DECLARE CURSOR
ERROR:  34000: cursor "emp_cursor" does not exist
ROLLBACK
Checkrun as lab_a
BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
SELECT name, statement FROM pg_cursors;
ROLLBACK;
OUTPUT
BEGIN
DECLARE CURSOR
  name   |                            statement
---------+------------------------------------------------------------------
 emp_cur | DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
(1 row)

ROLLBACK

The name in the error is the one you asked for; compare it with pg_cursors.name.

After the fixrun as lab_a
BEGIN;
DECLARE emp_cur CURSOR FOR SELECT id, name FROM emp ORDER BY id;
FETCH 2 FROM emp_cur;
COMMIT;
✓ SUCCESS
BEGIN
DECLARE CURSOR
 id | name
----+------
  1 | Kim
  2 | Lee
(2 rows)

COMMIT

03A 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
Setuprun as lab_a
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;
$$;
OUTPUT
CREATE TABLE
INSERT 0 3
CREATE FUNCTION
Reproducerun as lab_a
SELECT open_emp('emp_cur');
FETCH ALL FROM emp_cur;
✕ ERROR
 open_emp
----------
 emp_cur
(1 row)

ERROR:  34000: cursor "emp_cur" does not exist
Checkrun as lab_a
BEGIN;
SELECT open_emp('emp_cur');
SELECT name FROM pg_cursors;
COMMIT;
SELECT count(*) AS open_cursors FROM pg_cursors;
OUTPUT
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.

After the fixrun as lab_a
BEGIN;
SELECT open_emp('emp_cur');
FETCH ALL FROM emp_cur;
COMMIT;
✓ SUCCESS
BEGIN
 open_emp
----------
 emp_cur
(1 row)

 id | name
----+------
  1 | Kim
  2 | Lee
  3 | Park
(3 rows)

COMMIT

FAQ

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