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

From: Gambhir Singh <[email-masked]>
Date: Mon, 11 May 2026 18:01:05 +0530

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

---

From: Adrian Klaver <[email-masked]>
Date: Thu, 14 May 2026 10:00:28 -0700

> all tables in DB from bigint to varchar. Please consider the following 

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
[email-masked]

---

From: Ron Johnson <[email-masked]>
Date: Thu, 14 May 2026 13:20:12 -0400

On Thu, May 14, 2026 at 11:07 AM Gambhir Singh <[email-masked]>
wrote:

>    - For reference, the largest partition size is 4 TB, similarly the
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.

> 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!

---

From: Adrian Klaver <[email-masked]>
Date: Thu, 14 May 2026 13:11:45 -0700

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?