mirror of
https://github.com/postgres/postgres.git
synced 2025-07-21 16:02:15 +03:00
Fix assorted bugs in CREATE INDEX CONCURRENTLY.
This patch changes CREATE INDEX CONCURRENTLY so that the pg_index flag changes it makes without exclusive lock on the index are made via heap_inplace_update() rather than a normal transactional update. The latter is not very safe because moving the pg_index tuple could result in concurrent SnapshotNow scans finding it twice or not at all, thus possibly resulting in index corruption. In addition, fix various places in the code that ought to check to make sure that the indexes they are manipulating are valid and/or ready as appropriate. These represent bugs that have existed since 8.2, since a failed CREATE INDEX CONCURRENTLY could leave a corrupt or invalid index behind, and we ought not try to do anything that might fail with such an index. Also fix RelationReloadIndexInfo to ensure it copies all the pg_index columns that are allowed to change after initial creation. Previously we could have been left with stale values of some fields in an index relcache entry. It's not clear whether this actually had any user-visible consequences, but it's at least a bug waiting to happen. This is a subset of a patch already applied in 9.2 and HEAD. Back-patch into all earlier supported branches. Tom Lane and Andres Freund
This commit is contained in:
@ -147,9 +147,6 @@ DefineIndex(RangeVar *heapRelation,
|
||||
LockRelId heaprelid;
|
||||
LOCKTAG heaplocktag;
|
||||
Snapshot snapshot;
|
||||
Relation pg_index;
|
||||
HeapTuple indexTuple;
|
||||
Form_pg_index indexForm;
|
||||
int i;
|
||||
|
||||
/*
|
||||
@ -595,23 +592,7 @@ DefineIndex(RangeVar *heapRelation,
|
||||
* commit this transaction, any new transactions that open the table must
|
||||
* insert new entries into the index for insertions and non-HOT updates.
|
||||
*/
|
||||
pg_index = heap_open(IndexRelationId, RowExclusiveLock);
|
||||
|
||||
indexTuple = SearchSysCacheCopy1(INDEXRELID,
|
||||
ObjectIdGetDatum(indexRelationId));
|
||||
if (!HeapTupleIsValid(indexTuple))
|
||||
elog(ERROR, "cache lookup failed for index %u", indexRelationId);
|
||||
indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
|
||||
|
||||
Assert(!indexForm->indisready);
|
||||
Assert(!indexForm->indisvalid);
|
||||
|
||||
indexForm->indisready = true;
|
||||
|
||||
simple_heap_update(pg_index, &indexTuple->t_self, indexTuple);
|
||||
CatalogUpdateIndexes(pg_index, indexTuple);
|
||||
|
||||
heap_close(pg_index, RowExclusiveLock);
|
||||
index_set_state_flags(indexRelationId, INDEX_CREATE_SET_READY);
|
||||
|
||||
/* we can do away with our snapshot */
|
||||
PopActiveSnapshot();
|
||||
@ -735,23 +716,7 @@ DefineIndex(RangeVar *heapRelation,
|
||||
/*
|
||||
* Index can now be marked valid -- update its pg_index entry
|
||||
*/
|
||||
pg_index = heap_open(IndexRelationId, RowExclusiveLock);
|
||||
|
||||
indexTuple = SearchSysCacheCopy1(INDEXRELID,
|
||||
ObjectIdGetDatum(indexRelationId));
|
||||
if (!HeapTupleIsValid(indexTuple))
|
||||
elog(ERROR, "cache lookup failed for index %u", indexRelationId);
|
||||
indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
|
||||
|
||||
Assert(indexForm->indisready);
|
||||
Assert(!indexForm->indisvalid);
|
||||
|
||||
indexForm->indisvalid = true;
|
||||
|
||||
simple_heap_update(pg_index, &indexTuple->t_self, indexTuple);
|
||||
CatalogUpdateIndexes(pg_index, indexTuple);
|
||||
|
||||
heap_close(pg_index, RowExclusiveLock);
|
||||
index_set_state_flags(indexRelationId, INDEX_CREATE_SET_VALID);
|
||||
|
||||
/*
|
||||
* The pg_index update will cause backends (including this one) to update
|
||||
@ -759,7 +724,7 @@ DefineIndex(RangeVar *heapRelation,
|
||||
* relcache inval on the parent table to force replanning of cached plans.
|
||||
* Otherwise existing sessions might fail to use the new index where it
|
||||
* would be useful. (Note that our earlier commits did not create reasons
|
||||
* to replan; relcache flush on the index itself was sufficient.)
|
||||
* to replan; so relcache flush on the index itself was sufficient.)
|
||||
*/
|
||||
CacheInvalidateRelcacheByRelid(heaprelid.relId);
|
||||
|
||||
|
Reference in New Issue
Block a user