Re: support ALTER TABLE DROP EXPRESSION for virtual generated column

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: jian he <jian(dot)universality(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: support ALTER TABLE DROP EXPRESSION for virtual generated column
Date: 2025-07-14 02:05:39
Message-ID: CAKFQuwbyOfLqHi0Z9+j+84tVNSO1JOgJnV3gp2XpHkyxGgPt_A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Mar 26, 2025 at 8:15 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> jian he <jian(dot)universality(at)gmail(dot)com> writes:
> > the attached patch is to implement $subject.
>
> Why would this be a good idea? I don't see any principled fallback
> definition of the column. (No, "NULL" is not that.) Certainly we
> should support ALTER TABLE DROP COLUMN, but removing the expression
> and not providing a substitute seems semantically nonsensical.
>

The fallback value being proposed is the result of evaluating the
about-to-be-dropped expression.

We already allow removing a generated expression from a column so it cannot
be that nonsensical.

In either case we are saying the value of this column for a given row is
X. If you "select col from tbl where id = n" you will get "X".
Whether X is:
Physical
Stored Generated
Virtual Generated
is immaterial.

Physical - Physical: N/A
Physical - Stored: Disallowed (syntax but doesn't work)
Physical - Virtual: Disallowed (no syntax)

Stored - Physical: Drop Expression (no table rewrite)
Stored - Stored: Set Expression As (table rewrite)
Stored - Virtual: Disallowed (no syntax)

*Virtual - Physical: Prohibited; Proposal: Drop Expression (table rewrite)*
Virtual - Stored: Disallowed (no syntax)
Virtual - Virtual: Set Expression As (no table rewrite)

In short, the following returns '1id' today.
create table tbl (id serial primary key,
val text not null generated always as (id || 'id') stored);
insert into tbl values (default, default);
alter table tbl alter val drop expression;
select * from tbl;

This otherwise identical sequence (just using virtual) returns "not
implemented", and this proposal means to implement it.
create table tbl (id serial primary key,
val text not null generated always as (id || 'id') virtual);
insert into tbl values (default, default);
alter table tbl alter val drop expression;
select * from tbl; -- would return '1id' under the proposal (not tested...)

The reference to 'NULL' is because the physical table has no stored value
of '1id' and so we need a table rewrite to populate it.

David J.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message wenhui qiu 2025-07-14 03:42:03 Add last_(auto)vacuum_duration column to pg_stat_all_tables
Previous Message Amit Langote 2025-07-14 01:58:24 Re: track needed attributes in plan nodes for executor use