Postgres:30亿行数据的大表自增主键用完了怎么办?- Option 1: 直接更改字段类型(英文版)

89 阅读1分钟

Option 1: Change the datatype of column directly

Overview of the process

Change the datatype by using ALTER COLUMN.

Risks:

  • Acquires an access exclusive lock which can be problematic for big tables.
  • Causes table rewrite where Postgres rewrites every single row of the table, which is a very expensive operation (both in terms of disk I/O and wall clock time).

Steps:

  • Run alter table to change datatype from int to bigint

Details of the process

Environment

Operating SystemDisk typeAmount of dataMaintenance time
MAC Big SurSSD0.5 billion1h3m
  • Run alter table to change datatype from int to bigint
-- 1 h 3 m 56 s 372 ms alter table trade_logs alter column id type bigint using id::bigint;