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
CREATE FUNCTION order_total(p_id int) RETURNS int LANGUAGE sql AS $$ SELECT 1050 $$;CREATE FUNCTIONCREATE OR REPLACE FUNCTION order_total(p_id int) RETURNS numeric LANGUAGE sql AS $$ SELECT 1050.50 $$;ERROR: 42P13: cannot change return type of existing function
HINT: Use DROP FUNCTION order_total(integer) first.SELECT oid::regprocedure AS signature, prorettype::regtype AS returns FROM pg_proc WHERE proname = 'order_total'; signature | returns
----------------------+---------
order_total(integer) | integer
(1 row)DROP FUNCTION order_total(int);
CREATE FUNCTION order_total(p_id int) RETURNS numeric LANGUAGE sql AS $$ SELECT 1050.50 $$;DROP FUNCTION
CREATE FUNCTIONSELECT order_total(1); 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
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 $$;CREATE TABLE
INSERT 0 2
CREATE FUNCTIONCREATE OR REPLACE FUNCTION order_list() RETURNS TABLE (id int, customer text, amount int) LANGUAGE sql AS $$ SELECT id, customer, amount FROM orders $$;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.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 $$;DROP FUNCTION
CREATE FUNCTIONSELECT * FROM order_list() ORDER BY id; 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
CREATE FUNCTION add_tax(amount int) RETURNS int LANGUAGE sql AS $$ SELECT amount * 11 / 10 $$;CREATE FUNCTIONCREATE OR REPLACE FUNCTION add_tax(p_amount int) RETURNS int LANGUAGE sql AS $$ SELECT p_amount * 11 / 10 $$;ERROR: 42P13: cannot change name of input parameter "amount"
HINT: Use DROP FUNCTION add_tax(integer) first.SELECT pg_get_function_arguments(oid) AS arguments FROM pg_proc WHERE proname = 'add_tax'; arguments
----------------
amount integer
(1 row)DROP FUNCTION add_tax(int);
CREATE FUNCTION add_tax(p_amount int) RETURNS int LANGUAGE sql AS $$ SELECT p_amount * 11 / 10 $$;DROP FUNCTION
CREATE FUNCTIONSELECT add_tax(1000); 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