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
- Add a unique predicate or
LIMIT 1with explicit ordering if appropriate. - Ensure the key is genuinely unique (constraint/index).
- Use a loop or aggregate if multiple rows are legitimately expected.
Related & next steps
Reference: PostgreSQL 18 Section 43.5 “Basic Statements”.
Thanks — noted. This helps keep the database accurate.