PostgreSQL:使用information_scheme删除函数

时间:2017-04-10 07:44:32

标签: postgresql function information-schema

我了解到我可以使用相同的名称选择我的所有函数:

select *
from information_schema.routines
where routine_type='FUNCTION' and routine_name='test';

然而,显然这是一种观点,当我尝试时:

delete
from information_schema.routines
where routine_type='FUNCTION' and routine_name='test';

我收到的消息是我无法从视图中删除。

我这种方法的最初原因是因为我想要一个懒惰的drop函数,我不必命名参数 - 我正在开发一些新函数,并且在这种状态下,参数列表将是可变的。 / p>

我可以使用此技术删除具有相同名称的函数吗?怎么样?

1 个答案:

答案 0 :(得分:1)

从不搞乱系统目录。

您可以创建一个小脚本来执行您想要的操作:

do
$$
declare
  proc_rec record;
  drop_ddl   text;
begin
  for proc_rec in SELECT n.nspname, p.proname, 
                         pg_get_function_arguments(p.oid) as args
                  FROM pg_catalog.pg_proc p  
                    JOIN pg_catalog.pg_namespace n on p.pronamespace = n.oid 
                  where n.nspname = 'public' -- don't forget the function's schema!
                    and p.proname = 'test'
  loop 
    drop_ddl := format('drop function %I.%I(%s)', proc_rec.nspname, proc_rec.proname, proc_rec.args);
    raise notice '%', drop_ddl;
    -- execute drop_ddl; -- uncomment to actually drop the function
  end loop;
end;
$$

如果您需要更频繁地执行此操作,可以将该代码放入函数中。