sql - 不能更改 PostgreSQL 中 View 或规则使用的列的类型

标签 sql postgresql

我不想通过以下方式将列的大小从字符 (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/

相关文章:

postgresql - AWS 云信息 : Enable PostGIS Extension in RDS from Cloudformation

postgresql - 如何从一行中对先前非规范化的数据进行分组

postgresql - 将 CSV 文件中的内容加载到 PostgreSQL 表中

postgresql - 当其他行具有值时,Postgres 不返回任何内容

c# - 如何打印参数化的 SQL 查询?

php - 在 IF 条件下选择 DATEDIFF 时出错

mysql - 选择具有三个 where 条件的查询较慢,但具有三个 where 条件中的任意两个组合的相同查询很快

sql - UNION 子查询的奇怪行为,为什么?

sql - 如何对 ID 字段进行分组并选择在另一个字段 PostgreSQL 中具有最高值的行

java - org.postgresql.util.PSQLException : ERROR: syntax error on or near "session_user"