在不知道参数数量/类型的情况下删除函数?
我将所有函数保存在一个带有 'CREATE OR REPLACE FUNCTION somefunction'
的文本文件中。
因此,如果我添加或更改某些函数,我只需将文件提供给 psql。
现在,如果我向现有函数添加或删除参数,它会创建一个具有相同名称的重载,并且要删除原始函数,我需要按确切的顺序键入所有参数类型,这有点乏味。
是否可以使用某种通配符来 DROP
具有给定名称的所有函数,以便我可以将 DROP FUNCTION
行添加到文件顶部?
I keep all my functions in a text file with 'CREATE OR REPLACE FUNCTION somefunction'
.
So if I add or change some function I just feed the file to psql.
Now if I add or remove parameters to an existing function, it creates an overload with the same name and to delete the original I need type in all the parameter types in the exact order which is kind of tedious.
Is there some kind of wildcard I can use to DROP
all functions with a given name so I can just add DROP FUNCTION
lines to the top of my file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
基本查询
此查询创建所有必需的 DDL 语句:
输出:
检查合理性后执行命令。
传递函数名称区分大小写且不添加双引号以与
pg_proc.proname
匹配。转换为对象标识符类型
regprocedure
(oid::regprocedure
),然后隐式地生成带有参数类型的函数名称双引号和模式限定根据当前的search_path
(在需要的地方)。 不可能进行 SQL 注入。pg_function_is_visible(oid)
将选择范围限制为当前search_path
(“可见”)。您可能想要也可能不想要这个。如果多个模式中有多个同名的函数,或者具有不同函数参数的重载函数,则所有这些函数都将单独列出。您可能希望限制为特定模式或特定函数参数。
相关:
函数
您可以围绕此构建一个 PL/pgSQL 函数,以使用
执行
。小心!它会删除您的函数!
调用:
如果没有引发异常,该函数将返回找到并删除的函数的数量。
0
如果没有找到。进一步阅读:
< em>对于 9.1 之前的 Postgres 版本或使用
regproc
的旧版本函数以及pg_get_function_identity_arguments(oid)
检查此答案的编辑历史记录。Basic query
This query creates all necessary DDL statements:
Output:
Execute the commands after checking plausibility.
Pass the function name case-sensitive and with no added double-quotes to match against
pg_proc.proname
.The cast to the object identifier type
regprocedure
(oid::regprocedure
), and then totext
implicitly, produces function names with argument types, automatically double-quoted and schema-qualified according to the currentsearch_path
where needed. No SQL injection possible.pg_function_is_visible(oid)
restricts the selection to functions in the currentsearch_path
("visible"). You may or may not want this.If you have multiple functions of the same name in multiple schemas, or overloaded functions with various function arguments, all of those will be listed separately. You may want to restrict to specific schema(s) or specific function parameter(s).
Related:
Function
You can build a PL/pgSQL function around this to execute the statements immediately with
EXECUTE
.Careful! It drops your functions!
Call:
The function returns the number of functions found and dropped if no exceptions are raised.
0
if none were found.Further reading:
For Postgres versions older than 9.1 or older variants of the function using
regproc
andpg_get_function_identity_arguments(oid)
check the edit history of this answer.您需要编写一个函数,该函数采用函数名称,并从
information_schema
中查找每个重载及其参数类型,然后为每个重载构建并执行一个DROP
。编辑:这比我想象的要困难得多。看起来
information_schema
没有在其routines
目录中保留必要的参数信息。因此,您需要使用 PostgreSQL 的补充表pg_proc
和pg_type
:我在重载函数上成功测试了这一点。它的组装速度相当快,但作为一个实用函数工作得很好。我建议在实践中使用它之前进行更多测试,以防我忽略了一些东西。
You would need to write a function that took the function name, and looked up each overload with its parameter types from
information_schema
, then built and executed aDROP
for each one.EDIT: This turned out to be a lot harder than I thought. It looks like
information_schema
doesn't keep the necessary parameter information in itsroutines
catalog. So you need to use PostgreSQL's supplementary tablespg_proc
andpg_type
:I successfully tested this on an overloaded function. It was thrown together pretty fast, but works fine as a utility function. I would recommend testing more before using it in practice, in case I overlooked something.
改进原始答案以考虑
架构
,即。schema.my_function_name
,Improving original answer in order to take
schema
into account, ie.schema.my_function_name
,埃尔文答案的稍微增强版。另外支持以下
复制/粘贴代码:
Slightly enhanced version of Erwin's answer. Additionally supports following
Code for copy/paste:
以下是我在 @Сухой27 解决方案之上构建的查询,该解决方案生成用于删除架构中所有存储函数的 SQL 语句:
Here is the query I built on top of @Сухой27 solution that generates sql statements for dropping all the stored functions in a schema:
当根据名称删除过程时,如果存在多个同名但参数不同的过程,pgsql 会生成错误。因此,如果您想删除单个过程而不影响其他过程,则只需使用以下查询即可。
pgsql generates an error if there exists more than one procedure with the same name but different arguments when the procedure is deleted according to its name. Thus if you want to delete a single procedure without affecting others then simply use the following query.
从 Postgres 10 开始,您只能按名称删除函数,只要名称对于其架构是唯一的。只需将以下声明放在函数文件的顶部:
文档此处。
As of Postgres 10 you can drop functions by name only, as long as the names are unique to their schema. Just place the following declaration at the top of your function file:
Documentation here.