Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
P0003 is the PL/pgSQL too_many_rows condition, the PostgreSQL counterpart of Oracle's ORA-01422. A variable can hold only one row, but the query behind INTO returned several. PostgreSQL adds HINT: Make sure the query returns a single row, or use LIMIT 1. and a CONTEXT line with the function and line number.
LIMIT 1 makes the error go away, but it picks an arbitrary row. First run the query by hand and count the rows, then decide whether you need an aggregate, a loop, or clean data. Below are three cases we reproduced on PostgreSQL 17, each with the fix actually run.
01The WHERE condition matches several rows (SELECT INTO STRICT)
dept_salary('SALES') runs SELECT salary INTO STRICT v FROM emp WHERE dept = p_dept. Grouping by dept showed SALES has 2 rows, so one variable cannot hold the result.
If you want the department total, aggregate: SELECT sum(salary) INTO STRICT v .... An aggregate always returns exactly one row. After the change the call returned 6500 for SALES and 4000 for HR.
Setup
CREATE TABLE emp (id int PRIMARY KEY, dept text, salary int);
INSERT INTO emp VALUES (1, 'SALES', 3000), (2, 'SALES', 3500), (3, 'HR', 4000);
CREATE FUNCTION dept_salary(p_dept text) RETURNS int LANGUAGE plpgsql AS $$
DECLARE v int;
BEGIN
SELECT salary INTO STRICT v FROM emp WHERE dept = p_dept;
RETURN v;
END $$;CREATE TABLE
INSERT 0 3
CREATE FUNCTIONSELECT dept_salary('SALES');ERROR: P0003: query returned more than one row
HINT: Make sure the query returns a single row, or use LIMIT 1.
CONTEXT: PL/pgSQL function dept_salary(text) line 4 at SQL statementSELECT dept, count(*) FROM emp GROUP BY dept ORDER BY dept; dept | count
-------+-------
HR | 1
SALES | 2
(2 rows)CREATE OR REPLACE FUNCTION dept_salary(p_dept text) RETURNS int LANGUAGE plpgsql AS $$
DECLARE v int;
BEGIN
SELECT sum(salary) INTO STRICT v FROM emp WHERE dept = p_dept;
RETURN v;
END $$;CREATE FUNCTIONPick the fix by meaning: sum/max for one value, a loop for each row, or a tighter WHERE when only one row was intended.
SELECT dept_salary('SALES') AS sales, dept_salary('HR') AS hr; sales | hr
-------+------
6500 | 4000
(1 row)02UPDATE ... RETURNING INTO changed more than one row
UPDATE orders SET status = 'SHIPPED' WHERE customer_id = 10 RETURNING id INTO v_id failed with P0003 even without STRICT. Customer 10 has orders 1 and 2. With RETURNING ... INTO, more than one row is an error regardless of STRICT.
To handle every updated row, loop over it: FOR r IN UPDATE ... RETURNING id LOOP. The block printed shipped order 1 and shipped order 2, and both rows became SHIPPED while order 3 stayed NEW.
Setup
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, status text);
INSERT INTO orders VALUES (1, 10, 'NEW'), (2, 10, 'NEW'), (3, 20, 'NEW');CREATE TABLE
INSERT 0 3DO $$
DECLARE v_id int;
BEGIN
UPDATE orders SET status = 'SHIPPED' WHERE customer_id = 10 RETURNING id INTO v_id;
RAISE NOTICE 'shipped order %', v_id;
END $$;ERROR: P0003: query returned more than one row
HINT: Make sure the query returns a single row, or use LIMIT 1.
CONTEXT: PL/pgSQL function inline_code_block line 4 at SQL statementSELECT id, customer_id, status FROM orders WHERE customer_id = 10; id | customer_id | status
----+-------------+--------
1 | 10 | NEW
2 | 10 | NEW
(2 rows)DO $$
DECLARE r record;
BEGIN
FOR r IN UPDATE orders SET status = 'SHIPPED' WHERE customer_id = 10 RETURNING id LOOP
RAISE NOTICE 'shipped order %', r.id;
END LOOP;
END $$;
SELECT id, status FROM orders ORDER BY id;NOTICE: 00000: shipped order 1
NOTICE: 00000: shipped order 2
DO
id | status
----+---------
1 | SHIPPED
2 | SHIPPED
3 | NEW
(3 rows)03Duplicate rows in a lookup table that should have a unique key
The code table had no primary key, and A01 was stored twice (Open and Open (old)). SELECT label INTO STRICT v FROM codes WHERE code = 'A01' failed. GROUP BY code HAVING count(*) > 1 showed A01 | 2.
Here the code is wrong, not the data model, so we deleted the old row (DELETE 1) and added PRIMARY KEY (code) so it cannot happen again. The same unchanged block then printed label = Open.
Setup
CREATE TABLE codes (code text, label text);
INSERT INTO codes VALUES ('A01', 'Open'), ('A01', 'Open (old)'), ('B02', 'Closed');CREATE TABLE
INSERT 0 3DO $$
DECLARE v text;
BEGIN
SELECT label INTO STRICT v FROM codes WHERE code = 'A01';
RAISE NOTICE 'label = %', v;
END $$;ERROR: P0003: query returned more than one row
HINT: Make sure the query returns a single row, or use LIMIT 1.
CONTEXT: PL/pgSQL function inline_code_block line 4 at SQL statementSELECT code, count(*) FROM codes GROUP BY code HAVING count(*) > 1; code | count
------+-------
A01 | 2
(1 row)DELETE FROM codes WHERE code = 'A01' AND label = 'Open (old)';
ALTER TABLE codes ADD PRIMARY KEY (code);DELETE 1
ALTER TABLECheck which duplicate is correct before deleting. Adding the key fails while duplicates remain.
DO $$
DECLARE v text;
BEGIN
SELECT label INTO STRICT v FROM codes WHERE code = 'A01';
RAISE NOTICE 'label = %', v;
END $$;NOTICE: 00000: label = Open
DOFAQ
What does "query returned more than one row" mean in PostgreSQL?
In PL/pgSQL, a query whose result goes INTO a variable returned two or more rows. It happens with SELECT ... INTO STRICT and with INSERT/UPDATE/DELETE ... RETURNING ... INTO.
Is P0003 the same as Oracle ORA-01422 exact fetch returns more than requested number of rows?
It is the PL/pgSQL error for the same situation (too many rows into a variable). The condition name in PostgreSQL is too_many_rows.
Should I just add LIMIT 1?
The HINT suggests it, but LIMIT 1 hides the extra rows and returns whichever comes first. Use it only when any row is acceptable; otherwise aggregate, loop, or fix the duplicates as shown here.
Why do I get P0003 from UPDATE RETURNING INTO without STRICT?
For INSERT, UPDATE and DELETE with RETURNING ... INTO, more than one row is always an error. Loop with FOR r IN UPDATE ... RETURNING ... LOOP instead.
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