Alter the datatype on all tables present in the database (bigint to varchar)

Started by Gambhir Singh14 days ago4 messagesgeneral
Jump to latest
#1Gambhir Singh
gambhir.singh05@gmail.com

Hi,

Please help me with the best way to change the datatype of a column in a
all tables in DB from bigint to varchar. Please consider the following
facts.

- DB Size - 75 TB
- Number of tables - ~100
- Some tables are partitioned and some are not partitioned.
- All partitioned tables are big in size.
- For reference, the largest partition size is 4 TB, similarly the size
of 10-12 partitions ranges between 1 to 4 TB.
- We have indexes and FK on tables.
- Physical Replication is configured with 2 standby DBs.

--
Thanks & Regards
Gambhir Singh

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Gambhir Singh (#1)
Re: Alter the datatype on all tables present in the database (bigint to varchar)

On 5/11/26 5:31 AM, Gambhir Singh wrote:

Hi,

Please help me with the best way to change the datatype of a column in a
all tables in DB from bigint to varchar. Please consider the following
facts.

1) Do not cross post to multiple lists.

2) What is the purpose behind this change?

* DB Size - 75 TB
* Number of tables - ~100
* Some tables are partitioned and some are not partitioned.
* All partitioned tables are big in size.
* For reference, the largest partition size is 4 TB, similarly the
size of 10-12 partitions ranges between 1 to 4 TB.
* We have indexes and FK on tables.
* Physical Replication is configured with 2 standby DBs.

--
Thanks & Regards
Gambhir Singh

--
Adrian Klaver
adrian.klaver@aklaver.com

#3Ron
ronljohnsonjr@gmail.com
In reply to: Gambhir Singh (#1)
Re: Alter the datatype on all tables present in the database (bigint to varchar)

On Thu, May 14, 2026 at 11:07 AM Gambhir Singh <gambhir.singh05@gmail.com>
wrote:

Hi,

Please help me with the best way to change the datatype of a column in a
all tables in DB from bigint to varchar. Please consider the following
facts.

- DB Size - 75 TB
- Number of tables - ~100
- Some tables are partitioned and some are not partitioned.
- All partitioned tables are big in size.
- For reference, the largest partition size is 4 TB, similarly the
size of 10-12 partitions ranges between 1 to 4 TB.
- We have indexes and FK on tables.

With tables that big, and all those FK constraints, logical replication is
probably your only hope for minimal downtime.

If you're running an older major version, this would also be a good time to
upgrade to 17.latest or 18.latest.

And Adrian's question is quite valid: *why*? Normally, people go the
other way, for efficiency.

- Physical Replication is configured with 2 standby DBs.

At least you have an existing server with enough disk space!!!

--
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Gambhir Singh (#1)
Re: Alter the datatype on all tables present in the database (bigint to varchar)

On 5/14/26 11:34 AM, Gambhir Singh wrote:

Please reply to list also.
Ccing list

Hi Adrian,

I've received this requirement from the application team. My main
concern is the partitioned tables.

I would think there would be a least some rationale for doing this, if
for no other reason then to determine whether this is the best solution.

From here:

https://www.postgresql.org/docs/17/sql-altertable.html

"Adding a column with a volatile DEFAULT or changing the type of an
existing column will require the entire table and its indexes to be
rewritten. As an exception, when changing the type of an existing
column, if the USING clause does not change the column contents and the
old type is either binary coercible to the new type or an unconstrained
domain over the new type, a table rewrite is not needed. However,
indexes must always be rebuilt unless the system can verify that the new
index would be logically equivalent to the existing one. For example, if
the collation for a column has been changed, an index rebuild is always
required because the new sort order might be different. However, in the
absence of a collation change, a column can be changed from text to
varchar (or vice versa) without rebuilding the indexes because these
data types sort identically. Table and/or index rebuilds may take a
significant amount of time for a large table; and will temporarily
require as much as double the disk space."

You also mentioned FKs, do these involve the columns being changed?

Thanks & Regards
Gambhir Singh

--
Adrian Klaver
adrian.klaver@aklaver.com