Dependency Tracking function

后端 未结 2 1902
野性不改
野性不改 2021-01-03 09:40

I just wonder if anyone knows how to automatize views creation after running DROP ... CASCADE? Now I\'m trying to drop view at first with classic DROP VIE

2条回答
  •  我在风中等你
    2021-01-03 10:14

    Table pg_depend contains all necessary informations, but it is not so easy to interpret them. Here you have sketch recursive function to retrieve dependencies of pg_class object in text format. You can tune up the function to your needs (and show us results:).

    create or replace function dependency
        (class_id regclass, obj_id regclass, obj_subid integer, dep_type "char")
    returns setof text language plpgsql as $$
    declare
        r record;
    begin
        return query 
            select pg_describe_object(class_id, obj_id, obj_subid)
                || ' ('|| dep_type|| ')';
        for r in
            select classid, objid, objsubid, deptype
            from pg_depend
            where class_id = refclassid
            and obj_id = refobjid
            and (obj_subid = refobjsubid or obj_subid = 0)
        loop
            return query select dependency(r.classid, r.objid, r.objsubid, r.deptype);
        end loop;
    end; $$;
    
    use:
    
    select dependency('pg_class'::regclass, 'my_view'::regclass, 0, ' ');
    select dependency('pg_class'::regclass, 'my_table'::regclass, 0, ' ');
    

提交回复
热议问题