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 1pPTHW-0003r0-NI for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 19:05:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPTHV-0000lJ-JM for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 19:05:29 +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 1pPTHP-0000df-9m for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 19:05:29 +0000 Received: from mail-oi1-x230.google.com ([2607:f8b0:4864:20::230]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pPTHE-0005cU-9T for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 19:05:18 +0000 Received: by mail-oi1-x230.google.com with SMTP id c15so13348746oic.8 for ; Tue, 07 Feb 2023 11:05:12 -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=+V0fEt9lUHbdPRB60f/PoA9GYNNz4njRwXhJ3QylnaM=; b=eSjs2a+ipM9rMUqRhHBxI5PnMxGEjOlbv7grpFtDABtH4fv/WkUXi6QQ45k0GciSCt JFraoYpru6ZLRPfP+J6bV0zpU3OrPLkOYMCuYn7NLG/Xlvbi2zFLF68x9gmx5wU3hs5d xyhHx0mSCQ54KnnmC3b5R07gZmUz7a4HTwnj5QIlJbjg25aIgIrPQri/hek0Kp1M3jsA D202iWG7aF/cNS9kVeKjqfhgw8ZNkd2aOfYyKz1cddff4yPARizMkQWV9mGm4YZEq8Qr fAVyCdc7ksw4/1LtkUDeyLIPQ1Vv/SzEsge7HCDjo6j1s/DuUCPh7GLHja9LV720Mbfe KidQ== 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=+V0fEt9lUHbdPRB60f/PoA9GYNNz4njRwXhJ3QylnaM=; b=1QZif69aosPxneRAlNUvxPY3YIw1COG4ZuAZtD2qGYmD+3N9sXU1aCZ8+/30KZRDe5 0+mr6Kn+rJlONN//Nvlh90EuozWN7Kzyd2FDQ1OfLglsUiaUP+Lc0y1aeAiGjPvEcOUU 4ab1/MyrEawXANCEUDoebk34m3IvyLvTr/jIupWxQAE8AD6oUcRFspJP64P95ilpMBdK 2QFeLNbjVMiFhwcWFgnhGseb2oGoMkImOTh/5t7NzkQ/qdiYXG9/vdad3p5ClrJP5lQY R4j8zjREP9DsKsoGlyuSjvq4K1kkPt4PZOahqZv2YtVpSkgVlSMtYdJKg5bivJYfGaPK vDRg== X-Gm-Message-State: AO0yUKUbZc3U9hKUc181x9cw2GsS1sCo1cvu1mQgyf1X4CazcesaANXE so3xqkFrSqgpaToQ9Ml+6xqlG5omrUg= X-Google-Smtp-Source: AK7set+q1Rypp0NgfePlFF+Wpkhx5SbhJw05JWbfyqJV6rALiMBQGcwEHPnfre9GDc+aTMc21uLe/w== X-Received: by 2002:a05:6808:a81:b0:378:8159:5b7b with SMTP id q1-20020a0568080a8100b0037881595b7bmr1672115oij.49.1675796710406; Tue, 07 Feb 2023 11:05:10 -0800 (PST) Received: from ?IPV6:2600:1700:bd2c:b810::43? ([2600:1700:bd2c:b810::43]) by smtp.googlemail.com with ESMTPSA id dd6-20020a056808604600b00364e50e10c0sm6016072oib.56.2023.02.07.11.05.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Feb 2023 11:05:09 -0800 (PST) Content-Type: multipart/alternative; boundary="------------Dnp54NEpI1q1ImvwTQgoMx0u" Message-ID: <4edb5000-67fe-73af-66ba-2fe02b9f5a6f@gmail.com> Date: Tue, 7 Feb 2023 13:05:07 -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. --------------Dnp54NEpI1q1ImvwTQgoMx0u 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. --------------Dnp54NEpI1q1ImvwTQgoMx0u 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.
--------------Dnp54NEpI1q1ImvwTQgoMx0u--