我不想通过以下方式将列的大小从字符 (9) 更改为字符 (12)
ALTER TABLE product_based_award ALTER COLUMN name TYPE character(12);
但在内部此列由 MView 使用,因此不允许更改表格并给出以下错误。
ERROR: cannot alter type of a column used by a view or rule
我找到了两个解决方案,一个是删除 mview 并重新创建它,另一个是更新 pg_attribute。 但是我不能使用这两个选项,因为我们的数据库非常复杂,所以更新 pg_attribute 可能会导致问题并且也不能删除 mview。
那么有没有其他最好的方法来解决这个问题。
最佳答案
更新 pg_attribute
总是非常糟糕的主意
要减少用户等待新定义的时间(假设它加载数据需要很长时间),您可以使用:
让 s117 成为旧的:
t=# create materialized view s117 as select now()::timestamp(1);
SELECT 1
Time: 54.329 ms
改变它:
t=# create materialized view s117_new as select now()::timestamp(2);
SELECT 1
Time: 64.024 ms
t=# begin;
BEGIN
Time: 0.099 ms
t=# drop materialized view s117;
DROP MATERIALIZED VIEW
Time: 4.134 ms
t=# alter materialized view s117_new rename to s117;
ALTER MATERIALIZED VIEW
Time: 1.054 ms
t=# end;
COMMIT
Time: 49.256 ms
检查:
t=# \d+ s117
Materialized view "public.s117"
Column | Type | Modifiers | Storage | Stats target | Description
--------+--------------------------------+-----------+---------+--------------+-------------
now | timestamp(2) without time zone | | plain | |
View definition:
SELECT now()::timestamp(2) without time zone AS now;
关于sql - 不能更改 PostgreSQL 中 View 或规则使用的列的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43885775/