Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
P0002 is the PL/pgSQL no_data_found condition, the PostgreSQL counterpart of Oracle's ORA-01403. It comes from SELECT ... INTO STRICT or EXECUTE ... INTO STRICT when the query returns no row. The CONTEXT line shows the function and line number, for example PL/pgSQL function get_salary(integer) line 4 at SQL statement.
The first step is the same every time: run the query by hand and confirm it really returns 0 rows. Then decide whether "no row" is an error or a normal case. Below are three cases we reproduced on PostgreSQL 17, each with the fix actually run.
01A function looks up a key that does not exist (SELECT INTO STRICT)
get_salary(99) runs SELECT salary INTO STRICT v FROM emp WHERE id = p_id, and there is no id 99. SELECT count(*) FROM emp WHERE id = 99 returned 0.
If a missing key is a normal case, catch it inside the function with EXCEPTION WHEN no_data_found THEN RETURN 0;. After the change, get_salary(99) returned 0 and get_salary(1) still returned 3000.
Setup
CREATE TABLE emp (id int PRIMARY KEY, name text, salary int);
INSERT INTO emp VALUES (1, 'Kim', 3000), (2, 'Lee', 4000);
CREATE FUNCTION get_salary(p_id int) RETURNS int LANGUAGE plpgsql AS $$
DECLARE v int;
BEGIN
SELECT salary INTO STRICT v FROM emp WHERE id = p_id;
RETURN v;
END $$;CREATE TABLE
INSERT 0 2
CREATE FUNCTIONSELECT get_salary(99);ERROR: P0002: query returned no rows
CONTEXT: PL/pgSQL function get_salary(integer) line 4 at SQL statementSELECT count(*) FROM emp WHERE id = 99; count
-------
0
(1 row)CREATE OR REPLACE FUNCTION get_salary(p_id int) RETURNS int LANGUAGE plpgsql AS $$
DECLARE v int;
BEGIN
SELECT salary INTO STRICT v FROM emp WHERE id = p_id;
RETURN v;
EXCEPTION
WHEN no_data_found THEN
RETURN 0;
END $$;CREATE FUNCTIONOnly catch it when you have a sensible default. If a missing row means bad input, let the error reach the caller.
SELECT get_salary(99) AS not_found, get_salary(1) AS found; not_found | found
-----------+-------
0 | 3000
(1 row)02STRICT used where "not found" should just be a branch
A DO block selected name INTO STRICT v_name for id 5, but the table only has ids 1 and 2, so it failed with P0002.
Without STRICT, a query with no rows does not raise an error; the variable stays NULL and FOUND is false. With SELECT name INTO v_name ... followed by IF NOT FOUND THEN, the block printed NOTICE: id 5 not found and finished normally.
Setup
CREATE TABLE emp (id int PRIMARY KEY, name text, salary int);
INSERT INTO emp VALUES (1, 'Kim', 3000), (2, 'Lee', 4000);CREATE TABLE
INSERT 0 2DO $$
DECLARE v_name text;
BEGIN
SELECT name INTO STRICT v_name FROM emp WHERE id = 5;
RAISE NOTICE 'name = %', v_name;
END $$;ERROR: P0002: query returned no rows
CONTEXT: PL/pgSQL function inline_code_block line 4 at SQL statementSELECT id, name FROM emp ORDER BY id; id | name
----+------
1 | Kim
2 | Lee
(2 rows)DO $$
DECLARE v_name text;
BEGIN
SELECT name INTO v_name FROM emp WHERE id = 5;
IF NOT FOUND THEN
RAISE NOTICE 'id 5 not found';
ELSE
RAISE NOTICE 'name = %', v_name;
END IF;
END $$;NOTICE: 00000: id 5 not found
DO03Dynamic SQL with EXECUTE ... INTO STRICT found no row
EXECUTE format('SELECT salary FROM %I WHERE name = $1', 'emp') INTO STRICT v USING 'Park' fails the same way; the CONTEXT line ends in at EXECUTE instead of at SQL statement. There is no Park in the table ((0 rows)).
Removing STRICT leaves v as NULL instead of raising. With coalesce(v, 0) the block printed salary = 0.
Setup
CREATE TABLE emp (id int PRIMARY KEY, name text, salary int);
INSERT INTO emp VALUES (1, 'Kim', 3000), (2, 'Lee', 4000);CREATE TABLE
INSERT 0 2DO $$
DECLARE v int;
BEGIN
EXECUTE format('SELECT salary FROM %I WHERE name = $1', 'emp') INTO STRICT v USING 'Park';
RAISE NOTICE 'salary = %', v;
END $$;ERROR: P0002: query returned no rows
CONTEXT: PL/pgSQL function inline_code_block line 4 at EXECUTESELECT name FROM emp WHERE name = 'Park'; name
------
(0 rows)DO $$
DECLARE v int;
BEGIN
EXECUTE format('SELECT salary FROM %I WHERE name = $1', 'emp') INTO v USING 'Park';
RAISE NOTICE 'salary = %', coalesce(v, 0);
END $$;NOTICE: 00000: salary = 0
DOFAQ
What does "query returned no rows" mean in PL/pgSQL?
A SELECT ... INTO STRICT or EXECUTE ... INTO STRICT returned zero rows. STRICT requires exactly one row, so zero rows raise P0002 (no_data_found).
Is P0002 the same as Oracle ORA-01403 no data found?
It is the PL/pgSQL error for the same situation, and the condition name is also no_data_found. In PostgreSQL it is raised by INTO STRICT; this page shows that without STRICT the same query just leaves the variable NULL.
How do I catch no_data_found in a PostgreSQL function?
Add EXCEPTION WHEN no_data_found THEN ... to the block, as in the first case here, which returned 0 instead of failing.
How do I check if SELECT INTO found a row?
Drop STRICT and test IF NOT FOUND THEN right after the SELECT. In the second case this printed id 5 not found instead of an error.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12