실제 DB 검증PostgreSQL 17.12에서 재현·해결 확인 · 2026-11-29 · 컨테이너 이미지 postgres:17
CREATE OR REPLACE FUNCTION은 이름과 인수 타입이 같은 함수를 바꾸지만, 바꿀 수 있는 건 본문과 일부 옵션뿐이에요. 반환형, RETURNS TABLE 컬럼, 입력 매개변수 이름을 바꾸면 42P13과 함께 Use DROP FUNCTION ... first. 힌트가 나와요.
아래 3가지가 각각의 경우예요. 모두 힌트대로 인수 목록을 붙여 DROP FUNCTION 한 뒤 CREATE FUNCTION으로 다시 만들어 해결했어요.
01반환형을 int에서 numeric으로 변경
order_total(int)는 int를 돌려줬어요. CREATE OR REPLACE FUNCTION order_total(p_id int) RETURNS numeric ...이 cannot change return type of existing function과 HINT: Use DROP FUNCTION order_total(integer) first.로 실패했어요.
pg_proc에서 지금 반환형이 integer인 걸 확인하고, DROP FUNCTION order_total(int) 후 RETURNS numeric으로 CREATE FUNCTION 하니 SELECT order_total(1)이 1050.50을 돌려줬어요.
사전 준비
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)02RETURNS TABLE에 컬럼 추가
order_list()는 TABLE (id, amount)를 돌려줬어요. CREATE OR REPLACE로 RETURNS TABLE에 customer를 넣으니 같은 메시지에 DETAIL: Row type defined by OUT parameters is different.가 붙어 실패했어요.
RETURNS TABLE 컬럼은 OUT 매개변수라서 바꾸면 반환형이 바뀌는 거예요. DROP FUNCTION order_list() 후 CREATE FUNCTION 하니 Kim, Lee의 id, customer, amount가 나왔어요.
사전 준비
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)03입력 매개변수 이름 변경 (amount → p_amount)
add_tax(amount int)를 CREATE OR REPLACE로 add_tax(p_amount int)로 다시 정의하니 cannot change name of input parameter "amount"(이것도 42P13)로 실패했어요.
pg_get_function_arguments로 amount integer를 확인하고, DROP FUNCTION add_tax(int) 후 새 이름으로 CREATE FUNCTION 하니 add_tax(1000)이 1100을 돌려줬어요.
사전 준비
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)자주 묻는 질문
PostgreSQL 함수 반환형은 어떻게 바꿔요?
인수 목록을 붙여 지우고 다시 만들어요. DROP FUNCTION order_total(int); 다음 CREATE FUNCTION .... CREATE OR REPLACE로는 안 돼요.
RETURNS TABLE에 컬럼 추가가 왜 안 돼요?
RETURNS TABLE 컬럼은 OUT 매개변수라서, 바꾸면 행 타입이 달라져요. DETAIL 줄이 그걸 알려 줘요.
CREATE OR REPLACE로 매개변수 이름을 바꿀 수 있나요?
없어요. 입력 매개변수 이름을 바꾸면 cannot change name of input parameter가 나요. 지우고 다시 만들어요.
지금 반환형과 매개변수는 어떻게 봐요?
SELECT oid::regprocedure, prorettype::regtype FROM pg_proc WHERE proname = '...'와 pg_get_function_arguments(oid)로 볼 수 있어요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 역할(같은 이름의 스키마 소유), postgres는 슈퍼유저예요. PostgreSQL 17.12