SQLSTATE 2F005 ERROR Class 2F: SQL Routine Exception

function_executed_no_return_statement control reached end of function without RETURN — 2F005

PostgreSQL error "control reached end of function without RETURN" (SQLSTATE 2F005): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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 IF lacks an ELSE that returns.
  • Does not apply to void functions.

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/CASE branch 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

  1. Add a RETURN to every code path (including a final default RETURN).
  2. Add an ELSE that returns in conditional logic.
  3. For set-returning functions, ensure RETURN/RETURN NEXT/RETURN QUERY covers all paths.

Related & next steps

Reference: PostgreSQL 18 Section 43.6 “Control Structures”.

Was this helpful?