PostgreSQL:如何在不指定参数的情况下 DROP FUNCTION IF EXISTS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30782925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:00:01  来源:igfitidea点击:

PostgreSQL: How to DROP FUNCTION IF EXISTS without specifying parameters?

postgresqlplpgsql

提问by Joseph Idziorek

I can successfully create a function as follows:

我可以成功创建一个函数,如下所示:

CREATE FUNCTION Foo(MY_Value INT) RETURNS INT
AS 'SELECT 2 + MY_Value'
LANGUAGE SQL

However, if I first want to check if the function exists and then drop it if I does, I must specify the following:

但是,如果我首先要检查该函数是否存在,然后在存在时删除它,则必须指定以下内容:

DROP FUNCTION IF EXISTS Foo(My_Value INT);

Without specifying the input parameters, the following returns an error stating "NOTICE: function foo() does not exist, skipping"

在不指定输入参数的情况下,以下内容将返回一个错误,指出“注意:函数 foo() 不存在,正在跳过”

DROP FUNCTION IF EXISTS Foo();

Similar to MySQL, is there a way to drop a FUNCTION in PostgreSQL without having to specify the parameters to the function? In other words, is there an equivalent for the following in MySQL statement (i.e., drop the stored procedure without specifying the input parameters)?

与 MySQL 类似,有没有办法在 PostgreSQL 中删除 FUNCTION 而不必指定函数的参数?换句话说,在 MySQL 语句中是否有以下等效项(即,在不指定输入参数的情况下删除存储过程)?

DROP PROCEDURE IF EXISTS Foo;

回答by klin

In Postgres functions can be overloaded, so parameters are necessary to distinguish overloaded functions. To unambiguously identify a function you can put only types of its parameters.

在 Postgres 中,函数是可以重载的,因此需要参数来区分重载的函数。要明确标识一个函数,您只能放置其参数的类型。

DROP FUNCTION IF EXISTS Foo(INT);

回答by Mark McKelvy

As of Postgres 10 you can drop functions by name only, as long as the names are unique to their schema.

从 Postgres 10 开始,您只能按名称删除函数,只要名称对于其架构是唯一的。

Example:

例子:

drop function if exists Foo;

Documentation here.

文档在这里