PostgreSQL:使用 information_scheme 删除函数
PostgreSQL: dropping functions using information_scheme
我了解到我可以 select 使用相同名称的所有函数:
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';
我收到无法从视图中删除的消息。
我最初采用这种方法的原因是因为我想要一个不必命名参数的惰性删除函数 — 我正在开发一些新函数,并且在这种情况下,参数列表将是可变的.
我可以使用这种技术删除同名函数吗?怎么样?
从不 乱用系统目录。
您可以创建一个小脚本来执行您想要的操作:
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;
$$
如果您需要更频繁地这样做,您可以将该代码放入一个函数中。
我了解到我可以 select 使用相同名称的所有函数:
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';
我收到无法从视图中删除的消息。
我最初采用这种方法的原因是因为我想要一个不必命名参数的惰性删除函数 — 我正在开发一些新函数,并且在这种情况下,参数列表将是可变的.
我可以使用这种技术删除同名函数吗?怎么样?
从不 乱用系统目录。
您可以创建一个小脚本来执行您想要的操作:
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;
$$
如果您需要更频繁地这样做,您可以将该代码放入一个函数中。