Common SQL Errors

PostgreSQL invalid_function_definition Error (42P13) Explained

August 4, 2025

PostgreSQL raises invalid_function_definition (SQLSTATE 42P13) when a CREATE FUNCTION or CREATE PROCEDURE statement contains an illegal attribute mix, duplicate clause, or a body that conflicts with the declared signature.

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 invalid_function_definition error?

PostgreSQL Error 42P13 - invalid_function_definition occurs when the CREATE FUNCTION statement includes conflicting attributes or a body that mismatches the declared signature. Recheck LANGUAGE, RETURNS, parameter types, and remove duplicate clauses to resolve the error.

Error Highlights

Typical Error Message

PostgreSQL Error 42P13

Error Type

Function Definition Error

Language

PostgreSQL

Symbol

invalid_function_definition

Error Code

42P13

SQL State

Explanation

Table of Contents

What is the PostgreSQL invalid_function_definition error?

PostgreSQL throws error 42P13 - invalid_function_definition - while parsing a CREATE FUNCTION or CREATE PROCEDURE command that contains incompatible or duplicate clauses, or when the function body contradicts the declared argument and return types.

The server halts execution of the statement to protect catalog integrity.

Developers must fix the definition before the object can be created or replaced.

What Causes This Error?

Conflicting attributes such as specifying both STRICT and CALLED ON NULL INPUT trigger 42P13 immediately.

PostgreSQL treats these flags as mutually exclusive.

Duplicate clauses like two LANGUAGE declarations or two RETURNS clauses violate grammar rules and surface the same error.

A body that does not return the declared data type (for example, returning TEXT when RETURNS integer is declared) also leads to invalid_function_definition at creation time in PL languages that check source code.

How to Fix PostgreSQL Error 42P13

Validate each attribute combination and keep only one of each clause.

Ensure the RETURNS clause matches the actual return expression in the body.

Run the function in a test block or use CREATE OR REPLACE with RAISE NOTICE statements to confirm types align.

Common Scenarios and Solutions

PL/pgSQL functions that declare RETURNS void but use RETURN query produce 42P13. Change RETURNS to TABLE or SETOF type, or remove the RETURN statement.

Functions copied from another database often include OUT parameters and a RETURNS clause.

Remove RETURNS when OUT parameters already define the result composite type.

Best Practices to Avoid This Error

Always write the signature first, then stub the body with RETURN NULL to let the parser evaluate clauses before adding logic.

Use Galaxy's linting inside the SQL editor to surface mixed attributes and type mismatches as you type, preventing invalid definitions from reaching the database.

Related Errors and Solutions

Error 42723 - duplicate_function appears when the name and argument types already exist.

Alter the signature or drop the old function.

Error 42601 - syntax_error may be thrown if the function body has unmatched BEGIN/END or missing semicolons.

.

Common Causes

Related Errors

FAQs

Does this error depend on PostgreSQL version?

42P13 exists in all supported versions. Newer releases may introduce extra attribute checks, so review release notes when upgrading.

Can I ignore the error and create the function later?

No. The function is not created until the definition is valid. Fix conflicts first.

How does Galaxy help?

Galaxy's real time parser flags duplicate clauses and type mismatches while you type, preventing 42P13 from reaching production.

Is STRICT always better than CALLED ON NULL INPUT?

Use STRICT when null inputs should short circuit execution. Use CALLED ON NULL INPUT when nulls need special handling inside the body.

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