当前位置:   article > 正文

pgsql调整表字段的长度,表被视图或者存储过程引用_postgresql修改数据库字段 长度sql

postgresql修改数据库字段 长度sql

本人遇到pgsql需要调整数据库字段长度,但是该表被很多视图和存储过程引用怎么办?一个一个删除修改在还原不现实,应为应用的地方比较多甚至其他服务的数据库也会应用,这时候有我们可以利用存储过程。一共分三步

1.先调用第一个存储过程(递归查询所有的视图及视图权限,并入表)

2.执行alert语句(修改字段长度的语句)

3.调用第二个存储过程(读表还原视图,删除表数据)

第一个存储过程(递归查询所有的视图及视图权限,并入表)

  1. CREATE OR REPLACE FUNCTION public.deps_save_and_drop_dependencies(p_view_schema name, p_view_name name)
  2. RETURNS void
  3. LANGUAGE plpgsql
  4. AS $function$
  5. declare
  6. v_curr record;
  7. begin
  8. for v_curr in
  9. (
  10. select obj_schema, obj_name, obj_type from
  11. (
  12. with recursive recursive_deps(obj_schema, obj_name, obj_type, depth) as
  13. (
  14. select p_view_schema, p_view_name, null::char, 0
  15. union
  16. select dep_schema::name, dep_name::name, dep_type::char, recursive_deps.depth + 1 from
  17. (
  18. select ref_nsp.nspname ref_schema, ref_cl.relname ref_name,
  19. rwr_cl.relkind dep_type,
  20. rwr_nsp.nspname dep_schema,
  21. rwr_cl.relname dep_name
  22. from pg_depend dep
  23. join pg_class ref_cl on dep.refobjid = ref_cl.oid
  24. join pg_namespace ref_nsp on ref_cl.relnamespace = ref_nsp.oid
  25. join pg_rewrite rwr on dep.objid = rwr.oid
  26. join pg_class rwr_cl on rwr.ev_class = rwr_cl.oid
  27. join pg_namespace rwr_nsp on rwr_cl.relnamespace = rwr_nsp.oid
  28. where dep.deptype = 'n'
  29. and dep.classid = 'pg_rewrite'::regclass
  30. ) deps
  31. join recursive_deps on deps.ref_schema = recursive_deps.obj_schema and deps.ref_name = recursive_deps.obj_name
  32. where (deps.ref_schema != deps.dep_schema or deps.ref_name != deps.dep_name)
  33. )
  34. select obj_schema, obj_name, obj_type, depth
  35. from recursive_deps
  36. where depth > 0
  37. ) t
  38. group by obj_schema, obj_name, obj_type
  39. order by max(depth) desc
  40. ) loop
  41. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  42. select distinct p_view_schema, p_view_name, indexdef
  43. from pg_indexes
  44. where schemaname = v_curr.obj_schema
  45. and tablename = v_curr.obj_name;
  46. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  47. select distinct tablename, rulename, definition
  48. from pg_rules
  49. where schemaname = v_curr.obj_schema
  50. and tablename = v_curr.obj_name;
  51. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  52. select p_view_schema, p_view_name, 'COMMENT ON ' ||
  53. case
  54. when c.relkind = 'v' then 'VIEW'
  55. when c.relkind = 'm' then 'MATERIALIZED VIEW'
  56. else ''
  57. end
  58. || ' ' || n.nspname || '.' || c.relname || ' IS ''' || replace(d.description, '''', '''''') || ''';'
  59. from pg_class c
  60. join pg_namespace n on n.oid = c.relnamespace
  61. join pg_description d on d.objoid = c.oid and d.objsubid = 0
  62. where n.nspname = v_curr.obj_schema and c.relname = v_curr.obj_name and d.description is not null;
  63. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  64. select p_view_schema, p_view_name, 'COMMENT ON COLUMN ' || n.nspname || '.' || c.relname || '.' || a.attname || ' IS ''' || replace(d.description, '''', '''''') || ''';'
  65. from pg_class c
  66. join pg_attribute a on c.oid = a.attrelid
  67. join pg_namespace n on n.oid = c.relnamespace
  68. join pg_description d on d.objoid = c.oid and d.objsubid = a.attnum
  69. where n.nspname = v_curr.obj_schema and c.relname = v_curr.obj_name and d.description is not null;
  70. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  71. select p_view_schema, p_view_name, 'GRANT ' || privilege_type || ' ON ' || table_schema || '.' || quote_ident(table_name) || ' TO ' || grantee
  72. from information_schema.role_table_grants
  73. where table_schema = v_curr.obj_schema and table_name = v_curr.obj_name;
  74. if v_curr.obj_type = 'v' then
  75. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  76. select p_view_schema, p_view_name, 'CREATE VIEW ' || v_curr.obj_schema || '.' || quote_ident(v_curr.obj_name) || ' AS ' || view_definition
  77. from information_schema.views
  78. where table_schema = v_curr.obj_schema and table_name = v_curr.obj_name;
  79. elsif v_curr.obj_type = 'm' then
  80. insert into deps_saved_ddl(deps_view_schema, deps_view_name, deps_ddl_to_run)
  81. select p_view_schema, p_view_name, 'CREATE MATERIALIZED VIEW ' || v_curr.obj_schema || '.' || quote_ident(v_curr.obj_name) || ' AS ' || definition
  82. from pg_matviews
  83. where schemaname = v_curr.obj_schema and matviewname = v_curr.obj_name;
  84. end if;
  85. execute 'DROP ' ||
  86. case
  87. when v_curr.obj_type = 'v' then 'VIEW'
  88. when v_curr.obj_type = 'm' then 'MATERIALIZED VIEW'
  89. end
  90. || ' ' || v_curr.obj_schema || '.' || quote_ident(v_curr.obj_name);
  91. end loop;
  92. end;
  93. $function$
  94. ;

第二个存储过程(读表还原视图,删除表数据)

  1. CREATE OR REPLACE FUNCTION public.deps_restore_dependencies(p_view_schema name, p_view_name name)
  2. RETURNS void
  3. LANGUAGE plpgsql
  4. AS $function$
  5. declare
  6. v_curr record;
  7. begin
  8. for v_curr in
  9. (
  10. select deps_ddl_to_run
  11. from deps_saved_ddl
  12. where deps_view_schema = p_view_schema and deps_view_name = p_view_name
  13. order by deps_id desc
  14. ) loop
  15. execute v_curr.deps_ddl_to_run;
  16. end loop;
  17. delete from deps_saved_ddl
  18. where deps_view_schema = p_view_schema and deps_view_name = p_view_name;
  19. end;
  20. $function$
  21. ;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/755125
推荐阅读
相关标签
  

闽ICP备14008679号