Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wZCDA-000qbZ-0s for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jun 2026 18:39:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wZCD8-00D6dx-37 for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jun 2026 18:39:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wZCD8-00D6do-24 for pgsql-hackers@lists.postgresql.org; Mon, 15 Jun 2026 18:39:02 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wZCD6-00000000WRy-2rxy for pgsql-hackers@postgresql.org; Mon, 15 Jun 2026 18:39:01 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-490ac10e337so25610355e9.3 for ; Mon, 15 Jun 2026 11:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1781548737; x=1782153537; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=dqcc729wENexaYHuNizZWL0vc8WDN6m2Xcw3vK78tfQ=; b=jTvTl1vr/WLUInRYVbZg0aS/Tih2mhrPXTiwIBBP1XraxI6kCXmV9oWtrNI52ONV9y xtz08I7nnwg4Dgr59PbnfL0pF8jZlC8/fA9yrm/wUxty/W6nGuNdKJqmj2t9NJUj5hOA afXGi43l+YC+YJLjwYsUnjnCW5F7hq2O2/xcxlmftA1zggPq40Q3ltjhULVMi3M+eaUB 2d7EfdqJtu3oCUfTib6VIaLqsSKV6SBUWDO5am80/MY3j2GFjHiJDQdu3+t90QmXf0bj KhH9Z0Ys40ORCyvKKcIDWd0dYnYaDYzcnk/amf6sSW3cQ3DAeACq4zPMJxjx7PAsQCTK Pf5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781548737; x=1782153537; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dqcc729wENexaYHuNizZWL0vc8WDN6m2Xcw3vK78tfQ=; b=RcKsrp8Yoqer7zFMVGezjtE6zeARJ0CTVtAF5VbjklpN1/liMbQIngVpV7W84yASN3 Ljlish3u0Jq1H/OMFm9M/F8BVGjSiM5b5B9S98dn1JfOm6Z5nZ95pmQ05QkjzZm8v9Nq ukoiCzbEtJkn3hrKPg1HACGkAPsiTSjK33xjp8WF8+VU56cusWpY0K1lkUQ/MDTFiAsA RPeemPP3NODDIu7pKjI1HvjOjgzoVzP62qk8B1y6f0hA0dbBLGGf0Tx7HtXQc8bo/tqT 6FQpIkSqx9pqLng5c23k3X9yfCpgts7OM19BdpO2IyUUYVAbaPRFUvKLv5ZeS9kPfCVV 5jGg== X-Forwarded-Encrypted: i=1; AFNElJ8T3ZOCmjc8lJvO+HzlpE2+Ed+qPRfe+32tLVU8i24LYMjX4nkfd8ObJKzzrSc3sgAPYPMvja883+sr+/W+@postgresql.org X-Gm-Message-State: AOJu0Yzr9gfV/620nYh2MAcC7VNHl48MgIgjet0Rdqn8VbjIL8amUJYO gsXJ9kJ9VKE6YER3gEPZXtPIhxIhphOrp1+IEpFfUQnxRwYJAC27ObyeTAV4Ipr0tzU= X-Gm-Gg: Acq92OGeRaj17ZxVOLPNvhys+udGwviwnPSeZn5sPK+5VRFCBP4ObVirMKdXLAPXlJ3 mh+5PY+x9boG10b/rxpCBA0vQlBPTLMKDDFp691tYHI5s/jjyLjC6Um1peVBHnVydBAoSAQyLGk U+VOHaUxgbs1ytFitNGYjuy+NbEBGT9BwbaVrklgSXLuc38Hlq5QE633KeBWw1U30m0tq2TFCYO NujSE4ema1QcjMYmggeMP9tprimLzno4heyBLA4u+Z9Ruj3TXBSc9uSOLEsUYqbjFTQioy2dbQ+ qmb1SddDgQElibKj2vaAn6m1tGAgQCeWqZhCeduXHFjS9a7r0fKayqzsVvbCsMgWArPoMAjcive ZxilTOpnmMOJFfYgp5m7+ikZquubbhuEcQ5n8f+Hw+CyiOM/a5FDSQg+i1u+ZBjLx2xnx1smZnX bznouvFXqTacYGDzvrgKWYFkGXgsmot/eZs8ELsQBWtPzikqvdR/E00g== X-Received: by 2002:a05:600c:c0c3:10b0:490:b724:dbd6 with SMTP id 5b1f17b1804b1-4922005087emr125855955e9.6.1781548736956; Mon, 15 Jun 2026 11:38:56 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:260:e8ff:99e8:fa54:1fbd:dc68]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490ea9520f9sm216159355e9.1.2026.06.15.11.38.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2026 11:38:56 -0700 (PDT) Message-ID: <3af338791e8ab27c4a7efbc4217c97162de76bc8.camel@cybertec.at> Subject: Re: Adding a stored generated column without long-lived locks From: Laurenz Albe To: Alberto Piai , pgsql-hackers@postgresql.org Cc: =?ISO-8859-1?Q?=C1lvaro?= Herrera Date: Mon, 15 Jun 2026 20:38:55 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.3 (3.58.3-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2026-05-27 at 19:43 +0200, Alberto Piai wrote: > I'd like to take his point, together with the question from Tom and the > suggestion by =C3=81lvaro, and make a new proposal for the design of this > command. >=20 > Design iteration 2 > ------------------ >=20 > Syntax: >=20 > ALTER TABLE t ALTER COLUMN c > ADD GENERATED ALWAYS AS (expr) STORED USING CONSTRAINT check_name >=20 > check_name must be a valid constraint of the form >=20 > CHECK (c IS NOT DISTINCT FROM (expr)) >=20 > This fails if: >=20 > - a check constraint named check_name is not found for table c > - the constraint is not valid > - the constraint does not match exactly the expr the user intends to use > as a stored default expression >=20 > On success, the table c is now a stored generated column with the given > default expression, and the check_name constraint has been removed. > > This addresses Tom's remark, we can now fail instead of just rewriting > the column. I like this proposal. It avoids the question "to rewrite or not to rewrite" by just outright failing if there is no suitable constraint. The idea to avoid the problem with NULL by forcing IS NOT DISTINCT FROM in the constraint is a good solution. Perhaps you could also allow the equality operator if the column in question is defined NOT NULL. > It improves slightly upon the issue of a potential conflict with a > future edition of the SQL standard, by being more specific. I don't see > a way to be completely sure we won't have conflicts. We could improve > more by making the syntax more "alien" and very unlinkely to be picked > up by the standard, but at a usability cost for Postgres. I'm open to > suggestions. I don't think that the new proposal makes it less likely to get in conflict with later additions to the standard. But I don't think that inventing unlikely syntax to avoid such conflicts is a good idea. First, the standard committee itself seems (or seemed) to have a strong predilection for alien, verbose syntax. Second, if we end up with weird, unwieldy syntax, that would be bad. No, the syntax you are proposing sounds reasonable to me. > It improves upon another question raised by =C3=81lvaro: does the user ha= ve > to clean up the constraint? In v1 I felt it was better to have the user > remove it after the migration. Since here it's explicitly mentioned as > the constraint to use to migrate the column, I think it's OK to remove > it. We are conceptually moving it from being a constraint to being the > new default expression. >=20 > The implementation should also be simpler, since there will never be a > table rewrite. >=20 > Any thoughts about this? Yes. I think that you should not drop the constraint. That's what I'd expect, similar to how we don't drop the check constraint that allows to skip the table scan in ALTER TABLE ... ALTER COLUMN ... SET NOT NULL or ALTER TABLE ... ATTACK PARTITION. I feel that automatically dropping the constraint is a bit too much black magic, but it is more a feeling than a conviction. Yours, Laurenz Albe