Re: Adding a stored generated column without long-lived locks

From: Alberto Piai <alberto(dot)piai(at)gmail(dot)com>
To: Alberto Piai <alberto(dot)piai(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Adding a stored generated column without long-lived locks
Date: 2026-05-14 22:46:32
Message-ID: DIIRFCWJIA80.2Q5DYIX6C7KZ5@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri Apr 24, 2026 at 2:10 AM PDT, Alberto Piai wrote:
> On Tue Apr 7, 2026 at 5:02 PM +08, Alberto Piai wrote:
>> On Tue Mar 17, 2026 at 5:31 PM +07, Alberto Piai wrote:
>>
>>> I recently needed to add a stored generated column to a table of
>>> nontrivial size, and realized that currently there is no way to do
>>> that without rewriting the table under an AccessExclusiveLock.

The attached v4 is a rebase against current master, plus:

- I moved the call to RememberAllDependentForRebuilding before the
update to pg_attribute, since it provides checks for some
invalid/unsupported invocations it seems more useful to do it before
changing anything.

- I hadn't noticed before that AddRelationNewConstraints returns the new
(cooked) default definitions, so we can use those instead of building
them again.

- cleaned up some includes I had added by mistake, and moved some tests
around between the two commits

A while back I also posted a fix for the issue of DROP EXPRESSION not
working with subpartitions [1], this patch isn't ajusted yet to match, I
would do that if the bugfix would be committed first.

I am still hoping to get a reviewer for the in-person commitfest at the
upcoming pgconf.dev :)

It's my first contribution, but the change is pretty self-contained and
hopefully not terribly complex to review. I'm trying to address a real
world use case, it would be fantastic to make some progress with this
patch.

Anyone's motivated? :)

Regards,

Alberto

[1] https://www.postgresql.org/message-id/DHMT78XOD8BK.341V3H87KZ7NO%40gmail.com

--
Alberto Piai
Sensational AG
Zürich, Switzerland

Attachment Content-Type Size
v4-0001-Support-changing-a-column-into-a-stored-generated.patch text/plain 27.8 KB
v4-0002-Try-to-avoid-a-rewrite-when-adding-a-stored-gener.patch text/plain 17.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexandra Wang 2026-05-14 23:13:26 Re: Is there value in having optimizer stats for joins/foreignkeys?
Previous Message Andres Freund 2026-05-14 22:17:17 Re: Refactor: allow pg_strncoll(), etc., to accept -1 length for NUL-terminated cstrings.