微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 如何获取函数参数列表(所以我可以放一个函数)

我想让sql在Postgresql删除一个函数.我从pg_proc写了DROP FUNCTION和get函数名.这没有问题.但是如果我留下空白参数,它不会丢弃该功能.

我检查了手册,然后写入,然后我必须使用其参数来标识函数,例如DROP FUNCTION some_func(text,integer)不仅仅是DROP FUNCTION some_func.

在哪里可以找到参数?在pg_proc表中的函数行中没有参数.那么如何才能让sql去掉函数呢?

Postgres有一个专门的功能为此目的. Per documentation:

pg_get_function_identity_arguments(func_oid) … get argument list to identify a function (without default values)

pg_get_function_identity_arguments returns the argument list
necessary to identify a function,in the form it would need to appear
in within ALTER FUNCTION,for instance. This form omits default values.

使用该方法,以下查询生成DDL语句以删除搜索词匹配的函数

SELECT 'DROP '
    || CASE WHEN p.proisagg THEN 'AGGREGATE ' ELSE 'FUNCTION ' END
    || quote_ident(n.nspname) || '.' || quote_ident(p.proname) || '(' 
    || pg_catalog.pg_get_function_identity_arguments(p.oid) || ');' AS stmt
FROM   pg_catalog.pg_proc p
JOIN   pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE  n.nspname = 'public'                     -- schema name (optional)
AND    p.proname ILIKE 'crosstab%'              -- function name
-- AND pg_catalog.pg_function_is_visible(p.oid) -- function visible to user
ORDER  BY 1;

返回:

stmt
---------------------------------------------------
 DROP FUNCTION public.dblink(text);
 DROP FUNCTION public.dblink(text,boolean);
 DROP FUNCTION public.dblink(text,text);
 DROP FUNCTION public.dblink(text,text,boolean);

在该示例中找到四个匹配,因为dblink使用overloaded functions.有选择地运行DDL语句!

原文地址:https://www.jb51.cc/postgresql/192565.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐