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_retriesto swap the names of the new and original index in a transaction, as renaming an index takes anACCESS EXCLUSIVElock 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 KEYorUNIQUEindexes.
- Currently, if with_lock_retriesexhausts all configured attempts to acquire the lock, it sets thelock_timeoutto 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 silv