Re: support create index on virtual generated column.

From: jian he <jian(dot)universality(at)gmail(dot)com>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: support create index on virtual generated column.
Date: 2025-04-14 11:09:45
Message-ID: CACJufxGu3jjiyKqPRKP+n92-7u3S_yexCLe1NCQywe96jQxzrg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Mar 26, 2025 at 5:36 PM Kirill Reshke <reshkekirill(at)gmail(dot)com> wrote:
> reshke=# CREATE TABLE xx (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL) ;
> CREATE TABLE
> reshke=# create index on xx (b);
> CREATE INDEX
> reshke=#
> reshke=# \d+ xx
> Table "public.xx"
> Column | Type | Collation | Nullable | Default
> | Storage | Compression | Stats target | Description
> --------+---------+-----------+----------+-----------------------------+---------+-------------+--------------+-------------
> a | integer | | |
> | plain | | |
> b | integer | | | generated always as (a * 2)
> | plain | | |
> Indexes:
> "xx_b_idx" btree (b)
> Access method: heap
>
> reshke=# alter table xx drop column b;
> ALTER TABLE
> reshke=# \d+ xx
> Table "public.xx"
> Column | Type | Collation | Nullable | Default | Storage |
> Compression | Stats target | Description
> --------+---------+-----------+----------+---------+---------+-------------+--------------+-------------
> a | integer | | | | plain |
> | |
> Indexes:
> "xx_b_idx" btree ("........pg.dropped.2........" int4_ops)
> Access method: heap
>
> reshke=#
> ```
>
> with regular columns we have different behaviour - with drop column we
> drop the index
>

I was wrong about dependency.
when creating an index on a virtual generated column, it will have
dependency with
virtual generated column attribute and the generation expression
associated attribute.

new patch attached. Now,
ALTER TABLE DROP COLUMN works fine.
ALTER INDEX ATTACH PARTITION works fine.
creating such an index on a partitioned table works just fine.
for table inheritance: create index on parent table will not cascade
to child table,
so we don't need to worry about this.

Attachment Content-Type Size
v2-0001-index-on-virtual-generated-column.patch text/x-patch 55.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2025-04-14 11:14:59 RE: Rename injection point names in test_aio
Previous Message wenhui qiu 2025-04-14 11:08:05 Re: An incorrect check in get_memoize_path