Re: Why cannot alter column type when a view depends on it?

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Why cannot alter column type when a view depends on it?
Date: 2025-09-28 09:03:58
Message-ID: CACJufxF5=6NgPCjeT_vZgnv3vQ+MdhWNe7qBefQUTGy5bKq1HQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Sep 28, 2025 at 4:19 PM Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>
> ```
> evantest=# create table t (c char(20));
> CREATE TABLE
> evantest=# create view v_t as select * from t;
> CREATE VIEW
>
> evantest=# alter table t alter column c type char(25);
> ERROR: cannot alter type of a column used by a view or rule
> DETAIL: rule _RETURN on view v_t depends on column "c"
> ```
>
> I tried to understand why this restriction is set, then I found that, when a function uses a view, the view can actually be dropped, only when the function is executed, it will raise an error saying the view doesn’t exist. From this perspective, I think we should allow alter column type when a view depends on the column.
>

hi.

the simple case you mentioned above, it's definitely doable.
however say we have
create view v_t1 as select * from v_t;

RememberAllDependentForRebuilding can not cope with the dependency
between t and v_t1.
ATPostAlterTypeCleanup, we use DROP_RESTRICT in
performMultipleDeletions(objects, DROP_RESTRICT, PERFORM_DELETION_INTERNAL);

That means
``alter table t alter column c type char(25);``
can not drop and recreate view v_t1,
it will error out within performMultipleDeletions while trying to drop view v_t.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2025-09-28 09:05:04 Re: [PATCH] GROUP BY ALL
Previous Message Xuneng Zhou 2025-09-28 09:02:43 Re: Implement waiting for wal lsn replay: reloaded