SQLSTATE P0002Severity lowLab verified

Incident brief

No data found

A PL/pgSQL SELECT ... INTO STRICT found zero matching rows. PostgreSQL raised an error instead of silently leaving the target null.

Reproduced on PostgreSQL 16.14Verified 2026-07-16 (Docker lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
No data found
What triggers it
Run SELECT ... INTO STRICT against a query that matches no rows.
The fix
Wrap the STRICT select in a BEGIN/EXCEPTION block and catch WHEN NO_DATA_FOUND if a missing row is an expected, handleable case.
Proof
Reproduced on PostgreSQL 16.14 → A SELECT ... INTO STRICT that matched zero rows was rejected with SQLSTATE P0002. The session was unaffected afterward.

The fix

What to do right now

The immediate, application-level response to this error.

  • Wrap the STRICT select in a BEGIN/EXCEPTION block and catch WHEN NO_DATA_FOUND if a missing row is an expected, handleable case.
  • Or check existence first with an ordinary (non-STRICT) SELECT INTO plus IF NOT FOUND, if you'd rather branch than raise.
Fix — SQL
BEGIN
  SELECT balance INTO STRICT v_balance FROM accounts WHERE id = 999;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    RAISE NOTICE 'account % not found', 999;
END;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-16 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed