Re: ALTER COLUMN to change GENERATED ALWAYS AS expression?

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Philip Semanchuk <philip(at)americanefficient(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: ALTER COLUMN to change GENERATED ALWAYS AS expression?
Date: 2023-02-07 15:06:13
Message-ID: 60ebe24e-33c4-2f56-a3ea-fc85518ecc22@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2/7/23 06:09, Philip Semanchuk wrote:
>
>
>> On Feb 7, 2023, at 3:30 AM, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at> wrote:
>>
>> On Mon, 2023-02-06 at 12:04 -0500, Philip Semanchuk wrote:
>>> I have a column defined GENERATED ALWAYS AS {my_expression} STORED. I’d like to change the
>>> {my_expression} part. After reading the documentation for ALTER TABLE
>>> (https://www.postgresql.org/docs/current/sql-altertable.html) and trying a few things that
>>> resulted in syntax errors, there doesn’t seem to be a way to alter the column’s GENERATED
>>> expression in place. It seems like my only option is to drop and re-add the column.
>>> Is that correct?
>>
>> I think that is correct. But changing the expression would mean rewriting the column
>> anyway. The only downside is that a dropped column remains in the table, and no even
>> a VACUUM (FULL) will get rid of it.
>
> Thanks for the confirmation. I hadn’t realized that the column would remain in the table even after a DROP + VACUUM FULL. I’m curious — its presence as a deleted column doesn't affect performance in any meaningful way, does it?

From docs:

https://www.postgresql.org/docs/current/sql-altertable.html

"The DROP COLUMN form does not physically remove the column, but simply
makes it invisible to SQL operations. Subsequent insert and update
operations in the table will store a null value for the column. Thus,
dropping a column is quick but it will not immediately reduce the
on-disk size of your table, as the space occupied by the dropped column
is not reclaimed. The space will be reclaimed over time as existing rows
are updated.

To force immediate reclamation of space occupied by a dropped column,
you can execute one of the forms of ALTER TABLE that performs a rewrite
of the whole table. This results in reconstructing each row with the
dropped column replaced by a null value."

>
> In this case we have the option of dropping and re-creating the table entirely, and that's probably what I'll do.
>
> Cheers
> Philip
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dominique Devienne 2023-02-07 16:07:47 Re: Sequence vs UUID
Previous Message Merlin Moncure 2023-02-07 14:47:17 Re: Sequence vs UUID