Postgres 有一个专门用于此目的的函数。随 Postgres 8.4 引入。手册: https://www.postgresql.org/docs/current/functions-info.html#FUNCTIONS-INFO-CATALOG-TABLE
pg_get_function_identity_arguments(func_oid)
...获取参数列表来标识函数(没有默认值)...
pg_get_function_identity_arguments
返回参数列表
需要识别一个函数,以它需要出现的形式
在里面ALTER FUNCTION
, 例如。这种形式省略了默认值。
使用它(和format() https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-FORMAT,随 Postgres 9.1 引入),以下查询生成 DDL 语句以删除与您的搜索词匹配的函数:
SELECT format('DROP %s %I.%I(%s);'
, CASE WHEN p.proisagg THEN 'AGGREGATE' ELSE 'FUNCTION' END
, n.nspname
, 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 p.proname = 'dblink' -- function name
-- AND n.nspname = 'public' -- schema name (optional)
-- AND pg_catalog.pg_function_is_visible(p.oid) -- function visible to user
ORDER BY 1;
系统目录pg_proc
改变于Postgres 11. proisagg
被替换为prokind
,添加了真正的存储过程。你需要适应。看:
- 如何删除 PostgreSQL 中的所有函数? https://dba.stackexchange.com/a/122777/3684
Returns:
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 使用重载函数 https://www.postgresql.org/docs/current/xfunc-overload.html.
Run DROP
有选择地发言!
或者,您可以使用方便的强制转换到对象标识符类型regprocedure https://www.postgresql.org/docs/current/datatype-oid.html#DATATYPE-OID-TABLE它返回一个完整的函数签名,包括参数类型:
-- SET LOCAL search_path = ''; -- optional, to get all names schema-qualified
SELECT format('DROP %s %s;'
, CASE WHEN proisagg THEN 'AGGREGATE' ELSE 'FUNCTION' END
, oid::regprocedure
) AS stmt
FROM pg_catalog.pg_proc
WHERE proname = 'dblink' -- function name
ORDER BY 1;