LoginSignup
7
0

More than 5 years have passed since last update.

DROP FUNCTIONの小さいけど嬉しい改善

Last updated at Posted at 2017-12-10

はじめに

にゃーん
この記事は、PostgreSQL 10全部ぬこ Advent Calendar 2017 の11日目のエントリです。

DROP FUNCTIONで複数の関数を削除

たとえば、こんな複数の関数が登録されているとする。
(https://github.com/nuko-yokohama/pg_scripts)

func=# \df
                                    List of functions
 Schema |   Name   | Result data type |           Argument data types           |  Type  
--------+----------+------------------+-----------------------------------------+--------
 public | tategaki | text             | src text                                | normal
 public | tategaki | text             | src text, width integer, height integer | normal
(2 rows)

PostgreSQL 9.6まで

PostgreSQL 9.6までは複数のSQL関数を削除する場合、SQL関数毎にDROP FUNCTIONを実行する必要があった。

func=# DROP FUNCTION tategaki(text), tategaki(text,int,int);
ERROR:  syntax error at or near ","
LINE 1: DROP FUNCTION tategaki(text), tategaki(text,int,int);
                                    ^
func=# DROP FUNCTION tategaki (text);
DROP FUNCTION
func=# DROP FUNCTION tategaki (text,int,int);
DROP FUNCTION

PostgreSQL 10から

PostgreSQL 10からは1つのDROP FUNCTIONで複数のSQL関数を削除できるようになった。

func=# DROP FUNCTION tategaki(text), tategaki(text,int,int);
DROP FUNCTION

DROP FUNCTIONで引数指定なしでSQL関数を削除

たとえば、こんな関数を登録しておく。

func=# CREATE FUNCTION my_add(int, int) RETURNS int AS $$ SELECT $1 + $2 $$ LANGUAGE SQL;
CREATE FUNCTION
func=# \df
                         List of functions
 Schema |  Name  | Result data type | Argument data types |  Type  
--------+--------+------------------+---------------------+--------
 public | my_add | integer          | integer, integer    | normal
(1 row)

PostgreSQL 9.6まで

PostgreSQL 9.6までは、関数名だけでなく、関数引数も合わせて指定しないとSQL関数は削除できなかった。

func=# DROP FUNCTION my_add;
ERROR:  syntax error at or near ";"
LINE 1: DROP FUNCTION my_add;
                            ^
func=# DROP FUNCTION my_add(int, int);
DROP FUNCTION

PostgreSQL 10から

PostgreSQL 10からはスキーマ内に同一名称の関数がない場合には、関数名だけで削除が可能になった。
(DROP FUNCTIONだけではなく、引数を持つオブジェクトのDROPコマンド全般にそうなったっぽい)

func=# DROP FUNCTION my_add;
DROP FUNCTION

ただし、同一名称(で引数が異なる)SQL関数が複数存在する場合には、従来どおり引数も付与してDROP FUNCTIONを発行する必要がある。

func=# \df
                                    List of functions
 Schema |   Name   | Result data type |           Argument data types           |  Type  
--------+----------+------------------+-----------------------------------------+--------
 public | tategaki | text             | src text                                | normal
 public | tategaki | text             | src text, width integer, height integer | normal
(2 rows)

func=# DROP FUNCTION tategaki ;
ERROR:  function name "tategaki" is not unique
HINT:  Specify the argument list to select the function unambiguously.

同名で異なる引数の関数群を一撃でDROPはできないっぽい。残念。

おわりに

参考:該当するリリースノート

本エントリに関連するPostgreSQL 10リリースノートの記載です。

E.2.3.4. Utility Commands

  • Allow the specification of a function name without arguments in DDL commands, if it is unique (Peter Eisentraut)
  • Allow multiple functions, operators, and aggregates to be dropped with a single DROP command (Peter Eisentraut)
7
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
7
0