Diagnostic Queries
Symptoms
A FETCH, MOVE, or CLOSE referenced a cursor name that is not defined in the session. PostgreSQL raises SQLSTATE 34000 (invalid_cursor_name).
- The named cursor was never declared or was already closed.
- Common after a transaction ended and closed the cursor.
- Non-holdable cursors close at commit.
What the server log shows
ERROR: cursor "mycursor" does not exist
Why PostgreSQL raises this — what the manual says
the FETCH reference (Description):
“FETCH retrieves rows using a previously-created cursor.”
Cursors live within a session/transaction. Referencing a name that was never declared, was already closed, or expired when its transaction ended cannot be resolved, so PostgreSQL reports 34000.
Common causes
- A typo in the cursor name.
- The cursor was already closed (or the transaction committed, closing a non-holdable cursor).
- Declaring the cursor in a different transaction.
How to fix it
- Declare the cursor with
DECLAREbefore fetching, and match the name exactly. - Keep the transaction open while using the cursor, or declare it
WITH HOLD. - Verify the cursor wasn’t already closed earlier in the session.
Related & next steps
Reference: PostgreSQL 18 — FETCH.
Thanks — noted. This helps keep the database accurate.