# P0002 — no_data_found

> Source-backed reference for PostgreSQL SQLSTATE P0002.
---

# P0002

## At a glance {#at-a-glance}

P0002 is PL/pgSQL `no_data_found`. Fixed paths include the strict-query ERROR text and a separate tablespace NOTICE path; it does not mean every SELECT returning zero rows.

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| Field | Value |
| --- | --- |
| SQLSTATE | `P0002` |
| Condition | `no_data_found` |
| Status | `active` |
| Known present by | `8.2.0` |
| Locked snapshots | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| Macros | `ERRCODE_NO_DATA_FOUND` |
| Aliases | `—` |

<!-- source facts: data/errcodes/P0002.json -->
<!-- END SQLSTATE FACTS -->

## Meaning {#meaning}

A fixed PL/pgSQL strict-query path reports `query returned no rows` and may attach `parameters: %s` detail, while a tablespace path uses the same SQLSTATE for the NOTICE `no matching relations in tablespace "%s" found`. It does not mean every SELECT returning zero rows.

## Diagnosis {#diagnosis}

Separate strict-query ERROR from the tablespace NOTICE by message and severity. A normal zero-row SELECT is not enough to establish P0002.

## Response {#response}

Handle the strict no-row case explicitly. For the tablespace NOTICE, verify whether the empty match is intended; if it is, no repair is required, otherwise correct the target before continuing. Do not automatically replay a zero-row operation.

## Versions {#versions}

The locked catalogue records this condition from 8.2.0 in the listed formal snapshots and 19beta3; fixed source coverage is PostgreSQL 18.6.

## Related {#related}

[`P0003`](../p0003/), [`P0004`](../p0004/), [`02000`](../02000/)

## Sources {#sources}

See the fixed [pl_exec.c](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/pl/plpgsql/src/pl_exec.c#L4384-L4387) and [tablecmds.c](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/tablecmds.c#L17162-L17166) paths alongside the structured [evidence record](../data/evidence/p0002.json).
