実機検証済みPostgreSQL 17.12 で再現・解決を確認 · 2026-11-29 · コンテナイメージ postgres:17
PostgreSQL は名前が同じで引数の型が違う関数(オーバーロード)を作れます。呼び出しが複数の関数に同じように合うと、42725 function ... is not unique とヒント Could not choose a best candidate function. You might need to add explicit type casts. で止まります。名前だけで DROP FUNCTION すると function name "..." is not unique になります。
以下の3つは実務でよくあるパターンです。毎回まず SELECT oid::regprocedure FROM pg_proc WHERE proname = '...' で既存の関数シグネチャを確認しました。
01DEFAULT 引数を持つ新しい関数が既存の関数と重なる
order_tax(int) があるところに order_tax(int, rate int DEFAULT 10) を追加しました。SELECT order_tax(1000) は両方に合うため function order_tax(integer) is not unique で失敗しました。
pg_proc には order_tax(integer) と order_tax(integer,integer) がありました。DROP FUNCTION order_tax(int) で古い関数を削除すると、デフォルト付きの版だけが残り order_tax(1000) は 100 を返しました。
事前準備
CREATE FUNCTION order_tax(amount int) RETURNS int LANGUAGE sql AS $$ SELECT amount / 10 $$;
CREATE FUNCTION order_tax(amount int, rate int DEFAULT 10) RETURNS int LANGUAGE sql AS $$ SELECT amount * rate / 100 $$;CREATE FUNCTION
CREATE FUNCTIONSELECT order_tax(1000);ERROR: 42725: function order_tax(integer) is not unique
LINE 1: SELECT order_tax(1000);
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.SELECT oid::regprocedure AS signature FROM pg_proc WHERE proname = 'order_tax'; signature
----------------------------
order_tax(integer)
order_tax(integer,integer)
(2 rows)DROP FUNCTION order_tax(int);DROP FUNCTIONCREATE OR REPLACE で引数を追加したときによく起きます。引数リストが違うと置き換えではなく新しい関数が作られます。
SELECT order_tax(1000); order_tax
-----------
100
(1 row)02bigint 用と numeric 用の関数があり、整数リテラルで呼び出す
fmt_amount(bigint) と fmt_amount(numeric) がありました。リテラル 100 は integer でどちらにも変換できるため、SELECT fmt_amount(100) が 42725 で失敗しました。
引数をキャストすると1つに決まりました。fmt_amount(100::bigint) は bigint 100、fmt_amount(100::numeric) は numeric 100 を返しました。
事前準備
CREATE FUNCTION fmt_amount(v bigint) RETURNS text LANGUAGE sql AS $$ SELECT 'bigint ' || v $$;
CREATE FUNCTION fmt_amount(v numeric) RETURNS text LANGUAGE sql AS $$ SELECT 'numeric ' || v $$;CREATE FUNCTION
CREATE FUNCTIONSELECT fmt_amount(100);ERROR: 42725: function fmt_amount(integer) is not unique
LINE 1: SELECT fmt_amount(100);
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.SELECT oid::regprocedure AS signature FROM pg_proc WHERE proname = 'fmt_amount'; signature
---------------------
fmt_amount(bigint)
fmt_amount(numeric)
(2 rows)SELECT fmt_amount(100::bigint) AS a, fmt_amount(100::numeric) AS b; a | b
------------+-------------
bigint 100 | numeric 100
(1 row)03オーバーロードがあるのに名前だけで DROP FUNCTION
get_price(int) と get_price(text) がありました。DROP FUNCTION get_price; は function name "get_price" is not unique とヒント Specify the argument list to select the function unambiguously. で失敗しました。
DROP FUNCTION get_price(text) と引数の型を書くとその関数だけが削除され、pg_proc には get_price(integer) だけが残りました。
事前準備
CREATE FUNCTION get_price(id int) RETURNS int LANGUAGE sql AS $$ SELECT 100 $$;
CREATE FUNCTION get_price(code text) RETURNS int LANGUAGE sql AS $$ SELECT 200 $$;CREATE FUNCTION
CREATE FUNCTIONDROP FUNCTION get_price;ERROR: 42725: function name "get_price" is not unique
HINT: Specify the argument list to select the function unambiguously.SELECT oid::regprocedure AS signature FROM pg_proc WHERE proname = 'get_price'; signature
--------------------
get_price(integer)
get_price(text)
(2 rows)DROP FUNCTION get_price(text);
SELECT oid::regprocedure AS signature FROM pg_proc WHERE proname = 'get_price';DROP FUNCTION
signature
--------------------
get_price(integer)
(1 row)よくある質問
同じ名前の関数がいくつあるか確認するには?
SELECT oid::regprocedure FROM pg_proc WHERE proname = '関数名'; を実行します。1行が引数の型付きの関数1つです。
CREATE OR REPLACE したのに関数が2つになったのはなぜ?
CREATE OR REPLACE は引数の型が同じときだけ置き換えます。引数リストが違うとオーバーロードがもう1つ作られます。
関数を削除せずに解決するには?
1つだけに合うよう引数をキャストします。例: fmt_amount(100::numeric)。
DROP FUNCTION の function name is not unique とは?
その名前の関数が複数あるという意味です。DROP FUNCTION get_price(text); のように引数の型を書きます。
検証方法
シナリオごとに新しいDBユーザーを作成して開始します。スクリプトが事前準備 → エラー再現(エラーコードの一致を確認)→ 対処 → 元の文の再実行の順に実行し、表示している出力はDBが実際に返した結果をスクリプトがそのまま保存したものです。
lab_a・lab_b はテスト用の一般ロール(同名のスキーマを所有)、postgres はスーパーユーザーです。 PostgreSQL 17.12