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.

What lands in your log

ERROR: query returned no rows

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

In 10 seconds

What triggers it
Run SELECT ... INTO STRICT against a query that matches no rows.
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.

Fix

What to do right now

Application-level steps for 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
INSERT INTO batch7_p0002_accounts(id, balance)
VALUES (999, 42);
DO $$
DECLARE
  v_balance numeric;
BEGIN
  SELECT balance INTO STRICT v_balance
  FROM batch7_p0002_accounts
  WHERE id = 999;
  RAISE NOTICE 'balance = %', v_balance;
END $$;

Connected

Everything this error touches

Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.

Open in the interactive map →

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops