On 29th of March 2019, Peter Eisentraut committed patch:
REINDEX CONCURRENTLY This adds the CONCURRENTLY option to the REINDEX command. A REINDEX CONCURRENTLY on a specific index creates a new index (like CREATE INDEX CONCURRENTLY), then renames the old index away and the new index in place and adjusts the dependencies, and then drops the old index (like DROP INDEX CONCURRENTLY). The REINDEX command also has the capability to run its other variants (TABLE, DATABASE) with the CONCURRENTLY option (but not SYSTEM). The reindexdb command gets the --concurrently option. Author: Michael Paquier, Andreas Karlsson, Peter Eisentraut Discussion: https://www.postgresql.org/message-id/flat/-956b-4478-45ed-%402ndquadrant.com#
This is amazing.
Well, reindex can be used to fix broken indexes, or remove bloat from one.
But it required exclusive lock on the index for the duration of rebuild.
This was possible to work around by doing:
=$ create index concurrently new_index on ...; =$ drop index concurrently old_index; =$ alter index new_index rename to old_index;
Which also needed exclusive lock, but only for the rename process.
What's more – it wasn't that easy to do it for indexes that are basis of constraint, especially if it was primary key index on a table that has foreign keys pointing to it.
Now, luckily we can:
=$ create table s (id serial primary key, payload text); CREATE TABLE =$ create table d (id serial primary key, s_id int4 not null references s (id), payload text); CREATE TABLE =$ reindex index CONCURRENTLY s_pkey; REINDEX
This is absolutely great, thanks guys.