SQLSTATE 42P11 ERROR Class 42: Syntax Error or Access Rule Violation

invalid_cursor_definition cannot open … query as cursor — 42P11

PostgreSQL error "cannot open … query as cursor" (SQLSTATE 42P11): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Diagnostic Queries

Symptoms

A cursor was opened over a query that cannot back a cursor (for example a utility/data-modifying statement). PostgreSQL raises SQLSTATE 42P11 (invalid_cursor_definition).

  • The statement type can’t be used as a cursor source.
  • Cursors require a query that returns a result set.
  • Common with non-SELECT statements.

What the server log shows

ERROR:  cannot open INSERT query as cursor

Why PostgreSQL raises this — what the manual says

As the DECLARE reference (Parameters) explains:

Only a SELECT or VALUES query can back a cursor; a statement that does not return a result set (such as a utility command, or a data-modifying statement without a RETURNING clause) cannot be opened as a cursor.

A cursor iterates over rows produced by a query. Statements that don’t produce a result set (e.g. an INSERT without RETURNING, or utility commands) can’t back a cursor, so PostgreSQL reports 42P11.

Common causes

  • Declaring a cursor over an INSERT/UPDATE/DELETE without RETURNING.
  • Trying to cursor over a utility/DDL statement.
  • Passing the wrong statement to a cursor-opening API.

How to fix it

  1. Use a SELECT or VALUES query for the cursor.
  2. Add RETURNING if you need rows back from a DML statement (without a cursor).
  3. Run utility statements directly rather than via a cursor.

Related & next steps

Reference: PostgreSQL 18 — DECLARE.

Was this helpful?