Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
2F005 is raised at run time, not when the function is created. PL/pgSQL compiles a function even if some paths lack RETURN; the error appears only when a call actually takes such a path. The CONTEXT line shows the function, e.g. PL/pgSQL function grade(integer).
Look for the path the failing input takes: an IF without ELSE, an exception handler, or the end of a loop. Below are three cases reproduced on PostgreSQL 17 with the fixes that were actually run.
01IF / ELSIF without a final RETURN
grade() returns A for 90+ and B for 80+, but nothing for lower scores. grade(75) fell through END IF and failed with 2F005.
Adding RETURN 'C'; after END IF covered the rest: grade(95), grade(85), grade(75) returned A, B, C.
Setup
CREATE FUNCTION grade(score int) RETURNS text LANGUAGE plpgsql AS $$
BEGIN
IF score >= 90 THEN
RETURN 'A';
ELSIF score >= 80 THEN
RETURN 'B';
END IF;
END $$;CREATE FUNCTIONSELECT grade(75);ERROR: 2F005: control reached end of function without RETURN
CONTEXT: PL/pgSQL function grade(integer)CREATE OR REPLACE FUNCTION grade(score int) RETURNS text LANGUAGE plpgsql AS $$
BEGIN
IF score >= 90 THEN
RETURN 'A';
ELSIF score >= 80 THEN
RETURN 'B';
END IF;
RETURN 'C';
END $$;CREATE FUNCTIONSELECT grade(95), grade(85), grade(75); grade | grade | grade
-------+-------+-------
A | B | C
(1 row)02EXCEPTION handler that only logs
to_amount() returns txt::numeric. For '12,000' the cast fails, the handler prints NOTICE: bad amount: 12,000, and then the function ends without a value, so 2F005 follows the notice.
Adding RETURN NULL; inside the handler fixed it: to_amount('12000') returned 12000 and to_amount('12,000') returned NULL with the same notice.
Setup
CREATE FUNCTION to_amount(txt text) RETURNS numeric LANGUAGE plpgsql AS $$
BEGIN
RETURN txt::numeric;
EXCEPTION
WHEN invalid_text_representation THEN
RAISE NOTICE 'bad amount: %', txt;
END $$;CREATE FUNCTIONSELECT to_amount('12,000');NOTICE: 00000: bad amount: 12,000
ERROR: 2F005: control reached end of function without RETURN
CONTEXT: PL/pgSQL function to_amount(text)CREATE OR REPLACE FUNCTION to_amount(txt text) RETURNS numeric LANGUAGE plpgsql AS $$
BEGIN
RETURN txt::numeric;
EXCEPTION
WHEN invalid_text_representation THEN
RAISE NOTICE 'bad amount: %', txt;
RETURN NULL;
END $$;CREATE FUNCTIONSELECT to_amount('12000') AS ok, to_amount('12,000') AS bad;NOTICE: 00000: bad amount: 12,000
ok | bad
-------+-----
12000 |
(1 row)03Loop returns only when it finds a match
find_name() loops over products and returns the name when the id matches. For id 3 no row matched, the loop ended, and the function reached its end without RETURN.
Adding RETURN NULL; after END LOOP made find_name(1) return pen and find_name(3) return NULL.
Setup
CREATE TABLE products (id int PRIMARY KEY, name text);
INSERT INTO products VALUES (1, 'pen'), (2, 'ink');
CREATE FUNCTION find_name(p_id int) RETURNS text LANGUAGE plpgsql AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT id, name FROM products LOOP
IF r.id = p_id THEN
RETURN r.name;
END IF;
END LOOP;
END $$;CREATE TABLE
INSERT 0 2
CREATE FUNCTIONSELECT find_name(3);ERROR: 2F005: control reached end of function without RETURN
CONTEXT: PL/pgSQL function find_name(integer)CREATE OR REPLACE FUNCTION find_name(p_id int) RETURNS text LANGUAGE plpgsql AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT id, name FROM products LOOP
IF r.id = p_id THEN
RETURN r.name;
END IF;
END LOOP;
RETURN NULL;
END $$;CREATE FUNCTIONSELECT find_name(1) AS found, find_name(3) AS not_found; found | not_found
-------+-----------
pen |
(1 row)FAQ
What does control reached end of function without RETURN mean?
A PL/pgSQL function with a return type reached its last line without executing RETURN. It happens only for inputs that take that path.
Why did CREATE FUNCTION succeed if RETURN is missing?
PL/pgSQL does not check every path at creation. In all three cases CREATE FUNCTION succeeded and the error came at the call.
Does the EXCEPTION block need its own RETURN?
Yes. After a handler runs, the function continues to its end. In the second case the notice printed and then 2F005 came until RETURN NULL; was added.
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