Rake task to reindex an index
The initial iteration will be to implement a Rake task which takes a single index name as its argument, and recreates the matching index. At a high level this means:
- Concurrently create a new index with temporary name matching the existing index definition.
- Use
with_lock_retries
to swap the names of the new and original index in a transaction, as renaming an index takes anACCESS EXCLUSIVE
lock on the index (at least in PG 11). - Drop the original index.
Couple error cases that should be handled:
- Disallow the rake task to operate on
PRIMARY KEY
orUNIQUE
indexes. - Currently, if
with_lock_retries
exhausts all configured attempts to acquire the lock, it sets thelock_timeout
to 0 and proceeds, to ensure the migration completes. Since a reindex is a non-critical operation, instead cleanup the temporary index and fail the rake task if the lock cannot be acquired.
Edited by Joshua Lambert