Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPTIj-0003tv-3x for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 19:06:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPTIh-0004uB-9e for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 19:06:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPTIe-0004tx-KL for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 19:06:42 +0000 Received: from mail-ot1-x331.google.com ([2607:f8b0:4864:20::331]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pPTIY-0005dQ-Nc for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 19:06:36 +0000 Received: by mail-ot1-x331.google.com with SMTP id g15-20020a9d6b0f000000b0068db1940216so709752otp.5 for ; Tue, 07 Feb 2023 11:06:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:references:to:from:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=VTvGD4ILTrSIjS8J2nkBsqkmbKRrvT7jAZmEqHYPHiA=; b=nteJR8YtiuP8WDyG9rtgvYvoiwhi+Np0a7NVne1m9qfWyyuVVxzSGlqEDwbxwlNWVO BlGHMPaeaOMuEwOnS0yUVN88vTUocH94mUqpkmgwjo+eChP82EXzzCBLiHs5dsI+X9Pg 8yLMOJ8C3D3saU8wmP/MB65k+t3re87E38VydHk4Fw18yVGFvYcdNsk0/lmBnLKtmdgq tNqP5Hy6y1n2VLoP/WjP2g3ivpl4h6xty2v2DWYpOfBBQJ5bSo6JrrGpsc4gcpZKDulZ fwzR/0zIicKPgRb7i7M8O8TeOjAYO0e6d3gw64jttRN0PHTYdtEMvBVLni5LhGDNRN35 1Umw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:references:to:from:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=VTvGD4ILTrSIjS8J2nkBsqkmbKRrvT7jAZmEqHYPHiA=; b=paTQwFTGiDP5WbeVgkC+tZ47BjFnBlZUSrN+eyLz1MbV6BgRgvxGrEWjYNRDhtURhY hi3k1gHjEXE3EmyvMV8fWKeEoue3KbegDTCZYiVl9ursZ2tsAmhiysIo4samtHutwKPA HjpKDf/kapG0uFNYiCgownHtHLru7k8SDn9JCtFSU1k74/XbhDHM/pw6AuS8D8UB64OV YXmVynNi2S9spfvMIqmdGXdmudGQmpW/aGq7hLoEftS7ulV46TXSlb8i2w3lE/dRwASZ NtdQqi/H5k3iEAj0tQMDBZyTWiDFPcG1tPU2N4Y8mbI6bUozUly1ZJB/BOsEZyNRO8sr oRng== X-Gm-Message-State: AO0yUKXhAABs1U6S72eVlZ+NgbogB8nEb6Xg3uz2EatgGcfai/7aSWd4 ZDlhXc424bJPEEt2VK5vwFWptbx3uOg= X-Google-Smtp-Source: AK7set+vBtlM+x48JPDp9PVPPUds+ke3hpolGcGWaEuEHmkL+FE2yNJhZieay7ylPtsVtyxGZBQ4Vg== X-Received: by 2002:a05:6830:1441:b0:688:4c9c:d45f with SMTP id w1-20020a056830144100b006884c9cd45fmr2464355otp.20.1675796793407; Tue, 07 Feb 2023 11:06:33 -0800 (PST) Received: from ?IPV6:2600:1700:bd2c:b810::43? ([2600:1700:bd2c:b810::43]) by smtp.googlemail.com with ESMTPSA id ci10-20020a05683063ca00b00684a10970adsm6944860otb.16.2023.02.07.11.06.32 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Feb 2023 11:06:32 -0800 (PST) Content-Type: multipart/alternative; boundary="------------n8lKyDiBACgsP74R6QWBcj2G" Message-ID: <8b6b1816-00be-4acb-948d-a1ca936aca04@gmail.com> Date: Tue, 7 Feb 2023 13:06:32 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: ALTER COLUMN to change GENERATED ALWAYS AS expression? Content-Language: en-US From: Ron To: pgsql-general@lists.postgresql.org References: <4C775CB6-D73C-42B6-A94E-40C495AAA521@americanefficient.com> <6442cf200b9ba3d86e695172c4da98a802bda48e.camel@cybertec.at> <60ebe24e-33c4-2f56-a3ea-fc85518ecc22@aklaver.com> <4edb5000-67fe-73af-66ba-2fe02b9f5a6f@gmail.com> In-Reply-To: <4edb5000-67fe-73af-66ba-2fe02b9f5a6f@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------n8lKyDiBACgsP74R6QWBcj2G Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Ignore this... On 2/7/23 13:05, Ron wrote: > On 2/7/23 09:06, Adrian Klaver wrote: >> On 2/7/23 06:09, Philip Semanchuk wrote: >>> >>> >>>> On Feb 7, 2023, at 3:30 AM, Laurenz Albe 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." > > VACUUM FULL doesn't rewrite the table? > > The doc page seems to say that it does: > https://www.postgresql.org/docs/current/sql-vacuum.html > "|VACUUM FULL|rewrites the entire contents of the table into a new disk > file with no extra space". > > -- > Born in Arizona, moved to Babylonia. -- Born in Arizona, moved to Babylonia. --------------n8lKyDiBACgsP74R6QWBcj2G Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Ignore this...

On 2/7/23 13:05, Ron wrote:
On 2/7/23 09:06, Adrian Klaver wrote:
On 2/7/23 06:09, Philip Semanchuk wrote:


On Feb 7, 2023, at 3:30 AM, Laurenz Albe <laurenz.albe@cybertec.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."

VACUUM FULL doesn't rewrite the table?

The doc page seems to say that it does:
https://www.postgresql.org/docs/current/sql-vacuum.html
"VACUUM FULL rewrites the entire contents of the table into a new disk file with no extra space".

--
Born in Arizona, moved to Babylonia.

--
Born in Arizona, moved to Babylonia.
--------------n8lKyDiBACgsP74R6QWBcj2G--