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.94.2) (envelope-from ) id 1sbejm-000Wof-8r for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2024 11:21:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sbejj-006VZY-GY for pgsql-general@arkaria.postgresql.org; Wed, 07 Aug 2024 11:21:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sbejj-006VZP-5Z for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2024 11:21:47 +0000 Received: from mail-vk1-xa36.google.com ([2607:f8b0:4864:20::a36]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sbejd-003cVT-0n for pgsql-general@lists.postgresql.org; Wed, 07 Aug 2024 11:21:46 +0000 Received: by mail-vk1-xa36.google.com with SMTP id 71dfb90a1353d-4f6c136a947so762557e0c.1 for ; Wed, 07 Aug 2024 04:21:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723029699; x=1723634499; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=YJxhhznQZYhpdFK1gkKmCWMakKp8iTKCOp7Ul/CCMXo=; b=dIW4gMxkfmnT1OkXjZEBYBzejBIhmVv1WQ/XRzjZrNzYsn1L9Do90pUafOjGkdZAvw GiJmm9NS6RfLKw7yzwORYDlx3ChLWwzTfu+NSJ1F8XBRySzsSaPCsY/MJIAKzyrFX9it IC0GuSRgXbZBvduaG1CJm9NrdsFzqSp2cp0f757CrNIiOswOqRn0oGt1T1Yc545LTPGj /Vn9CSv6hWhR7ivc7GJPJ4fLhFmbUI5XQfekM3BXgbzRZLZlAi+/vhdVFRjuRX5gfDqj Qw0dOfIQy/eHmut88W85HnmvTwNqpqlfp6B8qtthekPSdGCTo32R2ZIEGKFBua5uOLGa eAyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723029699; x=1723634499; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YJxhhznQZYhpdFK1gkKmCWMakKp8iTKCOp7Ul/CCMXo=; b=g7sRHbAGPT1IBVdYCgekrvC7NreF8HZjFUKYzgs/Jw8JwIGxiNvKCQlKwon7G9bvdl 3TnHNzF9N7VCE1Jy2Eb/kmmPPKumh530zBXoHzGA1/eIwP0Uf38pisx7BihqFM5OGHfP OxJSZkCeL+ty+LpA28O4FAvY0B59JEN+JEPvMZpDcWaseH3HeVwQHiDWSMW4THc7OfBa 3ocHrQ5s23L557I17phNGgu7st3IG6YXvp8AdIE1cJSeLSPSIXucge2dMQpTHuSb3qNs 4CEwEKzB1eIePya8UnJNsBKx72CHdf4RLdsLClU8RyKi7CV5sBPAB/m8CZhogpRaITP+ /uyw== X-Gm-Message-State: AOJu0YzkrzYGQvrzhdICUVy2U+onAwZayd63p4Xm6IqxOKyeNFqetpZx 1wjI51WcWVFQfEZcAMeKlj8JRYLJ0nc5ZPHxrDkNOt6CGz3788jEWO0AqHpq/4Vx/D8VdrPSjiy qTeXwbGV+cqTgle6E9PxWW7BA3EA= X-Google-Smtp-Source: AGHT+IFfl12Th6ZTeuMTJx5Mb44gxl8J9u6620VHYoLFAK8wW8NI0T5NCwoxbZ3gk5eJRw+DiPcefdZLSFQIUiDl6Lw= X-Received: by 2002:a05:6122:1806:b0:4f5:f65:26be with SMTP id 71dfb90a1353d-4f89fe7d3camr23165836e0c.0.1723029699097; Wed, 07 Aug 2024 04:21:39 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: sud Date: Wed, 7 Aug 2024 16:51:26 +0530 Message-ID: Subject: Re: Column type modification in big tables To: Lok P Cc: pgsql-general Content-Type: multipart/alternative; boundary="00000000000026fd95061f161da7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000026fd95061f161da7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Aug 7, 2024 at 4:39=E2=80=AFPM Lok P wrote: > Hello all, > We have a postgres table which is a range partitions on a timestamp colum= n > having total size ~3TB holding a total ~5billion rows spanning across ~15= 0 > daily partitions and there are ~140+columns in the table. Also this table > is a child to another partition table. And we have partition creation > handled through pg_partman extension on this table. > > We have a requirement of modifying the existing column lengths as below . > So doing it directly through a single alter command will probably scan an= d > rewrite the whole table which may take hours of run time. > > So trying to understand from experts what is the best possible way to > tackle such changes in postgres database? And if any side effects we may > see considering this table being child to another one and also dependency > with pg_partman extension. > > two of the columns from varchar(20) to numeric(3) > one of the columns from varchar(20) to varchar(2) > one of the columns from Number(10,2) to Numeric(8,2) > > > Others may correct but i think, If you don't have the FK defined on these columns you can do below. --Alter table add column which will be very fast within seconds as it will just add it to the data dictionary. ALTER TABLE tab1 ADD COLUMN new_column1 NUMERIC(3), new_column2 varchar2(3); *-- Back populate the data partition wise and commit, if it's really needed= * UPDATE tab1_part1 SET new_column1 =3D CAST(old_column1 AS NUMERIC(3)), new_column2 =3D CAST(old_column2 AS varchar2(3)) ; commit; UPDATE tab1_part2 SET new_column1 =3D CAST(old_column1 AS NUMERIC(3)), new_column2 =3D CAST(old_column2 AS varchar2(3)) ; commit; UPDATE tab1_part3 SET new_column1 =3D CAST(old_column1 AS NUMERIC(3)), new_column2 =3D CAST(old_column2 AS varchar2(3)) ; commit; ..... *--Alter table drop old columns which will be very fast within seconds as it will just drop it from the data dictionary.* ALTER TABLE your_table DROP COLUMN old_column1, DROP COLUMN old_column2; --00000000000026fd95061f161da7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Wed, Aug 7, 2024 at 4:39=E2=80=AFPM Lo= k P <loknath.73@gmail.com>= ; wrote:
Hello all,
We have a postgres table which is a range partitio= ns on a timestamp column having total size ~3TB holding a total ~5billion r= ows spanning across ~150 daily partitions and there are ~140+columns in the= table. Also this table is a child to another partition table. And we have = partition creation handled through pg_partman extension on this table.
<= br>We have a requirement of modifying the existing column lengths as below = . So doing it directly through a single alter command will probably scan an= d rewrite the whole table which may take hours of run time.

So tryi= ng to understand from experts what is the best possible way to tackle such = changes in postgres database? And if any side effects we may see considerin= g this table being child to another one and also dependency with pg_partman= extension.

two of the columns from varchar(20) to numeric(3)
on= e of the columns from varchar(20) to varchar(2)
one of the columns from = Number(10,2) to Numeric(8,2)


<= /blockquote>


Others may correct but i think, I= f you don't have the FK defined on these columns you can do below.
<= br>
--Alter table add column which will be very fast within seconds as i= t will just add it to the data dictionary.

ALTER TABLE tab1 ADD COLU= MN new_column1 NUMERIC(3),=C2=A0 =C2=A0new_column2 varchar2(3);
<= br>
-- Back populate the data partition wise and commit, if it= 's really needed

UPDATE tab1_part1 SET new_column1 =3D CAST(= old_column1 AS NUMERIC(3)), new_column2 =3D CAST(old_column2 AS varchar2(3)= ) ;
commit;
UPDATE tab1_part2 SET new_column1 =3D CAST(old_column1 AS= NUMERIC(3)), new_column2 =3D CAST(old_column2 AS varchar2(3)) ;
commit;=
UPDATE tab1_part3 SET new_column1 =3D CAST(old_column1 AS NUMERIC(3)), = new_column2 =3D CAST(old_column2 AS varchar2(3)) ;
commit;
.....
<= br>--Alter table drop old columns which will be very fast within seconds= as it will just drop it from the data dictionary.

ALTER TABLE y= our_table DROP COLUMN old_column1, DROP COLUMN old_column2;=C2=A0
--00000000000026fd95061f161da7--