Common SQL Errors

PostgreSQL function_executed_no_return_statement (2F005) Error Explained and Fixed

August 4, 2025

PostgreSQL raises 2F005 when a PL/pgSQL function that is declared to return a value finishes without executing a RETURN statement.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is the PostgreSQL function_executed_no_return_statement error?

function_executed_no_return_statement (PostgreSQL 2F005) appears when a PL/pgSQL function declared with RETURNS <type> exits without hitting a RETURN statement. Ensure every execution path ends with RETURN or change the function to RETURNS void to resolve the error.

Error Highlights

Typical Error Message

function_executed_no_return_statement

Error Type

Runtime Error

Language

PostgreSQL

Symbol

function_executed_no_return_statement

Error Code

2F005

SQL State

Explanation

Table of Contents

What is the function_executed_no_return_statement error in PostgreSQL?

PostgreSQL error code 2F005 signals that a PL/pgSQL function declared to return a value completed execution without issuing a RETURN. The server cannot supply the expected result, so it aborts the call and raises function_executed_no_return_statement.

The error is runtime, not syntax. It only appears when the problematic execution path is taken.

Catching it early is important because it can break application logic, stored procedure chains, and scheduled jobs.

What Causes This Error?

A RETURN is missing in at least one code path of a non-void function.

This commonly happens after adding conditional branches, early EXIT statements, or TRY-CATCH blocks that swallow the RETURN.

The error also occurs when a RETURNS TABLE or RETURNS SETOF function forgets to RETURN NEXT or RETURN QUERY before the end.

How to Fix function_executed_no_return_statement

Add an explicit RETURN at the end of the function or in every branch. If the function should not return anything, redefine it as RETURNS void.

Verify that exception handlers rethrow or return a default value.

Test all logical paths with representative input data.

Common Scenarios and Solutions

If-ELSE chains: place a final ELSE with a RETURN or end the function with RETURN default_value.

Exception blocks: include RETURN inside EXCEPTION WHEN or add a final RETURN after END;

Best Practices to Avoid This Error

Use static analysis or pgTAP tests to assert that every path returns.

Keep functions short and single-responsibility so missing RETURNs are obvious.

Prefer RETURNS void for procedural code that produces side effects only. Review diff changes to ensure new branches include RETURN.

Related Errors and Solutions

2F002 - modifying_sql_data_not_permitted triggers when a read-only function writes data. Change volatility or rewrite queries.

25114 - invalid_cursor_state arises when fetching from a closed cursor. Always OPEN before FETCH and CLOSE afterward.

.

Common Causes

Related Errors

FAQs

Does this error occur in SQL functions written in SQL language?

Yes. SQL language functions declared RETURNS something must include a SELECT expression or RETURN QUERY. Omitting it triggers the same 2F005 error.

Can I catch 2F005 in application code?

Yes. PostgreSQL sends SQLSTATE 2F005 over the wire, so drivers can trap it. However, fixing the function is the recommended approach.

Will changing to RETURNS void affect callers?

Callers that expect a result set will break. Ensure no dependency exists or update the calling SQL before switching to void.

How does Galaxy help?

Galaxy’s editor flags missing RETURN statements during linting and lets teams review PL/pgSQL diffs collaboratively, preventing the error before deployment.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo