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 1kksFA-00048L-H2 for pgsql-general@arkaria.postgresql.org; Thu, 03 Dec 2020 17:18:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kksF9-0001pm-Ed for pgsql-general@arkaria.postgresql.org; Thu, 03 Dec 2020 17:18:11 +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 1kksF9-0001nx-6x for pgsql-general@lists.postgresql.org; Thu, 03 Dec 2020 17:18:11 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kksF6-0008Tu-LR for pgsql-general@lists.postgresql.org; Thu, 03 Dec 2020 17:18:10 +0000 Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 75900265109 for ; Thu, 3 Dec 2020 09:18:05 -0800 (PST) Date: Thu, 3 Dec 2020 09:18:05 -0800 (PST) From: Rich Shepard To: PostgreSQL General Subject: Re: Alter the column data type of the large data volume table. In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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