Diagnostic Queries
Symptoms
A PL/pgSQL function declared to return a value finished executing without hitting a RETURN statement on some code path. PostgreSQL raises SQLSTATE 2F005 (function_executed_no_return_statement).
- Some branch of the function falls through without RETURN.
- Common when an
IFlacks anELSEthat returns. - Does not apply to
voidfunctions.
What the server log shows
ERROR: control reached end of function without RETURN
CONTEXT: PL/pgSQL function classify(integer)
Why PostgreSQL raises this — what the manual says
Section 41.6.1 Returning from a Function:
“If control reaches the end of the top-level block of the function without hitting a RETURN statement, a run-time error will occur.”
A value-returning PL/pgSQL function must produce a value via RETURN on every path. If execution reaches the end without one, there is nothing to return and PostgreSQL raises 2F005.
Common causes
- An
IF/CASEbranch with no terminating RETURN. - Missing a final fallback RETURN at the end of the function.
- A loop that exits without returning.
How to fix it
- Add a RETURN to every code path (including a final default RETURN).
- Add an
ELSEthat returns in conditional logic. - For set-returning functions, ensure
RETURN/RETURN NEXT/RETURN QUERYcovers all paths.
Related & next steps
Reference: PostgreSQL 18 Section 43.6 “Control Structures”.
Thanks — noted. This helps keep the database accurate.