Re: Why cannot alter a column's type when it's used by a generated column

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Why cannot alter a column's type when it's used by a generated column
Date: 2025-10-21 07:30:49
Message-ID: CACJufxH+66OuFahUtQ6DP_ksY42h8+g7en_RgE6BGmgvARAgMg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Oct 21, 2025 at 3:03 PM Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>
> Here I am just proposing a patch for a small step. When any dependent generated column has SET EXPRESSION, then we can allow the column type change:
>
> ```
> -- Before this patch, the recommended usage was:
> ALTER TABLE gtest
> DROP COLUMN x,
> ALTER COLUMN a TYPE float8,
> ADD COLUMN x bigint GENERATED ALWAYS AS ((a::int + b) * 2) STORED;
>
> -- With this patch, the statement is simplified as:
> ALTER TABLE gtest
> ALTER COLUMN a TYPE float8,
> ALTER COLUMN x SET EXPRESSION ((a::int + b) * 2);
> ```
hi.

please feel free to bump the attached patch version.

+-- So, you can change a column's type as long as any dependent generated
+-- column already has a set expression defined:
+ALTER TABLE gtest27
+ ALTER COLUMN a TYPE float8,
+ ALTER COLUMN x SET EXPRESSION AS ((a::int + b) * 2);
+\d gtest27
+ Table "generated_stored_tests.gtest27"
+ Column | Type | Collation | Nullable |
Default
+--------+------------------+-----------+----------+---------------------------------------------------
+ a | double precision | | |
+ b | bigint | | |
+ x | bigint | | | generated always
as ((a::integer + b) * 2) stored
+

the above output seems wrong?
one way to quickly test it is create table gtest27 again
(
create table gtest27(a double precision, b bigint, c bigint GENERATED
ALWAYS as ((a +b) * 2) stored);
)
and the result of "\d gtest27" is

Column | Type | Collation | Nullable |
Default
--------+------------------+-----------+----------+--------------------------------------------------------------------------------
a | double precision | | |
b | bigint | | |
c | bigint | | | generated always
as (((a + b::double precision) * 2::double precision)) stored

which conflicts with your changes.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2025-10-21 07:34:13 Re: Client-only Meson Build From Sources
Previous Message Álvaro Herrera 2025-10-21 07:25:27 Re: CREATE POLICY IF NOT EXISTS