Skip to content

Commit 83158f7

Browse files
committed
Make index_set_state_flags() transactional
3c84046 is the original commit that introduced index_set_state_flags(), where the presence of SnapshotNow made necessary the use of an in-place update. SnapshotNow has been removed in 813fb03, so there is no actual reasons to not make this operation transactional. Note that while making the operation more robust, using a transactional operation in this routine was not strictly necessary as there was no use case for it yet. However, some future features are going to need a transactional behavior, like support for CREATE/DROP INDEX CONCURRENTLY with partitioned tables, where indexes in a partition tree need to have all their pg_index.indis* flags updated in the same transaction to make the operation stable to the end-user by keeping partition trees consistent, even with a failure mid-flight. REINDEX CONCURRENTLY uses already transactional updates when swapping the old and new indexes, making this change more consistent with the index-swapping logic. Author: Michael Paquier Reviewed-by: Anastasia Lubennikova Discussion: https://postgr.es/m/20200903080440.GA8559@paquier.xyz
1 parent 3e0242b commit 83158f7

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/backend/catalog/index.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,18 +3311,10 @@ validate_index_callback(ItemPointer itemptr, void *opaque)
33113311
* index_set_state_flags - adjust pg_index state flags
33123312
*
33133313
* This is used during CREATE/DROP INDEX CONCURRENTLY to adjust the pg_index
3314-
* flags that denote the index's state. Because the update is not
3315-
* transactional and will not roll back on error, this must only be used as
3316-
* the last step in a transaction that has not made any transactional catalog
3317-
* updates!
3314+
* flags that denote the index's state.
33183315
*
3319-
* Note that heap_inplace_update does send a cache inval message for the
3316+
* Note that CatalogTupleUpdate() sends a cache invalidation message for the
33203317
* tuple, so other sessions will hear about the update as soon as we commit.
3321-
*
3322-
* NB: In releases prior to PostgreSQL 9.4, the use of a non-transactional
3323-
* update here would have been unsafe; now that MVCC rules apply even for
3324-
* system catalog scans, we could potentially use a transactional update here
3325-
* instead.
33263318
*/
33273319
void
33283320
index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
@@ -3331,9 +3323,6 @@ index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
33313323
HeapTuple indexTuple;
33323324
Form_pg_index indexForm;
33333325

3334-
/* Assert that current xact hasn't done any transactional updates */
3335-
Assert(GetTopTransactionIdIfAny() == InvalidTransactionId);
3336-
33373326
/* Open pg_index and fetch a writable copy of the index's tuple */
33383327
pg_index = table_open(IndexRelationId, RowExclusiveLock);
33393328

@@ -3397,8 +3386,8 @@ index_set_state_flags(Oid indexId, IndexStateFlagsAction action)
33973386
break;
33983387
}
33993388

3400-
/* ... and write it back in-place */
3401-
heap_inplace_update(pg_index, indexTuple);
3389+
/* ... and update it */
3390+
CatalogTupleUpdate(pg_index, &indexTuple->t_self, indexTuple);
34023391

34033392
table_close(pg_index, RowExclusiveLock);
34043393
}

0 commit comments

Comments
 (0)