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 1kkskz-0005Qo-1P for pgsql-general@arkaria.postgresql.org; Thu, 03 Dec 2020 17:51:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkskw-0006aA-3o for pgsql-general@arkaria.postgresql.org; Thu, 03 Dec 2020 17:51:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kkskv-0006a2-Oy for pgsql-general@lists.postgresql.org; Thu, 03 Dec 2020 17:51:01 +0000 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kkskr-0000IF-Qq for pgsql-general@lists.postgresql.org; Thu, 03 Dec 2020 17:51:01 +0000 Received: by mail-oi1-x22b.google.com with SMTP id x16so3090677oic.3 for ; Thu, 03 Dec 2020 09:50:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=mF49G2HNYMRmxmW8/RW8dVKFtCNfBhQEvjzGi6ZDI7s=; b=aokODigHg9Y+KgfLy59SbjcidwrZoPZyuoxEVW+xVLghGdWVOaNbQRdmreKFJSHoj3 tPg6Ot0rYsdYIex4nzsOTNg4dTg+sLEjlteelkEBqBaGTOmUNPFaTU8FrQtXaBnjYeum 1WQ/uJZ0XypmAgeXZaX0pehWcyLzb3+bfmD2firkasAGaL9gOVLx7kPYiLzyzpgcGw08 0BwshSNSlz8bW20rGPqw2+1x7JA6jkFdMRBzVixoN5VeqQ/x1Je3iaMeaaGSbk6t6VZO yWil15gRbZKZlVFzLNhga/lOfbqGaFM7tRPOFtev15TFKpBySsJ2sdD69Oh8y4bOXWLc 2H4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=mF49G2HNYMRmxmW8/RW8dVKFtCNfBhQEvjzGi6ZDI7s=; b=ppuzYNVLbX/pKZ0oRf/TWFCtH+NgQ/tfu37Hy1yKMP9GWGEx1pBZBvI+jLEOkkLyzJ 0LcSakV+zD2zF7FU3Dvr0TAM6VP54kZFYFZc+NbtZQLaANAGMgdn6kvYunVbt2GLThao wSoTRuNweQbKXaKuadxvctEzCybgYSRbicNNwHmP5Q5rXvVZtVcwGcYHFMKe3hrcUinv lV/TSKwW2qJEdTdoRr6kPgv8RflSMlUNYJNKNlMAx/s5hV3XIiX3fQD74/UnpKDvnuA9 33yi3Z0Qb7tVxfqcjSZlz2wrgBwhR1laHzdK2V+2gcyYURwla6E89CnXng4Z3C3vgH4B U10Q== X-Gm-Message-State: AOAM531S4gcy1uuh00peUmf+fSSn+489tp8nEqS1XVnJZYu+5nz9Il6I sn29NYxArQ0MQqmUWIIQD8BnifKtko4qJA== X-Google-Smtp-Source: ABdhPJzfUTRtoAE/ioycHWaFvSGMU7IgjKcbD9yrMHs0AUT5n+Zfs98woEfa49S5mRKRXWAeITMs4g== X-Received: by 2002:aca:2301:: with SMTP id e1mr101153oie.8.1607017855532; Thu, 03 Dec 2020 09:50:55 -0800 (PST) Received: from [192.168.1.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id j18sm14872otn.23.2020.12.03.09.50.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 03 Dec 2020 09:50:54 -0800 (PST) Subject: Re: Alter the column data type of the large data volume table. To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: Date: Thu, 3 Dec 2020 11:50:54 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------5A705C38CFE7C309B1857353" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------5A705C38CFE7C309B1857353 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 12/3/20 11:26 AM, Michael Lewis wrote: > On Thu, Dec 3, 2020 at 10:18 AM Rich Shepard > wrote: > > On Thu, 3 Dec 2020, Michael Lewis wrote: > > > On Wed, Dec 2, 2020 at 11:53 PM charles meng > wrote: > > >> I have a table with 1.6 billion records. The data type of the > primary key > >> column is incorrectly used as integer. I need to replace the type > of the > >> column with bigint. Is there any ideas for this? > > > You can add a new column with NO default value and null as default > and have > > it be very fast. Then you can gradually update rows in batches (if on > > PG11+, perhaps use do script with a loop to commit after X rows) to > set the > > new column the same as the primary key. Lastly, in a transaction, update > > any new rows where the bigint column is null, and change which column is > > the primary key & drop the old one. This should keep each transaction > > reasonably sized to not hold up other processes. > > Tell me, please, why > > ALTER TABLE ALTER COLUMN SET DATA TYPE BIGINT > > will not do the job? > > I've found some varchar columns in a couple of tables too small and > used the > above to increase their size. Worked perfectly. > > Regards, > > Rich > > > Afaik, it will require an access exclusive lock for the entire time it > takes to re-write the 1.6 billion rows and update all indexes. That sort > of lock out time doesn't seem workable in many production systems. Yet another argument for partitioning! 1. Split split all the partitions from the main table, 2. drop the PK, 3. do all the ALTER statements in parallel, 4. recreate the PK indices, then 5. join them back to the main table. Not instant, but faster than updating 1.6Bn rows in one single giant statement. (Of course, that doesn't help OP with his current problem.) -- Angular momentum makes the world go 'round. --------------5A705C38CFE7C309B1857353 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit On 12/3/20 11:26 AM, Michael Lewis wrote:
On Thu, Dec 3, 2020 at 10:18 AM Rich Shepard <rshepard@appl-ecosys.com> wrote:
On Thu, 3 Dec 2020, Michael Lewis wrote:

> On Wed, Dec 2, 2020 at 11:53 PM charles meng <xlyybz@gmail.com> wrote:

>> I have a table with 1.6 billion records. The data type of the primary key
>> column is incorrectly used as integer. I need to replace the type of the
>> column with bigint. Is there any ideas for this?

> You can add a new column with NO default value and null as default and have
> it be very fast. Then you can gradually update rows in batches (if on
> PG11+, perhaps use do script with a loop to commit after X rows) to set the
> new column the same as the primary key. Lastly, in a transaction, update
> any new rows where the bigint column is null, and change which column is
> the primary key & drop the old one. This should keep each transaction
> reasonably sized to not hold up other processes.

Tell me, please, why

ALTER TABLE <tablename> ALTER COLUMN <columnname> SET DATA TYPE BIGINT

will not do the job?

I've found some varchar columns in a couple of tables too small and used the
above to increase their size. Worked perfectly.

Regards,

Rich

Afaik, it will require an access exclusive lock for the entire time it takes to re-write the 1.6 billion rows and update all indexes. That sort of lock out time doesn't seem workable in many production systems.

Yet another argument for partitioning!

1. Split split all the partitions from the main table,
2. drop the PK,
3. do all the ALTER statements in parallel,
4. recreate the PK indices, then
5. join them back to the main table.

Not instant, but faster than updating 1.6Bn rows in one single giant statement.

(Of course, that doesn't help OP with his current problem.)

--
Angular momentum makes the world go 'round.
--------------5A705C38CFE7C309B1857353--