Diagnostic Queries
Symptoms
A PL/pgSQL SELECT … INTO STRICT returned no rows. PostgreSQL raises SQLSTATE P0002 (no_data_found).
- A STRICT INTO query found zero rows.
- STRICT requires exactly one row.
- Common when the lookup key doesn’t match.
What the server log shows
ERROR: query returned no rows
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 none, it raises P0002 so the missing-data case is handled explicitly.
Common causes
- The lookup key matched no rows.
- Filtering criteria excluded all rows.
- Expecting a row that was deleted/never inserted.
How to fix it
- Handle the case with an
EXCEPTION WHEN NO_DATA_FOUNDblock. - Use non-STRICT
INTOand checkNOT FOUNDif zero rows is valid. - Verify the lookup key exists before the STRICT query.
Related & next steps
Reference: PostgreSQL 18 Section 43.5 “Basic Statements”.
Thanks — noted. This helps keep the database accurate.