删除命名空间中的所有函数? (执行生成的DDL命令?)
我正在尝试编写一个命令来删除命名空间中的所有函数。我已经找到了一个将生成删除函数脚本的命令:
SELECT 'DROP FUNCTION ' || ns.nspname || '.' || proname || '('
|| oidvectortypes(proargtypes) || ');'
FROM pg_proc INNER JOIN pg_namespace ns ON (pg_proc.pronamespace = ns.oid)
WHERE ns.nspname = 'public' order by proname;
来源:http://www.postgresonline.com/journal/archives/74-How-to-delete-many-functions.html
这将生成类似以下内容:
?column?
------------------------------------------
DROP FUNCTION public.function1(bigint);
DROP FUNCTION public.function2();
DROP FUNCTION public.function3(text);
但是,我无法弄清楚如何更改代码,以便实际上删除函数 - 而不是仅生成命令。
有什么想法吗?
I'm trying to write a command that will delete all functions in a namespace. I've already found a command that will generate the drop functions script:
SELECT 'DROP FUNCTION ' || ns.nspname || '.' || proname || '('
|| oidvectortypes(proargtypes) || ');'
FROM pg_proc INNER JOIN pg_namespace ns ON (pg_proc.pronamespace = ns.oid)
WHERE ns.nspname = 'public' order by proname;
Source: http://www.postgresonline.com/journal/archives/74-How-to-delete-many-functions.html
This will generate something like:
?column?
------------------------------------------
DROP FUNCTION public.function1(bigint);
DROP FUNCTION public.function2();
DROP FUNCTION public.function3(text);
However, I can't figure out how to change the code, so that the functions are actually deleted - as opposed to only generating the commands.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Postgres 11 中的系统目录发生了变化! (
prokind
而不是proisagg
)请参阅:可能如下所示:
调用显示:
调用删除:
要点
为此您需要动态 SQL。使用 plpgsql 函数 或 DO 语句 (PostgreSQL 9.0+) 与
EXECUTE
。注意函数
pg_get_function_identity_arguments()的使用
和pg_function_is_visible
。后者可以省略。这是一种保护措施,因此您不会删除当前用户的search_path
之外的函数。我添加了“安全模式”。仅当
$2 = 'del'
时才删除。否则只显示生成的 SQL。请注意,如果函数存在于您从中删除的架构中,则该函数将删除自身。
我还添加了
quote_ident()
来防范 SQLi。请考虑以下事项:CASCADE
来解决,但我在这里没有这样做,因为它使函数更加危险。相关:
The system catalogs changed in Postgres 11! (
prokind
instead ofproisagg
) See:Could look like this:
Call to show:
Call to delete:
Major points
You need dynamic SQL for that. Use a plpgsql function or a DO statement (PostgreSQL 9.0+) with
EXECUTE
.Note the use of the functions
pg_get_function_identity_arguments()
andpg_function_is_visible
. The latter can be omitted. It's a safeguard so you don't delete functions outside of the current user'ssearch_path
.I added a "safe mode". Only delete if
$2 = 'del'
. Else only show generated SQL.Be aware that the function will delete itself if it lives in the schema you delete from.
I also added
quote_ident()
to safeguard against SQLi. Consider the following:CASCADE
, but I did not do that here, since it makes the function more dangerous, yet.Related:
只需将查询的输出复制+粘贴到您的
psql
解释器中即可。它将运行您粘贴的所有命令。Just COPY+PASTE the output of your query, into your
psql
interpreter. It will run all commands you paste.对于生成一组命令的任何 SQL 表达式:
For any SQL expression that generates a set of commands:
我的版本没有存储过程
My Version without a stored procedure