0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PostgreSQL 10の関数名指定の変更点

Last updated at Posted at 2020-09-30

CentOS8のデフォルトでインストールされるPostgreSQLが10(手元の環境を見たら、10.14でした)になっていますが、
関数名の指定方法が、9.6以前と変わった点があり、その点でちょっとだけハマったので、書いておきます。

PostgreSQL 10で、以下の変更点があります。

唯一であるならDDLコマンドで引数無しに関数名の指定を可能にしました。 (Peter Eisentraut)
例えば、その名前をもつ関数が一つだけしかないなら、引数なしで関数名だけのDROP FUNCTIONが可能です。 この振る舞いはSQL標準で必要とされています。

以下、変更点が書かれているページです。
https://www.postgresql.jp/document/10/html/release-10.html#id-1.11.6.10.5

関数名の指定で、その関数がユニークであるなら、()が省略できるようになりました、ということです。
※引数の有無関係なく、関数名がユニークな場合、引数があっても()が省略できました。

以下、実際にこの変更点に関する部分を試した内容です。

CREATE OR REPLACE FUNCTION myfunc(i integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
DECLARE
BEGIN
	
	RETURN;
END
$function$
;

-- PostgreSQL 10は、成功する
-- PostgreSQL 9.6以前の場合は、ERROR:  syntax error at or near "owner"
alter function myfunc owner to myname;


CREATE OR REPLACE FUNCTION myfunc(v text)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
DECLARE
BEGIN
	
	RETURN;
END
$function$
;

-- myfunc(i integer); と、myfunc(v text);がある場合、PostgreSQL 10でも失敗する
-- エラーメッセージは、ERROR: function name "myfunc" is not unique
alter function myfunc owner to myname;

で、ハマった点として、CentOS7(PostgreSQL 9系)とCentOS8を使って作業をしていて、CentOS8でクエリを実行して問題なかったのに、CentOS7だとクエリが通らない、という状況に陥って、なんで?ってなった次第です。はい。

新しいバージョンだけ使っていくことが確定しているなら良いですが、9系もまだ使う、という場合は、注意が必要ですね。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?