अगला कदम... (मेरे पिछले उत्तर की निरंतरता)।
फ़ंक्शन save_views(ऑब्जेक्टनाम टेक्स्ट) ऑब्जेक्टनाम . के आधार पर दृश्य संग्रहीत करता है (देखें या तालिका) तालिका में सहेजे गए_दृश्य ।
फ़ंक्शन restore_views() तालिका से दृश्य पुनर्स्थापित करता है सहेजे गए_दृश्य .
create or replace function save_views_oid(objectid oid)
returns void language plpgsql as $$
declare
r record;
begin
for r in
select distinct c.oid, c.relname, n.nspname
from pg_depend d
join pg_rewrite w on w.oid = d.objid
join pg_class c on c.oid = w.ev_class
join pg_namespace n on n.oid = c.relnamespace
where d.refclassid = 'pg_class'::regclass
and d.classid = 'pg_rewrite'::regclass
and d.refobjid = objectid
and c.oid <> objectid
loop
insert into saved_views values (
'CREATE VIEW ' || r.nspname || '.' || r.relname ||
' AS ' || pg_get_viewdef(r.oid, 'f'));
perform save_views_oid(r.oid);
end loop;
end; $$;
create or replace function save_views(objectname text)
returns void language plpgsql as $$
begin
create table if not exists saved_views(viewbody text);
truncate saved_views;
perform save_views_oid(objectname::regclass);
end; $$;
create or replace function restore_views()
returns void language plpgsql as $$
declare
viewtext text;
begin
for viewtext in
select viewbody from saved_views
loop
execute viewtext;
end loop;
drop table saved_views;
end; $$;
परीक्षण:
select save_views('my_view'); -- may be save_views('my_schema.my_view');
select * from saved_views;
उपयोग करें:
select save_views('my_view');
drop view my_view cascade;
create view my_view as ...
select restore_views();