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 1tcbME-001Hil-9B for pgsql-general@arkaria.postgresql.org; Tue, 28 Jan 2025 02:29:42 +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 1tcbMB-00Fy2j-Ab for pgsql-general@arkaria.postgresql.org; Tue, 28 Jan 2025 02:29:39 +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 1tcbMA-00Fy2a-QD for pgsql-general@lists.postgresql.org; Tue, 28 Jan 2025 02:29:38 +0000 Received: from mail-yb1-xb31.google.com ([2607:f8b0:4864:20::b31]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tcbM7-001rVa-2r for pgsql-general@lists.postgresql.org; Tue, 28 Jan 2025 02:29:37 +0000 Received: by mail-yb1-xb31.google.com with SMTP id 3f1490d57ef6-e398484b60bso7716619276.1 for ; Mon, 27 Jan 2025 18:29:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738031375; x=1738636175; 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=NbegDzeEntsRX4ExN7UsYJeNHntQaJZTr7x5/DLBTDk=; b=j5kSJhUufWgWLBH5AqqKvGePN/7yZ3vY9+PivMqgMuC7LmLpycp4OFMX+UB4vxlJZU nuu43U1IoSKNxPR4uXbGY/ZUiKJQbesXT9k9jh4HV26jTp9nfVwkkaxSevFNpSbiK5XU DhgI0QZq50p868q8VbJ8MAxAyddAU6UDHRKUGsrODlWIgMfs4EQ+kwL8BGp4wrY4qEI+ ryLF0qOWvl9pPGJz1/sf7mYyqf1jcy3XzMF79wt8QXTdZX9cVhjG0sdlaRRJUm0XDqks tkcfrSZUqZTYlZoU60if6dLL4hiYguFCl8HsaSXKL3zBHamuc6Tma+BWfXxEjwqu4rRy GgFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738031375; x=1738636175; 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=NbegDzeEntsRX4ExN7UsYJeNHntQaJZTr7x5/DLBTDk=; b=okS+Npb/QCkhrD9v5WIyCgJWB3c4FGMsp4rmskrtqrvqIoPB6k+CHas6kXMCg/GRAW 5Ew6uVee3y2Tx2LNf790yjR8nHIFqBdsLk5VPJ+0SjwlpMeV60+0b/ZhRy9AbGBcnKKS L1yqQSqzFRtrBwRDTv08JUt8ABhsNZczvJB/V5Pdj3NDwO8PcTw1sursNtud9pa9gblV p8N3frVBi/Ku+fKJXTnkJoH/sMjH0HuZuy1iLyKjReJHx2bqSEdJ3l/H0wmA/0MPC9MS Jgnl61uRaFgeKOVXqum0yQgFDOc9ao2TdxB+sGi3kDzlv/RShKYuX6wM7fmFp6ly+ka2 YHwA== X-Gm-Message-State: AOJu0Yx1pWdvoJe4hh9SESMzSg2ElZWmeOkpPQSSi8n29ybNE20rkUyZ W1usx3OlL7QkOrD87uvjQEjGjtP9E/BvJ38z+rvFL1XNtZa8KmL3OFPaBgsniQwEjty7fHYAnPb Vogc+esbCOlmcH448PW1G0BbV1ic= X-Gm-Gg: ASbGncvYFarMLPw1umwlMVEqgQ6Lf4vCnMuP96etKupSZ9ChPjCFCyRMq9QPNHYMxBX 7/muR0IPDhc1WC4eQRG7g9BDClbCNvK2KdtenrEOG3LPjAdNCJSv4byHYr+rV7Yp4/IE/n5k= X-Google-Smtp-Source: AGHT+IFojPjupFplVAArNkC9wyKTeKtXLNKsXh93O76KKh2MyIUd1/F9/iamZWp3+JxdYwNveBibxPn4cpRee5NcWcM= X-Received: by 2002:a05:690c:6913:b0:6ef:6f74:9acc with SMTP id 00721157ae682-6f6eb65c96emr377871717b3.6.1738031375401; Mon, 27 Jan 2025 18:29:35 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Gus Spier Date: Mon, 27 Jan 2025 21:29:24 -0500 X-Gm-Features: AWEUYZkqRb_HlfRkv6pNUZ1QlcZ-ZPeJy2eSP5r_mxVUr5CP1AiMzGx6qVVOvX8 Message-ID: Subject: Re: Alter table fast To: Ron Johnson Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000bd4a92062cbaf64d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000bd4a92062cbaf64d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I don't think I qualify as an expert, but I can nominate a course of action for you to consider. Rather than convert an existing column from one data type to another, might it not be easier to simply recreate the table with the correct data type. There are caveats! You have to consider any referential integrity constraints. You have to have a LOT of available disk space. You really want to do this in batches. If you can swing those issues, you might have a shot. 1. CREATE TABLE LIKE ; 2. ALTER ALTER COLUMN TYPE ; 3. INSERT INTO SELECT -- recommend you do this in batches 4. DROP TABLE ; 5. ALTER TABLE RENAME to Regards, Gus On Thu, Jan 9, 2025 at 11:27=E2=80=AFAM Ron Johnson wrote: > On Thu, Jan 9, 2025 at 11:25=E2=80=AFAM veem v wrot= e: > >> Hello, >> It's postgres version 16.1, we want to convert an existing column data >> type from integer to numeric and it's taking a long time. The size of th= e >> table is ~50GB and the table has ~150million rows in it and it's not >> partitioned. We tried running the direct alter and it's going beyond hou= rs, >> so wanted to understand from experts what is the best way to achieve thi= s? >> >> > Out of curiosity, why NUMERIC(15,0) instead of BIGINT? > > -- > Death to , and butter sauce. > Don't boil me, I'm still alive. > lobster! > --000000000000bd4a92062cbaf64d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I don't think I qualify as an expert, but I can nomina= te a course of action for you to consider.

Rather than c= onvert an existing column from one data type to another, might it not be ea= sier to simply recreate the table with the correct data type.
The= re are=C2=A0 caveats!=C2=A0

You have to consider a= ny referential integrity constraints.
You have to have a LOT of a= vailable disk space.
You really want to do this in batches.
=

If you can swing those issues, you might have a shot.

=C2=A01. CREATE TABLE <table_name_new> LIKE &= lt;original_table_name>;
2. ALTER <table_name_new> ALTER= COLUMN <column_name> TYPE <new_data_type>;
3. INSERT= INTO <table_name_new> SELECT <column list w. the target column_na= me cast as the new type>
-- recommend you do this in batches
4. DROP TABLE <original_table_name>;
5. ALTER TABL= E <table_name_new> RENAME to <original_table_name>
Regards,
Gus

On Thu, Jan = 9, 2025 at 11:27=E2=80=AFAM Ron Johnson <ronljohnsonjr@gmail.com> wrote:
On Thu= , Jan 9, 2025 at 11:25=E2=80=AFAM veem v <veema0000@gmail.com> wrote:
Hello,
It's postgres version 16.1, we want to convert an= existing column data type from integer to numeric and it's taking a lo= ng time. The size of the table is ~50GB and the table has ~150million rows = in it and it's not partitioned. We tried running the direct alter and i= t's going beyond hours, so wanted to understand from experts what is th= e best way to achieve this?


Out of curiosity, why NUMERIC(15,0) instead of BIGINT= ?=C2=A0

--=
Dea= th to <Redacted>, and butter sauce.
Don't boil me, I'm st= ill alive.
<Redacted> lobster!
--000000000000bd4a92062cbaf64d--