postgresql – 更改列忽略依赖视图

我有字符变化(20)类型的列列,我想将它增加到50
ALTER TABLE table ALTER COLUMN column TYPE character varying(50);

我收到一个错误,视图view_name取决于列“列”.我想知道如何在不丢弃和重新创建大约10个依赖视图的情况下更改列?

您可以在此 @L_301_0@找到您的问题的答案

Postgresql is very restrictive when it comes to modifying existing
objects. Very often when you try to ALTER TABLE or REPLACE VIEW it
tells you that you cannot do it,because there’s another object
(typically a view or materialized view),which depends on the one you
want to modify. It seems that the only solution is to DROP dependent
objects,make desired changes to the target object and then recreate
dropped objects.

It is tedious and cumbersome,because those dependent objects can have
further dependencies,which also may have other dependencies and so
on. I created 07001 which can help in such situations.

The usage is very simple – you just have to call:

select deps_save_and_drop_dependencies(p_schema_name,p_object_name);

You
have to pass two arguments: the name of the schema and the name of the
object in that schema. This object can be a table,a view or a
materialized view. The function will drop all views and materialized
views dependent on p_schema_name.p_object_name and save DDL which
restores them in a helper table.

When you want to restore those dropped objects (for example when you
are done modyfing p_schema_name.p_object_name),you just need to make
another simple call:

select deps_restore_dependencies(p_schema_name,p_object_name);

and the dropped objects will be recreated.

These functions take care about:

  • dependencies hierarchy
  • proper order of dropping and creating views/materialized views across hierarchy
  • restoring comments and grants on views/materialized views

Click 07002 for a working sqlfiddle example or check 07003 for a complete source code

相关文章

来源:http://www.postgres.cn/docs/11/ 4.1.1. 标识符和关键词 SQL标识符和关键词必须以一个...
来源:http://www.postgres.cn/docs/11/ 8.1. 数字类型 数字类型由2、4或8字节的整数以及4或8...
来源:http://www.postgres.cn/docs/11/ 5.1. 表基础 SQL并不保证表中行的顺序。当一个表被读...
来源:http://www.postgres.cn/docs/11/ 6.4. 从修改的行中返回数据 有时在修改行的操作过程中...
来源:http://www.postgres.cn/docs/11/ 13.2.1. 读已提交隔离级别 读已提交是PostgreSQL中的...
来源:http://www.postgres.cn/docs/11/ 9.7. 模式匹配 PostgreSQL提供了三种独立的实现模式匹...