SQLSTATE P0003 ERROR Class P0: PL/pgSQL Error

too_many_rows query returned more than one row — P0003

PostgreSQL error “query returned more than one row — P0003” (SQLSTATE P0003): what it means, common causes, and how to fix it.

PG 9.6, 10, 11, 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed Jun 2026 Grounded in source

Diagnostic Queries

Symptoms

A PL/pgSQL SELECT … INTO STRICT returned more than one row. PostgreSQL raises SQLSTATE P0003 (too_many_rows).

  • A STRICT INTO query returned multiple rows.
  • STRICT requires exactly one row.
  • Common when the key isn’t actually unique.

What the server log shows

ERROR:  query returned more than one row
CONTEXT:  PL/pgSQL function get_user(integer) line 5 at SQL statement

Why PostgreSQL raises this — what the manual says

Section 41.5.3 Executing a Command with a Single-Row Result:

“If the STRICT option is specified, the command must return exactly one row or a run-time error will be reported, either NO_DATA_FOUND (no rows) or TOO_MANY_ROWS (more than one row).”

With INTO STRICT, PL/pgSQL requires exactly one result row. When the query returns several, it raises P0003 because the single-row assumption is violated.

Common causes

  • The lookup key matched multiple rows (not unique).
  • Missing filter narrowing the result to one row.
  • Joins producing duplicate matches.

How to fix it

  1. Add a unique predicate or LIMIT 1 with explicit ordering if appropriate.
  2. Ensure the key is genuinely unique (constraint/index).
  3. Use a loop or aggregate if multiple rows are legitimately expected.

Related & next steps

Reference: PostgreSQL 18 Section 43.5 “Basic Statements”.

Was this helpful?