【问题标题】:cannot alter type of a column used by a view or rule in PostgreSQL无法更改 PostgreSQL 中视图或规则使用的列的类型
【发布时间】:2017-05-10 07:13:05
【问题描述】:

我不想将列的大小从字符(9)更改为字符(12)

ALTER TABLE product_based_award ALTER COLUMN name TYPE character(12);

但在内部,此列由 MView 使用,因此它不允许更改表并给出以下错误。

错误:无法更改视图或规则使用的列的类型

我为此找到了两种解决方案,一种是删除 mview 并重新创建它,另一种是更新 pg_attribute。 但是我不能使用这两个选项,因为我们的数据库非常复杂,所以更新 pg_attribute 会导致问题并且也不能删除 mview。

那么有没有其他最好的方法来解决这个问题。

【问题讨论】:

  • 片段仅适用于 html/css/javascript 问题。不要将 sn-ps 用于其他语言。
  • 如果您无法删除该视图或更新 pg_attribute(无论如何不推荐),您将无法更改类型。您将必须删除此视图以及依赖它的视图。

标签: sql postgresql


【解决方案1】:

更新 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;

【讨论】:

  • 如果新旧视图都基于 same 基表并且想要替换视图的原因是因为您不能使用此视图切换技巧'想要更改基表中的列定义。在定义更改之前,您无法创建新视图,并且在删除旧视图之前,您无法更改定义。
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 2012-01-21
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
相关资源
最近更新 更多