PG
PRO
42P11ERRORTier 2 — Caution✅ HIGH confidence

invalid cursor definition

Category: Syntax Error or Access Rule ViolationVersions: All Postgres versions

What this means

SQLSTATE 42P11 is raised when a cursor definition is structurally invalid — for example, using FOR UPDATE on a cursor query that contains a JOIN, DISTINCT, or aggregate which makes the cursor non-updatable.

Why it happens

  1. 1DECLARE ... FOR UPDATE on a cursor query that is not simple enough to support row-level locking (contains JOINs, DISTINCT, GROUP BY, etc.)

How to reproduce

FOR UPDATE cursor with a JOIN.

trigger — this will ERROR
DECLARE my_cursor CURSOR FOR
  SELECT e.*, d.name FROM employees e JOIN departments d ON e.dept_id = d.id
  FOR UPDATE;
ERROR: cursor FOR UPDATE/SHARE is not allowed with joins

Fix 1: Remove FOR UPDATE from cursors with JOINs

When row locking is not needed.

fix
DECLARE my_cursor CURSOR FOR
  SELECT e.*, d.name FROM employees e JOIN departments d ON e.dept_id = d.id;

Why this works

FOR UPDATE is only supported on simple single-table cursor queries.

Fix 2: Separate the cursor query to a single table for locking

When row locking is required.

fix
DECLARE my_cursor CURSOR FOR
  SELECT * FROM employees FOR UPDATE;

Why this works

Use a simple single-table cursor with FOR UPDATE, then join other tables in the processing loop if needed.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 42 — Syntax Error or Access Rule Violation (Postgres-specific)

Confidence assessment

✅ HIGH confidence

Postgres-specific. Stable across versions.

See also

📄 Reference pages

DECLARE CURSORFOR UPDATE
⚙️ 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 →