I have a large Postgres table with 2+ billion entries (1.5TB) and mostly non-null, char var columns. To speed up inserts, I dropped the indexes before bulk uploading. However, it is now taking forever for the b-tree indexes to be created. For one of the runs that I cut short, it had spent >12 hours creating the indexes.
Sample table and indexes that I'm trying to make:
Column | Type | Modifiers
-----------------------+-----------------------------+-----------
name | character varying | not null
id | character varying |
lifecycle_id | character varying |
dt | character varying |
address | character varying |
...
Indexes:
"name_idx" PRIMARY KEY, btree (name)
"id_idx" btree (rec_id)
"lifecycle_id_idx" btree (lifecycle_id)
The actual table has 18 columns. I've set the maintenance_work_mem to 15GB. This is running on Postgres 9.6.11 on RDS. The instance class is db.m4.4xlarge.
Since there are three indexes, it would be hard to sort the data before inserting. Would it be faster to just insert the data without dropping the indexes? Any other suggestions for speeding up the index creation?