PostgreSQL Error 34000: Invalid Cursor Name Fix
In this article
PostgreSQL error 34000 (invalid_cursor_name) means your session tried to use a cursor that does not exist, has already been closed, or was defined in a different transaction or session. The fix is to make sure the cursor is declared and opened in the same session and transaction that fetches from it, and to stop closing or reusing it incorrectly. In practice you either re-declare the cursor before the next FETCH, keep the transaction open for the whole cursor lifetime, or switch to a driver-level result set that does not rely on server-side cursors.
What You'll Need
- Access to the PostgreSQL server and the database where the error occurs (psql, pgAdmin, or your application's connection settings).
- The exact SQL statement or application code that triggers the error, including any BEGIN, DECLARE, FETCH, CLOSE, or COMMIT calls around it.
- Permission to read the server log (postgresql.conf sets log_min_error_statement and log_min_messages; your DBA or hosting panel can expose these).
- A test database or a transaction you can roll back, so you can experiment without affecting production data.
- Your driver and version (for example psql, JDBC, Npgsql, psycopg, node-postgres), because cursor handling differs between them.
Step-by-Step
- Reproduce the error in psql first. Open a terminal and run
psql -h your-host -U your-user -d your-db. Paste the exact sequence of statements your application runs, one at a time. Seeing which statement returnsERROR: cursor "..." does not existtells you whether the problem is in your SQL or in the driver's transaction handling. Note that psql runs each statement in its own implicit transaction unless you start one explicitly, so a cursor declared outside a transaction block will not survive the next statement. - Check that DECLARE and FETCH are in the same session. A cursor is private to the session that created it. If your application opens a connection, declares a cursor, returns it to a pool, and later fetches on a different pooled connection, the second connection has no such cursor. Confirm by logging the backend PID with
SELECT pg_backend_pid();before the DECLARE and again before the FETCH. If the numbers differ, you are on a different connection and must keep the cursor on one connection for its whole life. - Check the transaction boundaries. Unless declared
WITH HOLD, a cursor is destroyed when its transaction ends. If your code doesBEGIN; DECLARE c CURSOR FOR SELECT ...; COMMIT;and thenFETCH, the cursor is gone. Either keep the transaction open until the last FETCH and then COMMIT, or declare itWITH HOLDso it survives the commit (it is then closed when the session ends or you CLOSE it). Do not leave long transactions open just to hold a cursor if you can avoid it; they block vacuum. - Look for an earlier CLOSE or a duplicate name. Search your code for
CLOSEon the same cursor name and for a secondDECLAREwith the same name in the same session. Declaring a cursor that already exists raises42P03 duplicate_cursor, but a CLOSE followed by a FETCH raises 34000. Make sure the CLOSE happens only after the last fetch and that error paths also close the cursor before returning the connection to the pool. - Check for a rolled-back transaction. If any statement inside the transaction fails, PostgreSQL marks the transaction aborted and every later command, including FETCH, fails until you ROLLBACK. In psql you will see
current transaction is aborted, but some drivers surface the cursor error instead. RunROLLBACK;and re-run the sequence from the start to confirm. - Inspect the server log. Find the log line for the error and read the statement and the session PID next to it. The log records the failing statement text, which is often enough to spot a FETCH that runs after COMMIT or on a recycled connection. If you cannot read the log directly, ask your DBA or hosting provider for the matching lines around the timestamp.
- Fix the application pattern. For JDBC, set the connection to auto-commit off and keep the same Connection for the ResultSet, or use
setFetchSizewith a forward-only ResultSet. For psycopg, use a named server-side cursor only inside a transaction (with conn.cursor(name='c') as cur:) and never across a commit. For node-postgres, use a single client from the pool for the whole cursor operation and release it only after CLOSE. For Npgsql, keep the reader open on one connection and dispose it before returning the connection to the pool. - Re-test the corrected sequence. Run the fixed code against the test database, then run the same sequence twice in a row to confirm the cursor is cleaned up and can be re-declared. Check
SELECT * FROM pg_cursors;while the transaction is open to see the cursor listed, and again after COMMIT to confirm it is gone (or still present if you used WITH HOLD).
Common Mistakes
- Declaring a cursor, committing, and then fetching. Without WITH HOLD the cursor no longer exists after COMMIT.
- Using a connection pool without pinning the cursor to one connection. The DECLARE and FETCH land on different backends.
- Assuming a cursor is global. Cursors are per session; another session cannot FETCH from your cursor.
- Forgetting to CLOSE on error paths, then later reusing the name and getting a duplicate-cursor error instead.
- Treating 34000 as a permissions problem. It is a name/lifetime problem, not a GRANT issue.
- Relying on a cursor inside an aborted transaction. After any error the transaction must be rolled back before further commands work.
Tips
- Prefer driver-level streaming or fetch-size settings over hand-written DECLARE/FETCH when you can; the driver manages the cursor lifetime for you.
- If you need a cursor to outlive a commit, declare it WITH HOLD, but close it explicitly when done so it does not linger until the session ends.
- Name cursors distinctly per operation (for example
c_orders_export) so a stale CLOSE from another code path is easier to spot. - Log the backend PID and transaction state around cursor operations during debugging; it makes connection-pool bugs obvious.
- Keep cursor transactions short. Long open transactions hold snapshots and delay vacuum, which affects the whole database.
Troubleshooting
If the error persists after checking the transaction and session, confirm which connection your code is actually using by logging pg_backend_pid() at each step. If the PID changes between DECLARE and FETCH, the pool is the cause; configure the pool to hand the same connection back for the duration of the operation, or move the cursor work into a single function that runs on one connection.
If you see current transaction is aborted alongside 34000, issue ROLLBACK and re-run the sequence; the cursor error is a side effect of the aborted state. If the log shows the FETCH running after a COMMIT, add WITH HOLD or restructure so the commit happens last. If you get 42P03 duplicate_cursor, a previous cursor with the same name was never closed; close it or use a unique name. If the error only appears under load, suspect connection pooling and concurrent requests sharing a session. If you cannot read the server log, ask your DBA or hosting provider for the lines around the error timestamp; the failing statement is usually recorded there.
Frequently Asked Questions
What does PostgreSQL error 34000 mean?
It is the invalid_cursor_name error: a FETCH, MOVE, CLOSE, or UPDATE/DELETE WHERE CURRENT OF referenced a cursor that does not exist in the current session or has already been closed.
Does a cursor survive a COMMIT?
Only if it was declared WITH HOLD. A normal cursor is closed automatically when its transaction ends, whether by COMMIT or ROLLBACK.
Can two sessions share a cursor?
No. Cursors are private to the session that declared them; another connection cannot fetch from your cursor by name.
Why does it work in psql but fail in my application?
psql keeps one session for your whole terminal, while application connection pools may switch backends between statements. Pin the cursor to one connection or use the driver's own cursor support.
Is 34000 a permissions error?
No. Permission problems produce different codes such as 42501 insufficient_privilege. 34000 is about the cursor's name and lifetime.
How do I list open cursors?
Query SELECT * FROM pg_cursors; in the same session; it shows cursors visible to that session, including their holdable status.