When you drop an index in PostgreSQL, the database needs to lock the table to ensure that no other transactions modify the table while the index is being dropped. This is because the index is used to maintain the integrity of the data in the table, and if other transactions were allowed to modify the table while the index was being dropped, it could lead to data inconsistencies and other issues.
Locking the table during the index drop operation ensures that any changes to the table are temporarily blocked until the operation is completed, which helps to maintain the integrity of the data. This locking behavior is necessary to prevent other transactions from accessing the table while it is being modified.
However, locking the table can cause performance issues for other transactions that are waiting for the lock to be released. This is especially true for large tables or complex indexes, where the drop operation can take a significant amount of time. To mitigate this issue, PostgreSQL provides a DROP INDEX CONCURRENTLY command that allows you to drop an index without locking the table, by creating a new index and tracking table modifications (as OP correctly mentioned).
Is interesting to remark the following for DROP INDEX CONCURRENTLY:
- PostgreSQL creates a new index with a different name and then use this new index to catch any modifications that happen to the table during the drop operation.
- Dropping an index concurrently can be a resource-intensive operation.
- Concurrent index drops can cause increased disk space usage.
One extra thing you can do to speed up the process is to increase the maintenance_work_mem setting: PostgreSQL uses the maintenance_work_mem setting to control the amount of memory it can use for maintenance operations like index drops. Increasing this setting can speed up the drop operation by allowing PostgreSQL to allocate more memory for the drop operation.