PG
PRO
34000ERRORTier 2 — Caution✅ HIGH confidence

invalid cursor name

Category: Invalid Cursor NameVersions: All Postgres versions

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
CLOSE nonexistent_cursor;
ERROR: cursor "nonexistent_cursor" does not exist

Fix 1: Declare and open the cursor before referencing it

When explicit cursor management is needed.

fix
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 debugging cursor name issues.

fix
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 docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 34 — Invalid Cursor Name

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE. Stable across all Postgres versions.

See also

📄 Reference pages

DECLARE CURSORFETCHCLOSEpg_cursors
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →