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 1wZE7X-000s75-07 for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jun 2026 20:41:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wZE7U-00DRQ6-2w for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Jun 2026 20:41:20 +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 1wZE7U-00DRPx-1u for pgsql-hackers@lists.postgresql.org; Mon, 15 Jun 2026 20:41:20 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wZE7S-00000000XLP-400z for pgsql-hackers@postgresql.org; Mon, 15 Jun 2026 20:41:19 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-490b8ac62baso37233055e9.0 for ; Mon, 15 Jun 2026 13:41:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1781556077; x=1782160877; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=9wH041glX4mteaVvm0xIbbbj0gMPrIsCW0kKrE6ZKPA=; b=bQ09Mr1wB7Kn9R6/97IfXIfv7PvA01BcOUkgqE+9DTG+rACvSLAacJJ/DOjuxldmfS EsWi18TzCQx+Ywtc+HvXAY/twC+XdQKh368DHkCaVSm1KMh13SN4ELy0bGhu1qyxmFGk MHxmRyDhJZtdsICCkikg3K0tlmpkFDNK6XoheEextknssAevD755irr2El00VKU3fvUI vxOi3ZHeTdLlqY8xGYLIOyUDDoXMsN63QmR5aIox4bKrThGRUWInb3ovMP8caGO5V+jY bacqJSNAxVbi6QYnks/ReRKQYbQW/LJZ1qZ770VYFMw5VHi3thbKkTcBNkQgyiOu+71n 4ZVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781556077; x=1782160877; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9wH041glX4mteaVvm0xIbbbj0gMPrIsCW0kKrE6ZKPA=; b=ejAgOzSLlI83/XMkqaED0HsIqdtcNd/Z5OZ2Cqd3ft5KIw2Z6W2I0vQAsH56QaGil2 vhxNUJPvpC87r089fRVZ9ye86fZz0thdU+vBP7vnu3GYGJkYirImRXJjgMcvotw9CfRb 1UxYcx3oG11y8UukGZCC0e8K80ZcnOJULk4kGjwd/oRIU6dsphlOIZEwbLfnE6nlJlWs ea+cxPWSv/xFKTInoicYyaz5tryjyaPDRU8jOZAsurW5CST5F/8V3fp4dyHJS8nFGSbA N8nQXeqq5PcequGJTzf0lmtMJTp0iXmH5v11kgqyeayhI5TKzvZcvLxR4zWs09fHLnLs bzSw== X-Forwarded-Encrypted: i=1; AFNElJ+ZKu0YZ5sDfifD1o9ZPY28tD96IFvX/E7dVeT8j23V4yO7hMgS1k4SD4TNfCuyujCX1Og49v+PcZ0s0qwg@postgresql.org X-Gm-Message-State: AOJu0Yywn8x1DGn1urlKW796epHvLKB7LA+An3AIvZRe0mRXV1BsSOoI +oetd6aNKKZbeEvUrl4oxF7FoZ1QLW5ppGoTorwgGHTGnsCm8+lFHx1oDRfWq+o4fCw= X-Gm-Gg: Acq92OHxCkN35zyC+N50cU23BNq5H4nMUGUaJ/zW/I83mHStybr2BtzOsiUqjX/Qv6p SwEo8Xx9rMbfytOpCy/MHqY3TZQQYjDE1R173P77vCs2MffPaJ70rSNiCFHa5D7KfXJ7O0x2Gik gxRwnikxmjl9n8Y2+QaCTRP5q4VXBNwt7NZnlwihGxyheOLkcxzvgI1QSy1WKtCQhi/mCMckR8W BXPWSvhcRErQ3A35LZxvq2qOPn9JdfsmHyKcCAhh0SzxLErPy35by1kw2T04GM/ZVpQJJ1I3lRc qbL8gACY9o6RoCHDBdQtb1zi5InFQH0d3CObCaYDr2Ft1MN1zB/SacmOQ0jJa2Wnb2vZHIV0IqV x3vuZgF77iQDDSMaMvFyDyicAE4gfrBKY/StYzmZoICm2DbogSt4dcrkiqXSVOUviSbkBVckiE0 gu+lDLo4NLav99STpt7MgBtk63M/vxmjW1ezm/HowFp8PKDt3jdOt3FKL+2LQgyako X-Received: by 2002:a05:600c:5688:b0:490:7df7:9190 with SMTP id 5b1f17b1804b1-4922fac31a9mr10130615e9.8.1781556076922; Mon, 15 Jun 2026 13:41:16 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:270:21f:fc97:3ffd:7f89:d662]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4922fa47179sm17570855e9.6.2026.06.15.13.41.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2026 13:41:16 -0700 (PDT) Message-ID: <03658280d2a719fe854457b581f420865a3d1238.camel@cybertec.at> Subject: Re: Adding a stored generated column without long-lived locks From: Laurenz Albe To: Alberto Piai , pgsql-hackers@postgresql.org Date: Mon, 15 Jun 2026 22:41:15 +0200 In-Reply-To: References: <1514ac90bc374f750df34fe465caadd77121b067.camel@cybertec.at> 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:44 +0200, Alberto Piai wrote: > On Tue May 26, 2026 at 5:23 PM CEST, Laurenz Albe wrote: >=20 >=20 > > 2) We currently have ALTER TABLE ... ALTER ... SET EXPRESSION AS (...) = to > > change the generation expression of a column. This command always > > rewrites the table, according to the documentation. > > I think that if the present patch adds support to skip rewriting the= table > > when a generation expression is added and the expression matches a c= heck > > constraint, changing the generation expression should also be possib= le > > without a rewrite. If not, I would consider that a violation of the > > principle of least astonishment. > > Would it be difficult to extend the patch to support that? >=20 > Yes, I don't see a way to make that work. Since we're talking only about > stored values, a rewrite will always be necessary. However, using this > new command, a user could add a column with the new expression, then > atomically drop the old one and rename. All without holding onto an > AccessExclusiveLock for a long time :) With your new proposal to never rewrite the table, but fail instead if there is no constraint, my objection loses its point, so I withdraw it. > > 3) We already have a couple of tricks to avoid blocking for a long time= : > >=20 > > - ALTER TABLE ... ALTER ... SET NOT NULL can skip the table scan if = there > > is a check constraint that makes sure that the column is NOT NULL > >=20 > > - ALTER TABLE ... ATTACH PARTITION can skip the scan of the new part= ition > > if there is a check constraint matching the partition constraint > >=20 > > It would be great to document these little tricks in the documentati= on, > > probably on the ALTER TABLE page. This is not necessarily the job o= f > > this patch, but it would also not be off-topic for the patch. >=20 > The SET NOT NULL one and the ATTACH PARTITION one are documented in the > section specific to the command. However >=20 > or, if an equivalent index already exists, it will be attached to the > target table's index, as if ALTER INDEX ATTACH PARTITION had been > executed >=20 > is not very explicit about the advantages this has for online > migrations. >=20 > In the NOTES section of the ALTER TABLE page, there is a paragraph about > NOT VALID / VALIDATE, which is another operation in the same spirit as > this. >=20 > Maybe we could group them all in a new section dedicated to online > schema migrations? You are right, the existing shortcuts are documented. Your new proposal makes the proposed feature different from these existing cases, so I don't think lumping them together is a good idea now. > Agreed, will fix all these in the next version of the patch. Great; I'm looking forward to it. Yours, Laurenz Albe