We have a very big POSTGRES table containing more than 8 BILLION rows and growing at a very high rate (30 million rows per day).
Our Table is Partitioned on date. Each partition contains 6 months data except the first partition which contains data for almost 18 months.
Postgres version: PostgreSQL 12.11
Database specs: db.r6g.16xlarge (vCPU:64, memory:512, EBSBandwidth(Mbps):19000)
Table schema:
create table table_1
(
id bigint default nextval('table_1_id_seq'::regclass) not null,
user_id integer not null,
amount numeric(18, 2) not null,
date timestamp not null,
column_1 varchar not null,
column_2 varchar,
.
.
primary key (id, date)
) partition by RANGE (date);
This table also contains multiple indexes (including partial indexes)
create index index_1
on table_1 (column_1);
create index index_2
on table_1 (date, column_2, column_3);
create index index_3
on table_1 (column_4);
create index index_4
on table_1 (user_id, column_3);
create index index_5
on table_1 (user_id asc, date desc)
where (column_3 = ANY (ARRAY [1, 2, 3, 6, 7, 8, 10]));
create index index_6
on table_1 (user_id, column_2, column_3, column_5);
Initially we created amount column with data type numeric(18, 2). Now we need to support higher precision upto 6. So we need to change our type to numeric(22, 6).
Now running an Alter command like below one is taking a lot of time (hours):
NOTE: we ran this command on the table without dropping any indexes
ALTER TABLE table_1 ALTER COLUMN amount type numeric(22, 6);
Few of the different strategies that we are exploring:
Drop all the indexes, triggers and foreign keys while the update runs and recreate them at the end.
Create new table:
a. Create a new partitioned table with exact same configuration,
b. migrate data from old to new table.
c. create all indexes on new table.
d. remove old table.
NOTE: Our primary goal is to minimise the down time. If in doing so a BIGGER DB instance can help, we are open to explore that as well.
We want to explore if there are any other strategies to do this task OR in our current strategies what can we do extra or different to minimise our down time.