From: Alberto Piai <alberto.piai@gmail.com>
To: Xiaoxuan Li <xiaoxuan.li.dev@gmail.com>
To: Alberto Piai <alberto.piai@gmail.com>
Cc: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: Álvaro Herrera <alvherre@kurilemu.de>
Cc: pgsql-hackers@postgresql.org
Subject: Re: Adding a stored generated column without long-lived locks
Date: Sat, 14 Nov 2026 16:50:50 +0100
Message-ID: <DMV1XMLK4NB4.2OSIZVN2TT2WR@gmail.com> (raw)
In-Reply-To: <CANuORAa25++iUj2qaQmxK3gkikxgX0nn=0v3h91Vqq3h8d1_LQ@mail.gmail.com>
References: <DLLBPPI9MKV4.1EYFI9ZD90Z1E@gmail.com>
<arKaxe9vLCCi_eio@alvherre.pgsql>
<CAEze2WhTKSn8gTAneJXEHRTqxpQ9Jix0ijd5fSa3owOpiQuC3Q@mail.gmail.com>
<d57d7a0b7e2fe1c148996e41559647adde4c2034.camel@cybertec.at>
<DLN1MKEVTIQW.31UMH54A38IA7@gmail.com>
<f2d93a4aabc2737ecb933d4f33b0739e9921e71b.camel@cybertec.at>
<DLTW5NS7XJA0.1Y3GAC6AK8N2H@gmail.com>
<DMO4P471X6Q0.FTWMWFEUG62W@gmail.com>
<CANuORAa25++iUj2qaQmxK3gkikxgX0nn=0v3h91Vqq3h8d1_LQ@mail.gmail.com>
Hi Xiaoxuan,
thank you for having a look at the patch!
On Fri Nov 13, 2026 at 9:06 PM CET, Xiaoxuan Li wrote:
> Hi Alberto,
>
> v12 (on e405a46c45b, Linux/aarch64, gcc 11, cassert) builds cleanly
> and passes the regression tests, but this new change loses data:
>
>> - clear atthasmissing and attmissingval for consistency with CREATE
>> TABLE: a column that was GENERATED since the beginning can't have
>> those set, so let's be consistent.
>
> A column added with a constant default has no stored value in the rows
> that existed before; they read attmissingval. The constraint is
> validated against those values, and then the command removes them:
Oops, nice catch, and what an embarassing thinko on my side.
The property I'd like to establish here is that a stored generated
column added with this command should be indistinguishable from one that
was defined with CREATE TABLE or ALTER TABLE ... ADD COLUMN.
So when I noticed the different atthasmissing/attmissingval, I thought
I'd just drop those.
Your test case shows an even bigger problem: in the presence of a
previous default value, the CHECK constraint is not sufficient to prove
that the column was completely backfilled with the correct data.
My first thought now would be to have the command bail if the column has
atthasmissing/attmissingval, and document that it must be added without
a DEFAULT value.
The (not so small, but not terrible and maybe unavoidable) downside is
that this can't be detected early, so a user who's doing this migration
and oversees the note in the docs will only notice at the very end, and
will have to start from scratch and do the work of backfilling the data
again.
Or alternatively, I give up on trying to preserve such a property. A
column which was added with a default value and freshly migrated with
this command will have atthasmissing/attmissingval. Since the constraint
has been validated aginst the missingval, we are sure that if/when the
table is eventually rewritten (for example with REPACK), the correct
value will be stored (and atthasmissing/attmissingval will be cleared).
This might allow saving some work in the backfilling phase if most rows
would end up with the default value anyway.
WDYT?
(I am inclined to go for the latter, but I want to spend some more time
thinking about this.)
Regards,
Alberto
--
Alberto Piai
Sensational AG
Zürich, Switzerland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: alberto.piai@gmail.com, xiaoxuan.li.dev@gmail.com, laurenz.albe@cybertec.at, boekewurm+postgres@gmail.com, alvherre@kurilemu.de
Subject: Re: Adding a stored generated column without long-lived locks
In-Reply-To: <DMV1XMLK4NB4.2OSIZVN2TT2WR@gmail.com>
References: <DLLBPPI9MKV4.1EYFI9ZD90Z1E@gmail.com> <arKaxe9vLCCi_eio@alvherre.pgsql> <CAEze2WhTKSn8gTAneJXEHRTqxpQ9Jix0ijd5fSa3owOpiQuC3Q@mail.gmail.com> <d57d7a0b7e2fe1c148996e41559647adde4c2034.camel@cybertec.at> <DLN1MKEVTIQW.31UMH54A38IA7@gmail.com> <f2d93a4aabc2737ecb933d4f33b0739e9921e71b.camel@cybertec.at> <DLTW5NS7XJA0.1Y3GAC6AK8N2H@gmail.com> <DMO4P471X6Q0.FTWMWFEUG62W@gmail.com> <CANuORAa25++iUj2qaQmxK3gkikxgX0nn=0v3h91Vqq3h8d1_LQ@mail.gmail.com> <DMV1XMLK4NB4.2OSIZVN2TT2WR@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox