1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-22 02:52:08 +03:00

Rework handling of invalid indexes with REINDEX CONCURRENTLY

Per discussion with others, allowing REINDEX INDEX CONCURRENTLY to work
for invalid indexes when working directly on them can have a lot of
value to unlock situations with invalid indexes without having to use a
dance involving DROP INDEX followed by an extra CREATE INDEX
CONCURRENTLY (which would not work for indexes with constraint
dependency anyway).  This also does not create extra bloat on the
relation involved as this works on individual indexes, so let's enable
it.

Note that REINDEX TABLE CONCURRENTLY still bypasses invalid indexes as
we don't want to bloat the number of indexes defined on a relation in
the event of multiple and successive failures of REINDEX CONCURRENTLY.

More regression tests are added to cover those behaviors, using an
invalid index created with CREATE INDEX CONCURRENTLY.

Reported-by: Dagfinn Ilmari Mannsåker, Álvaro Herrera
Author: Michael Paquier
Reviewed-by: Peter Eisentraut, Dagfinn Ilmari Mannsåker
Discussion: https://postgr.es/m/20190411134947.GA22043@alvherre.pgsql
This commit is contained in:
Michael Paquier
2019-04-17 09:33:51 +09:00
parent c8e0f6bbdb
commit a6dcf9df4d
5 changed files with 80 additions and 33 deletions

View File

@ -2776,11 +2776,6 @@ ReindexRelationConcurrently(Oid relationOid, int options)
}
case RELKIND_INDEX:
{
/*
* For an index simply add its Oid to list. Invalid indexes
* cannot be included in list.
*/
Relation indexRelation = index_open(relationOid, ShareUpdateExclusiveLock);
Oid heapId = IndexGetRelation(relationOid, false);
/* A shared relation cannot be reindexed concurrently */
@ -2801,25 +2796,13 @@ ReindexRelationConcurrently(Oid relationOid, int options)
/* Track the heap relation of this index for session locks */
heapRelationIds = list_make1_oid(heapId);
/*
* Save the list of relation OIDs in private context. Note
* that invalid indexes are allowed here.
*/
indexIds = lappend_oid(indexIds, relationOid);
MemoryContextSwitchTo(oldcontext);
if (!indexRelation->rd_index->indisvalid)
ereport(WARNING,
(errcode(ERRCODE_INDEX_CORRUPTED),
errmsg("cannot reindex concurrently invalid index \"%s.%s\", skipping",
get_namespace_name(get_rel_namespace(relationOid)),
get_rel_name(relationOid))));
else
{
/* Save the list of relation OIDs in private context */
oldcontext = MemoryContextSwitchTo(private_context);
indexIds = lappend_oid(indexIds, relationOid);
MemoryContextSwitchTo(oldcontext);
}
index_close(indexRelation, NoLock);
break;
}
case RELKIND_PARTITIONED_TABLE: