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 1pPStB-0002bK-Lc for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 18:40:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPStA-0005ws-Ba for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 18:40:20 +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 1pPSt9-0005wi-Ln for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 18:40:19 +0000 Received: from mail-qv1-xf30.google.com ([2607:f8b0:4864:20::f30]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pPSt6-0005JC-Lf for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 18:40:18 +0000 Received: by mail-qv1-xf30.google.com with SMTP id m16so3902478qvm.4 for ; Tue, 07 Feb 2023 10:40:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=irdwX54haWfGIHnSY4K89JTNsRthqZ9xc+V/MvQdeR8=; b=E9LsJxXIykJ6qSyhQQieZWvn1tbFN7rZgawvAQF3xYhOrT9pt9hpBRY5Feon/anWSX OgJ1wLsplxbt2FbDE9cJEIP7weARRov07AHUWFvXdN1ItFZbnO6e4sV43hSDKJWcAmN5 K3X2TNZ9gm2nrc+bfef5nEj3/ZDGFBtfAOfy2jl/olvvOlX2ueVrfBHHNrUPNWDhFQ12 10+vPZB9jxpneo9y3ubn6+xl76/FytT6mtAZsT9UP/ZDbmUIDp9B2dtF104rikOnFbmo I0opwZ6JpWBytnUivL0UDn5Im0K1iMk+R8Jen5StTGTZegWJgOM6hf6FaYV/BzE9zgZm hV2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=irdwX54haWfGIHnSY4K89JTNsRthqZ9xc+V/MvQdeR8=; b=jLgkjsRw3PP3eQRgX0JHoc3dKT9G6a7wKeAHYO2fGzh3WX2LtPSOGTPh5NfMYYwc6s LzvdXOWO3xzeJ3jroAMBJ1zqFdY6rTqaJ1eULxeWjCeNIYz4wZCVJLcZmOJxHX0hE7kE M6o59ZHNQFEFgc50mxgXQCYpyqSVCndx8mclC2fd8x7w7XQTELJRVQE52pYCZ32zlRF1 C63/nqSGTIbgKXc9CCt9lLEcvTAaGSKFb3TPbi72mq0+N24hMG3ZuifoxEgvDSMe3Z3W McaKCbMGsgj/AzB3eDV8zygNkphPIqmUg8rUzoNu53NWIaWyJqRpEzw96W/b7hjUl5jF dkCA== X-Gm-Message-State: AO0yUKVpfT+q8TnSMS9/wVvBALK5kG1hVSq+xf7WIRnhUC/LSwaWB+5D +zgxKHkIoEesQyvskTbc3UTvZSVDPqs= X-Google-Smtp-Source: AK7set/z/PDTFyLGXDX+OXj36D21EbWcXZzx1/26lwIVVA4PbaF/1UV0Fb7NYk0p0P9wDC0zYvbQgg== X-Received: by 2002:a05:6214:20ac:b0:56b:f319:cc44 with SMTP id 12-20020a05621420ac00b0056bf319cc44mr6553414qvd.7.1675795215001; Tue, 07 Feb 2023 10:40:15 -0800 (PST) Received: from ?IPV6:2600:1700:bd2c:b810::43? ([2600:1700:bd2c:b810::43]) by smtp.googlemail.com with ESMTPSA id br15-20020a05620a460f00b00706bc44fda8sm9894505qkb.79.2023.02.07.10.40.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Feb 2023 10:40:14 -0800 (PST) Content-Type: multipart/alternative; boundary="------------0RnzxAixyAZ27NMZvqofAAmj" Message-ID: Date: Tue, 7 Feb 2023 12:40:13 -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 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> From: Ron In-Reply-To: <60ebe24e-33c4-2f56-a3ea-fc85518ecc22@aklaver.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. --------------0RnzxAixyAZ27NMZvqofAAmj Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. --------------0RnzxAixyAZ27NMZvqofAAmj Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.
--------------0RnzxAixyAZ27NMZvqofAAmj--