34000
PostgreSQLERRORNotableInvalid Cursor NameHIGH confidence

invalid cursor name

What this means

SQLSTATE 34000 is raised when a cursor name referenced in a FETCH, CLOSE, or MOVE statement does not correspond to any currently open cursor in the session.

Why it happens
  1. 1Referencing a cursor name that was never declared or was already closed
  2. 2Typo in the cursor name in FETCH or CLOSE
  3. 3Cursor declared in a different transaction that has since ended
How to reproduce

CLOSE on a cursor that does not exist.

trigger — this will error
trigger — this will error
CLOSE nonexistent_cursor;

expected output

ERROR:  cursor "nonexistent_cursor" does not exist

Fix 1

Declare and open the cursor before referencing it

WHEN When explicit cursor management is needed.

Declare and open the cursor before referencing it
DECLARE my_cursor CURSOR FOR SELECT * FROM orders;
FETCH 10 FROM my_cursor;
CLOSE my_cursor;

Why this works

Cursors must be declared and opened in the current transaction before they can be fetched from or closed.

Fix 2

Check pg_cursors to list open cursors

WHEN When debugging cursor name issues.

Check pg_cursors to list open cursors
SELECT name FROM pg_cursors;

Why this works

pg_cursors shows all open cursors in the current session, confirming which names are valid.

Sources
Official documentation ↗

Class 34 — Invalid Cursor Name

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors