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 1tX5dE-003pXY-PQ for pgsql-general@arkaria.postgresql.org; Sun, 12 Jan 2025 21:36:29 +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 1tX5dC-0039Zr-R1 for pgsql-general@arkaria.postgresql.org; Sun, 12 Jan 2025 21:36:27 +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.94.2) (envelope-from ) id 1tX5dC-0039Zj-8y for pgsql-general@lists.postgresql.org; Sun, 12 Jan 2025 21:36:26 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tX5dB-00047X-1I for pgsql-general@lists.postgresql.org; Sun, 12 Jan 2025 21:36:25 +0000 Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-5d122cf8dd1so6178154a12.2 for ; Sun, 12 Jan 2025 13:36:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736717782; x=1737322582; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=ITocZJC1xrkRCH2EntE5mxFKZu942wGUE1EErLInsgM=; b=Wvebiqs7NeWF6zw8pu0B409NfrJ6OeRPy/TnnZZdUWVMpqh+sqq9GgXcixPtkb3mBY Ie+NDoX36mPzdcaPcBS1gDq9cCrPAMS1OTQ5qJQebEHB12BNTUpsYZ6LUJqv2wQmwYnd aDCJoTkhKJ1HKp3F718DVK78RsUcXPTpeYMDopaquTa9b+ai9BuQA8n/Yqsxzgm55wWU cgOQebJCeropQQcXhQ4UuEK0MLWBWrtUNVfr8TOMA+fP+g1Bs2NOoRZyJTwYmDaiLT/+ LZZN7+FlbwTLYqgkfMmGmYe9K/MR778G6lhw+In5qAulfO/2y8iAj+8E6CwUyo0pZ51M lFqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736717782; x=1737322582; h=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=ITocZJC1xrkRCH2EntE5mxFKZu942wGUE1EErLInsgM=; b=Ll6cRikCEwQnemvRVQMnNwIoqfelvzM5E1Hngiq5nZ+kkrgyg7MZjRWe+SREn8rY22 2a5i3Q2fU8po7Q+gsOGrc6dyaV/otRML5vFcEwiM8mfCchg2bfo3MMZ8WWZxUrqS0/yX ouenZSLJ32Tl7BF2gndzhRygZGBBs7DCN3fY0xNPrLL3QTYEv8+6jlHRmpRMuR6i+LDi P0x5XVny0GG7ipzD1j+vzHRb5khPCqsGEVSr3jWdIInK1Fe4f5md0DzuBw9qBuUH+S1P D/vcR3VWwm4CoaM8ePNnXPR1HBQWvzQedzSv8WvcOn0cpVylMpI0XrKEV20r8s+FwOwx r7Pg== X-Gm-Message-State: AOJu0YyXByURzT/TRG5m5JD4gL+Jy9uCei+QfGu84OJffi4nkzyPJoqL eRAWDiQaZ7dBubURMjLFtmopQzLTUeHYbsx7/9+cQuJyfI0+bCIC8/cWJuTHCPSYhtJLu6yPdEK KQVeKRoNmE0xcWx2kRNhvRqysjUOB/DMg X-Gm-Gg: ASbGnctDbtrRpiha+d32ukXQaSbeGbFTKQQIgYN3Sd6qUsGzthnUm5enN6RoA0/+6Pz cPJv0domeA/YG+zw/gXLFVrWMVW8f9/6t21VnzC8= X-Google-Smtp-Source: AGHT+IEZc9SC0HF5/uPwCB/8YAUikPXU2FmvCIzR8EYcaUEddzJ1MD5qRBpxeOZLkT7DS2U/+ifcweWrt66fHBrNGEw= X-Received: by 2002:a05:6402:400b:b0:5d0:e63e:21ca with SMTP id 4fb4d7f45d1cf-5d972e1c0d3mr17696801a12.19.1736717782363; Sun, 12 Jan 2025 13:36:22 -0800 (PST) MIME-Version: 1.0 References: <9e3d705e-df1d-4d32-b5ba-718f492cb1cc@gmx.net> <20250112211901.r6cealpdlsovwahv@hjp.at> In-Reply-To: <20250112211901.r6cealpdlsovwahv@hjp.at> From: Marco Torres Date: Sun, 12 Jan 2025 14:36:10 -0700 X-Gm-Features: AbW1kvbwOQw1Rvv2QIMsYovVIOfIkMxcsL4dbeZV6OANalN8U9fYJtUkloj2JfY Message-ID: Subject: Re: Alter table fast To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000007e3505062b891e34" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000007e3505062b891e34 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable This is the right approach, Peter J. Holzer, from a well season DBA perspective "ALTER TABLE working_table ADD COLUMN B INTEGER ; UPDATE working_table SET B =3D A;" Bare in mind the indexes or existing references to an from other tables and act accordingly-- define the new and drop the old. Good luck. On Sun, Jan 12, 2025, 2:20=E2=80=AFPM Peter J. Holzer wr= ote: > On 2025-01-09 20:52:27 +0100, shammat@gmx.net wrote: > > Am 09.01.25 um 20:17 schrieb veem v: > > > > Out of curiosity, why NUMERIC(15,0) instead of BIGINT? > > > > > > It's for aligning the database column types to the data model and > > > it's happening across all the upstream downstream systems. I was > > > thinking if this can be made faster with the single line alter > > > statement "Alter table alter column type > > > numeric(15,0) USING ::NUMERIC(15,0);" > > > > Hmm, I would rather change numeric(15,0) to bigint if I had to "align" > types across systems. > > I'm also wondering what "the data model" is. > > If I have numeric(15,0) in an abstract data model, that means that I > expect values larger than 99,999,999,999,999 but at most > 999,999,999,999,999. That seems to be oddly specific and also somewhat > at odds with reality when until now there apparently haven't been any > values larger than 2,147,483,647. What kind of real world value could > suddenly jump by more than 5 orders of magnitude but certainly not by 7? > > A bigint is much less precise (more than 2,147,483,647 but not more > than 9,223,372,036,854,775,807) and therefore more suitable for values > where you don't really know the range. > > However, for the problem at hand, I doubt it makes any difference. > Surely converting a few million values takes much less time than > rewriting a 50 GB table and all its indexes. > > So there isn't really a faster way to do what Veem wants. There may > however be less disruptive way: He could create a new column with the > new values (which takes at least as long but can be done in the > background) and then switch it over and drop the old column. > > hp > > -- > _ | Peter J. Holzer | Story must make more sense than reality. > |_|_) | | > | | | hjp@hjp.at | -- Charles Stross, "Creative writing > __/ | http://www.hjp.at/ | challenge!" > --0000000000007e3505062b891e34 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
This is the right approach, Peter J. Holzer, from a = well season DBA perspective "ALTER TABLE working_table
ADD COLUMN B INTEGER=C2=A0; UPDATE working_table
SET B =3D A;"

Bare in mind the indexes or existing references to an from other tables a= nd act accordingly-- define the new and drop the old.

Good luck.

On Sun, Jan 12, 2025, 2:20=E2=80=AFPM Peter J. Holzer <= ;hjp-pgsql@hjp.at> wrote:
On 2025-01-09 20:52:27 +0100, shammat@gmx.ne= t wrote:
> Am 09.01.25 um 20:17 schrieb veem v:
> > > Out of curiosity, why NUMERIC(15,0) instead of BIGINT?
> >
> > It's for aligning the database column types to the data model= and
> > it's happening across all the upstream downstream systems. I = was
> > thinking if this can be made=C2=A0 faster with the single line al= ter
> > statement "Alter table <table_name> alter column <c= olumn_name> type
> > numeric(15,0) USING <column_name>::NUMERIC(15,0);"
>
> Hmm, I would rather change numeric(15,0) to bigint if I had to "a= lign" types across systems.

I'm also wondering what "the data model" is.

If I have numeric(15,0) in an abstract data model, that means that I
expect values larger than 99,999,999,999,999 but at most
999,999,999,999,999. That seems to be oddly specific and also somewhat
at odds with reality when until now there apparently haven't been any values larger than 2,147,483,647. What kind of real world value could
suddenly jump by more than 5 orders of magnitude but certainly not by 7?
A bigint is much less precise (more than 2,147,483,647 but not more
than 9,223,372,036,854,775,807) and therefore more suitable for values
where you don't really know the range.

However, for the problem at hand, I doubt it makes any difference.
Surely converting a few million values takes much less time than
rewriting a 50 GB table and all its indexes.

So there isn't really a faster way to do what Veem wants. There may
however be less disruptive way: He could create a new column with the
new values (which takes at least as long but can be done in the
background) and then switch it over and drop the old column.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 hp

--
=C2=A0 =C2=A0_=C2=A0 | Peter J. Holzer=C2=A0 =C2=A0 | Story must make more = sense than reality.
|_|_) |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 |
| |=C2=A0 =C2=A0| hjp@hjp.at=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A0 --= Charles Stross, "Creative writing
__/=C2=A0 =C2=A0| http://www.hjp.at/ |=C2=A0 =C2=A0 =C2=A0 =C2=A0ch= allenge!"
--0000000000007e3505062b891e34--