Engineering note · Bulk loading
Dropping indexes before COPY: a PostgreSQL reload nearly 4× faster
A PostgreSQL 17 reload of 10 million rows fell from 17:21.82 to 4:26 after dropping indexes before COPY. The trade-off: live queries waited during the overwrite.
In a recent PostgreSQL 17 tuning session, reloading 10 million rows into one table took 17 minutes 21.82 seconds. After dropping indexes before COPY, the reported total time for that table was 4 minutes 26 seconds—about 3.9× faster.
This was a full overwrite. The tables were truncated before loading, and live queries were stuck during the operation. That matters when deciding whether to use the same approach elsewhere.
What changed
The tables had eight indexes each. The schema included a string-based primary key, indexes on creation and update fields, and composite B-tree indexes.
The reload used TRUNCATE ... RESTART IDENTITY CASCADE, followed by PostgreSQL’s COPY command. In the faster approach, indexes were dropped before copying the data and recreated afterward.
| Single-table reload | Reported elapsed time |
|---|---|
| Indexes present during the load | 17:21.82 |
| Indexes dropped before the load | 4:26 total |
Dividing 1,041.82 seconds by 266 gives a speed ratio of 3.92. That is roughly 12 minutes 56 seconds saved, or a 74.5% reduction in elapsed time.
These two reported timings describe one table with 10 million rows and this index layout. No repeated-run measurements are included here, so they do not establish an average speedup or predict the improvement for a different workload.
COPY still has to maintain indexes
COPY avoids much of the overhead of inserting rows through separate statements. With indexes present, PostgreSQL still has to update them as the rows arrive.
Dropping them changes the order of work: load the rows first, then build the indexes over the populated table. PostgreSQL’s documentation explains that building indexes over existing data is faster than maintaining them incrementally during a load. It recommends this order for freshly created tables and says it can also help with large loads into existing ones. See PostgreSQL 17: populating a database.
Here, truncation meant the load started with empty tables. The comparison was between rebuilding the same dataset with indexes already present and postponing index construction until after the copy.
The overwrite blocked readers
The faster time did not make this an online refresh. Live queries waited during the overwrite.
TRUNCATE takes an ACCESS EXCLUSIVE lock, which blocks concurrent access to the affected tables, including ordinary reads. Locks are normally held until the transaction ends. If truncation and reloading share a transaction, readers can wait for that transaction to finish. See the PostgreSQL 17 locking documentation.
The rest of the truncation command also matters. RESTART IDENTITY resets sequences owned by columns of the truncated tables. CASCADE extends truncation to tables with foreign-key references to them. It can therefore empty more tables than the ones explicitly named. See the TRUNCATE reference.
For a workload that must keep serving reads, the refresh design needs separate attention. A faster blocking overwrite still leaves readers waiting.
Which indexes can you remove?
An ordinary secondary index and an index backing a primary key have different responsibilities. Removing a primary-key or unique constraint also removes its protection against invalid data during the load.
Treat constraint-backed indexes as schema changes, with their dependencies accounted for. DROP INDEX CONCURRENTLY cannot remove an index supporting a primary-key or unique constraint; it is not a general shortcut for this reload pattern. See the DROP INDEX reference.
Before adopting the approach, retain the exact index and constraint definitions and establish how they will be restored. A fast copy is not a successful reload if a required constraint cannot be recreated.
Measure until the table is usable again
For another workload, compare the complete operations:
- Reload with indexes present.
- Drop the selected indexes, reload, recreate indexes and constraints, and validate the result.
Include the rebuild in the timing, and measure how long readers wait separately. After a substantial reload, run ANALYZE so the planner has current statistics, as recommended in the PostgreSQL bulk-loading guidance.
This is worth testing when replacing a large dataset and the application can accommodate the interruption. For a small append to a large existing table, rebuilding indexes over the entire table may cost more than maintaining them during the append.
The decision point is when the table is ready for its users again: data loaded, required indexes and constraints restored, and queries able to proceed.