Re: [GENERAL] Speed of conversion from int to bigint - Mailing list pgsql-general

From Tomas Vondra
Subject Re: [GENERAL] Speed of conversion from int to bigint
Date
Msg-id 27ff6a7d-e7b9-5597-ca83-d3a1e83c57d4@2ndquadrant.com
Whole thread Raw
In response to [GENERAL] Speed of conversion from int to bigint  (Jonathan Moules <jonathan-lists@lightpear.com>)
Responses Re: [GENERAL] Speed of conversion from int to bigint  ("Jehan-Guillaume (ioguix) de Rorthais" <ioguix@free.fr>)
List pgsql-general

On 09/27/2017 10:08 AM, Jonathan Moules wrote:
> Hi,
> (Postgres 9.5 and 9.6)
> We have a table of about 650million rows. It's a partitioned table, with
> two "child" tables. We want to change its primary key type from int to
> bigint while retaining the current values.
> 
> We're using this:
> 
> ALTER TABLE dta.my_table ALTER column table_id TYPE bigint;
> 
> But it's taking a very long time, and locking the database. We're going
> to need to do this in production as well, so a long-term table-lock
> isn't workable.

It's taking very long time, because it does a full-table rewrite while
holding AccessExclusiveLock on it. Which is the strongest lock mode.

> Is there anything we can do to speed things up? How long is this likely
> to take?
> 

What you can do, is roughly this:

---
ALTER TABLE my_table ADD COLUMN new_table_id TYPE bigint;

-- do this in batches, so that a single transaction does not update
-- all the rows
UPDATE my_table SET new_table_id = table_id;

-- build unique index on the new column
CREATE INDEX CONCURRENTLY my_table_2_pkey ON my_table (new_table_id);

BEGIN;
-- switch the PK constraint
ALTER TABLE my_table DROP CONSTRAINT my_table_pkey;
ALTER TABLE my_table ADD CONSTRAINT my_table_pkey PRIMARY KEY
(new_table_id) USING my_table_2_pkey;

-- drop, rename the columns
ALTER TABLE my_table DROP COLUMN table_id;
ALTER TABLE my_table ALTER COLUMN new_table_id RENAME TO table_id;
COMMIT;

You may need to tweak this to handle the inheritance tree.

-- 
Tomas Vondra                  http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

pgsql-general by date:

Previous
From: Denisa Cirstescu
Date:
Subject: Re: [GENERAL] Catching errors inside a LOOP is causing performance issues
Next
From: George Neuner
Date:
Subject: Re: [GENERAL] Speed of conversion from int to bigint