PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 42P13: cannot change return type of existing function

CREATE OR REPLACE FUNCTION can change the body, but not the return type or input parameter names of an existing function.

PostgreSQL 17.12
42P13: cannot change return type of existing function

Jump to a cause

  1. 01Return type changed from int to numeric
  2. 02A column added to RETURNS TABLE
  3. 03Input parameter renamed (amount → p_amount)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

CREATE OR REPLACE FUNCTION replaces a function with the same name and argument types, but only its body and some options. If the new definition changes the return type, the columns of RETURNS TABLE, or the name of an input parameter, PostgreSQL stops with 42P13 and the hint Use DROP FUNCTION ... first.

The three runs below hit each of those. In all of them the fix was the one in the hint: DROP FUNCTION with the argument list, then CREATE FUNCTION again.

01Return type changed from int to numeric

order_total(int) returned int. CREATE OR REPLACE FUNCTION order_total(p_id int) RETURNS numeric ... failed with cannot change return type of existing function and HINT: Use DROP FUNCTION order_total(integer) first.

pg_proc showed the current return type integer. After DROP FUNCTION order_total(int) and CREATE FUNCTION with RETURNS numeric, SELECT order_total(1) returned 1050.50.

Setup
Setuprun as lab_a
CREATE FUNCTION order_total(p_id int) RETURNS int LANGUAGE sql AS $$ SELECT 1050 $$;
OUTPUT
CREATE FUNCTION
Reproducerun as lab_a
CREATE OR REPLACE FUNCTION order_total(p_id int) RETURNS numeric LANGUAGE sql AS $$ SELECT 1050.50 $$;
✕ ERROR
ERROR:  42P13: cannot change return type of existing function
HINT:  Use DROP FUNCTION order_total(integer) first.
Checkrun as lab_a
SELECT oid::regprocedure AS signature, prorettype::regtype AS returns FROM pg_proc WHERE proname = 'order_total';
OUTPUT
      signature       | returns
----------------------+---------
 order_total(integer) | integer
(1 row)
Fixrun as lab_a
DROP FUNCTION order_total(int);
CREATE FUNCTION order_total(p_id int) RETURNS numeric LANGUAGE sql AS $$ SELECT 1050.50 $$;
OUTPUT
DROP FUNCTION
CREATE FUNCTION
After the fixrun as lab_a
SELECT order_total(1);
✓ SUCCESS
 order_total
-------------
     1050.50
(1 row)

02A column added to RETURNS TABLE

order_list() returned TABLE (id, amount). Adding customer to RETURNS TABLE in CREATE OR REPLACE failed with the same message plus DETAIL: Row type defined by OUT parameters is different.

RETURNS TABLE columns are OUT parameters, so changing them changes the return type. After DROP FUNCTION order_list() and CREATE FUNCTION, the function returned id, customer, amount for Kim and Lee.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int, customer text, amount int);
INSERT INTO orders VALUES (1, 'Kim', 100), (2, 'Lee', 70);
CREATE FUNCTION order_list() RETURNS TABLE (id int, amount int) LANGUAGE sql AS $$ SELECT id, amount FROM orders $$;
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE FUNCTION
Reproducerun as lab_a
CREATE OR REPLACE FUNCTION order_list() RETURNS TABLE (id int, customer text, amount int) LANGUAGE sql AS $$ SELECT id, customer, amount FROM orders $$;
✕ ERROR
ERROR:  42P13: cannot change return type of existing function
DETAIL:  Row type defined by OUT parameters is different.
HINT:  Use DROP FUNCTION order_list() first.
Fixrun as lab_a
DROP FUNCTION order_list();
CREATE FUNCTION order_list() RETURNS TABLE (id int, customer text, amount int) LANGUAGE sql AS $$ SELECT id, customer, amount FROM orders $$;
OUTPUT
DROP FUNCTION
CREATE FUNCTION
After the fixrun as lab_a
SELECT * FROM order_list() ORDER BY id;
✓ SUCCESS
 id | customer | amount
----+----------+--------
  1 | Kim      |    100
  2 | Lee      |     70
(2 rows)

03Input parameter renamed (amount → p_amount)

add_tax(amount int) was redefined as add_tax(p_amount int) with CREATE OR REPLACE. It failed with cannot change name of input parameter "amount", also 42P13.

pg_get_function_arguments showed amount integer. After DROP FUNCTION add_tax(int) and CREATE FUNCTION with the new name, add_tax(1000) returned 1100.

Setup
Setuprun as lab_a
CREATE FUNCTION add_tax(amount int) RETURNS int LANGUAGE sql AS $$ SELECT amount * 11 / 10 $$;
OUTPUT
CREATE FUNCTION
Reproducerun as lab_a
CREATE OR REPLACE FUNCTION add_tax(p_amount int) RETURNS int LANGUAGE sql AS $$ SELECT p_amount * 11 / 10 $$;
✕ ERROR
ERROR:  42P13: cannot change name of input parameter "amount"
HINT:  Use DROP FUNCTION add_tax(integer) first.
Checkrun as lab_a
SELECT pg_get_function_arguments(oid) AS arguments FROM pg_proc WHERE proname = 'add_tax';
OUTPUT
   arguments
----------------
 amount integer
(1 row)
Fixrun as lab_a
DROP FUNCTION add_tax(int);
CREATE FUNCTION add_tax(p_amount int) RETURNS int LANGUAGE sql AS $$ SELECT p_amount * 11 / 10 $$;
OUTPUT
DROP FUNCTION
CREATE FUNCTION
After the fixrun as lab_a
SELECT add_tax(1000);
✓ SUCCESS
 add_tax
---------
    1100
(1 row)

FAQ

How do I change the return type of a PostgreSQL function?

Drop it with its argument list and create it again: DROP FUNCTION order_total(int); then CREATE FUNCTION .... CREATE OR REPLACE cannot do it.

Why does adding a column to RETURNS TABLE fail?

RETURNS TABLE columns are OUT parameters; changing them changes the row type, which the DETAIL line reports as different.

Can I rename a function parameter with CREATE OR REPLACE?

No. Renaming an input parameter gives cannot change name of input parameter. Drop and create the function.

How do I see the current return type and parameters?

SELECT oid::regprocedure, prorettype::regtype FROM pg_proc WHERE proname = '...' and pg_get_function_arguments(oid) show them.

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