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" (SQLSTATE P0003): 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 PL/pgSQL SELECT … INTO STRICT expected exactly one row but the query returned several. PostgreSQL raises SQLSTATE P0003 (too_many_rows).

  • Only occurs with the STRICT option in PL/pgSQL.
  • The query matched multiple rows when one was required.
  • Often a missing/non-unique key in the WHERE clause.

What the server log shows

ERROR:  query returned more than one row
CONTEXT:  PL/pgSQL function get_customer(text) 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).”

INTO STRICT can assign only one row to its targets. When the query yields more than one, PL/pgSQL cannot choose deterministically and raises P0003.

Common causes

  • A WHERE clause that is not actually unique.
  • Joining produces duplicate rows.
  • Looking up by a non-key column expecting uniqueness.

How to fix it

  1. Make the predicate unique (filter on a primary/unique key).
  2. Add LIMIT 1 with an explicit ORDER BY if any single row is acceptable.
  3. Drop STRICT and loop over results if multiple rows are valid.

Related & next steps

Reference: PostgreSQL 18 Section 43.5 “Basic Statements”.

Was this helpful?