2017-05-27 65 views

回答

1

下降的觀點:

create function drop_views_by_owner(owner regrole, do_drop boolean default true) 
returns setof text language plpgsql as $$ 
declare r text; 
begin 
    for r in 
     select format('%s.%s', nspname, relname) 
     from pg_class c 
     join pg_namespace n on n.oid = relnamespace 
     where c.relowner = $1 
     and relkind = 'v' 
    loop 
     if do_drop then 
      execute format('drop view %s', r); 
     end if; 
     return next r; 
    end loop; 
end $$; 

刪除功能:

create function drop_functions_by_owner(owner regrole, do_drop boolean default true) 
returns setof text language plpgsql as $$ 
declare r text; 
begin 
    for r in 
     select format('%s.%s(%s)', 
      nspname, proname, pg_get_function_identity_arguments(p.oid)) 
     from pg_proc p 
     join pg_namespace n on n.oid = pronamespace 
     where p.proowner = $1 
    loop 
     if do_drop then 
      execute format('drop function %s', r); 
     end if; 
     return next r; 
    end loop; 
end $$; 

如果第二個參數是false功能不下降的對象:

select drop_views_by_owner('newuser');   -- list views and drop them 
select drop_views_by_owner('newuser', false);  -- only list views  

select drop_functions_by_owner('newuser');  -- list functions and drop them 
select drop_functions_by_owner('newuser', false); -- only list functions   
+0

謝謝你的努力,但我真的找/希望PostgreSQL的命令。 – William

0

如果用戶只擁有意見和功能,你可以簡單地:

DROP OWNED BY your_role; 

您必須爲羣集中您用戶擁有對象的每個數據庫執行此操作。

您可能會發現這個文檔有用:https://www.postgresql.org/docs/current/static/role-removal.html

+0

看起來很危險。它可能會有不可預見的後果。 – William

+0

這確實很危險,但是如果您需要刪除用戶擁有的所有對象以便能夠放下用戶,它可能變得非常方便。 – Arkhena