Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 16c4fa4

Browse files
michail-nikolaevCommitfest Bot
authored and
Commitfest Bot
committed
Remove PROC_IN_SAFE_IC optimization
This optimization allowed concurrent index builds to ignore other indexes without expressions or predicates. With the new snapshot handling approach that periodically refreshes snapshots, this optimization is no longer necessary. The change simplifies concurrent index build code by: - removing the PROC_IN_SAFE_IC process status flag - eliminating set_indexsafe_procflags() calls and related logic - removing special case handling in GetCurrentVirtualXIDs() - removing related test cases and injection points
1 parent ab06d3c commit 16c4fa4

File tree

9 files changed

+13
-237
lines changed

9 files changed

+13
-237
lines changed

src/backend/access/brin/brin.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,11 +2893,9 @@ _brin_parallel_build_main(dsm_segment *seg, shm_toc *toc)
28932893
int sortmem;
28942894

28952895
/*
2896-
* The only possible status flag that can be set to the parallel worker is
2897-
* PROC_IN_SAFE_IC.
2896+
* There are no possible status flag that can be set to the parallel worker.
28982897
*/
2899-
Assert((MyProc->statusFlags == 0) ||
2900-
(MyProc->statusFlags == PROC_IN_SAFE_IC));
2898+
Assert(MyProc->statusFlags == 0);
29012899

29022900
/* Set debug_query_string for individual workers first */
29032901
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);

src/backend/access/gin/gininsert.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,11 +2106,9 @@ _gin_parallel_build_main(dsm_segment *seg, shm_toc *toc)
21062106
int sortmem;
21072107

21082108
/*
2109-
* The only possible status flag that can be set to the parallel worker is
2110-
* PROC_IN_SAFE_IC.
2109+
* There are no possible status flag that can be set to the parallel worker.
21112110
*/
2112-
Assert((MyProc->statusFlags == 0) ||
2113-
(MyProc->statusFlags == PROC_IN_SAFE_IC));
2111+
Assert(MyProc->statusFlags == 0);
21142112

21152113
/* Set debug_query_string for individual workers first */
21162114
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);

src/backend/access/nbtree/nbtsort.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,11 +1910,9 @@ _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
19101910
#endif /* BTREE_BUILD_STATS */
19111911

19121912
/*
1913-
* The only possible status flag that can be set to the parallel worker is
1914-
* PROC_IN_SAFE_IC.
1913+
* There are no possible status flag that can be set to the parallel worker.
19151914
*/
1916-
Assert((MyProc->statusFlags == 0) ||
1917-
(MyProc->statusFlags == PROC_IN_SAFE_IC));
1915+
Assert(MyProc->statusFlags == 0);
19181916

19191917
/* Set debug_query_string for individual workers first */
19201918
sharedquery = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, true);

src/backend/commands/indexcmds.c

Lines changed: 4 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ static bool ReindexRelationConcurrently(const ReindexStmt *stmt,
115115
Oid relationOid,
116116
const ReindexParams *params);
117117
static void update_relispartition(Oid relationId, bool newval);
118-
static inline void set_indexsafe_procflags(void);
119118

120119
/*
121120
* callback argument type for RangeVarCallbackForReindexIndex()
@@ -418,10 +417,7 @@ CompareOpclassOptions(const Datum *opts1, const Datum *opts2, int natts)
418417
* lazy VACUUMs, because they won't be fazed by missing index entries
419418
* either. (Manual ANALYZEs, however, can't be excluded because they
420419
* might be within transactions that are going to do arbitrary operations
421-
* later.) Processes running CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY
422-
* on indexes that are neither expressional nor partial are also safe to
423-
* ignore, since we know that those processes won't examine any data
424-
* outside the table they're indexing.
420+
* later.)
425421
*
426422
* Also, GetCurrentVirtualXIDs never reports our own vxid, so we need not
427423
* check for that.
@@ -442,8 +438,7 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
442438
VirtualTransactionId *old_snapshots;
443439

444440
old_snapshots = GetCurrentVirtualXIDs(limitXmin, true, false,
445-
PROC_IS_AUTOVACUUM | PROC_IN_VACUUM
446-
| PROC_IN_SAFE_IC,
441+
PROC_IS_AUTOVACUUM | PROC_IN_VACUUM,
447442
&n_old_snapshots);
448443
if (progress)
449444
pgstat_progress_update_param(PROGRESS_WAITFOR_TOTAL, n_old_snapshots);
@@ -463,8 +458,7 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
463458

464459
newer_snapshots = GetCurrentVirtualXIDs(limitXmin,
465460
true, false,
466-
PROC_IS_AUTOVACUUM | PROC_IN_VACUUM
467-
| PROC_IN_SAFE_IC,
461+
PROC_IS_AUTOVACUUM | PROC_IN_VACUUM,
468462
&n_newer_snapshots);
469463
for (j = i; j < n_old_snapshots; j++)
470464
{
@@ -578,7 +572,6 @@ DefineIndex(Oid tableId,
578572
amoptions_function amoptions;
579573
bool exclusion;
580574
bool partitioned;
581-
bool safe_index;
582575
Datum reloptions;
583576
int16 *coloptions;
584577
IndexInfo *indexInfo;
@@ -1181,10 +1174,6 @@ DefineIndex(Oid tableId,
11811174
}
11821175
}
11831176

1184-
/* Is index safe for others to ignore? See set_indexsafe_procflags() */
1185-
safe_index = indexInfo->ii_Expressions == NIL &&
1186-
indexInfo->ii_Predicate == NIL;
1187-
11881177
/*
11891178
* Report index creation if appropriate (delay this till after most of the
11901179
* error checks)
@@ -1671,10 +1660,6 @@ DefineIndex(Oid tableId,
16711660
CommitTransactionCommand();
16721661
StartTransactionCommand();
16731662

1674-
/* Tell concurrent index builds to ignore us, if index qualifies */
1675-
if (safe_index)
1676-
set_indexsafe_procflags();
1677-
16781663
/*
16791664
* The index is now visible, so we can report the OID. While on it,
16801665
* include the report for the beginning of phase 2.
@@ -1729,9 +1714,6 @@ DefineIndex(Oid tableId,
17291714
CommitTransactionCommand();
17301715
StartTransactionCommand();
17311716

1732-
/* Tell concurrent index builds to ignore us, if index qualifies */
1733-
if (safe_index)
1734-
set_indexsafe_procflags();
17351717
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
17361718
PROGRESS_CREATEIDX_PHASE_WAIT_2);
17371719
/*
@@ -1761,10 +1743,6 @@ DefineIndex(Oid tableId,
17611743
CommitTransactionCommand();
17621744
StartTransactionCommand();
17631745

1764-
/* Tell concurrent index builds to ignore us, if index qualifies */
1765-
if (safe_index)
1766-
set_indexsafe_procflags();
1767-
17681746
/*
17691747
* Phase 3 of concurrent index build
17701748
*
@@ -1790,9 +1768,7 @@ DefineIndex(Oid tableId,
17901768

17911769
CommitTransactionCommand();
17921770
StartTransactionCommand();
1793-
/* Tell concurrent index builds to ignore us, if index qualifies */
1794-
if (safe_index)
1795-
set_indexsafe_procflags();
1771+
17961772
/*
17971773
* Merge content of auxiliary and target indexes - insert any missing index entries.
17981774
*/
@@ -1809,9 +1785,6 @@ DefineIndex(Oid tableId,
18091785
CommitTransactionCommand();
18101786
StartTransactionCommand();
18111787

1812-
/* Tell concurrent index builds to ignore us, if index qualifies */
1813-
if (safe_index)
1814-
set_indexsafe_procflags();
18151788

18161789
/* We should now definitely not be advertising any xmin. */
18171790
Assert(MyProc->xmin == InvalidTransactionId);
@@ -1852,10 +1825,6 @@ DefineIndex(Oid tableId,
18521825
CommitTransactionCommand();
18531826
StartTransactionCommand();
18541827

1855-
/* Tell concurrent index builds to ignore us, if index qualifies */
1856-
if (safe_index)
1857-
set_indexsafe_procflags();
1858-
18591828
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
18601829
PROGRESS_CREATEIDX_PHASE_WAIT_5);
18611830
/* Now wait for all transaction to see auxiliary as "non-ready for inserts" */
@@ -1876,10 +1845,6 @@ DefineIndex(Oid tableId,
18761845
CommitTransactionCommand();
18771846
StartTransactionCommand();
18781847

1879-
/* Tell concurrent index builds to ignore us, if index qualifies */
1880-
if (safe_index)
1881-
set_indexsafe_procflags();
1882-
18831848
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
18841849
PROGRESS_CREATEIDX_PHASE_WAIT_6);
18851850
/* Now wait for all transaction to ignore auxiliary because it is dead */
@@ -3620,7 +3585,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
36203585
Oid junkAuxIndexId;
36213586
Oid tableId;
36223587
Oid amId;
3623-
bool safe; /* for set_indexsafe_procflags */
36243588
} ReindexIndexInfo;
36253589
List *heapRelationIds = NIL;
36263590
List *indexIds = NIL;
@@ -3994,17 +3958,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
39943958
save_nestlevel = NewGUCNestLevel();
39953959
RestrictSearchPath();
39963960

3997-
/* determine safety of this index for set_indexsafe_procflags */
3998-
idx->safe = (RelationGetIndexExpressions(indexRel) == NIL &&
3999-
RelationGetIndexPredicate(indexRel) == NIL);
4000-
4001-
#ifdef USE_INJECTION_POINTS
4002-
if (idx->safe)
4003-
INJECTION_POINT("reindex-conc-index-safe", NULL);
4004-
else
4005-
INJECTION_POINT("reindex-conc-index-not-safe", NULL);
4006-
#endif
4007-
40083961
idx->tableId = RelationGetRelid(heapRel);
40093962
idx->amId = indexRel->rd_rel->relam;
40103963

@@ -4070,7 +4023,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
40704023
newidx->indexId = newIndexId;
40714024
newidx->auxIndexId = auxIndexId;
40724025
newidx->junkAuxIndexId = junkAuxIndexId;
4073-
newidx->safe = idx->safe;
40744026
newidx->tableId = idx->tableId;
40754027
newidx->amId = idx->amId;
40764028

@@ -4171,11 +4123,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
41714123
CommitTransactionCommand();
41724124
StartTransactionCommand();
41734125

4174-
/*
4175-
* Because we don't take a snapshot in this transaction, there's no need
4176-
* to set the PROC_IN_SAFE_IC flag here.
4177-
*/
4178-
41794126
/*
41804127
* Phase 2 of REINDEX CONCURRENTLY
41814128
*
@@ -4207,10 +4154,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
42074154
*/
42084155
CHECK_FOR_INTERRUPTS();
42094156

4210-
/* Tell concurrent indexing to ignore us, if index qualifies */
4211-
if (newidx->safe)
4212-
set_indexsafe_procflags();
4213-
42144157
/* Build auxiliary index, it is fast - without any actual heap scan, just an empty index. */
42154158
index_concurrently_build(newidx->tableId, newidx->auxIndexId);
42164159

@@ -4219,11 +4162,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
42194162

42204163
StartTransactionCommand();
42214164

4222-
/*
4223-
* Because we don't take a snapshot in this transaction, there's no need
4224-
* to set the PROC_IN_SAFE_IC flag here.
4225-
*/
4226-
42274165
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
42284166
PROGRESS_CREATEIDX_PHASE_WAIT_2);
42294167
/*
@@ -4248,10 +4186,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
42484186
*/
42494187
CHECK_FOR_INTERRUPTS();
42504188

4251-
/* Tell concurrent indexing to ignore us, if index qualifies */
4252-
if (newidx->safe)
4253-
set_indexsafe_procflags();
4254-
42554189
/*
42564190
* Update progress for the index to build, with the correct parent
42574191
* table involved.
@@ -4271,11 +4205,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
42714205

42724206
StartTransactionCommand();
42734207

4274-
/*
4275-
* Because we don't take a snapshot or Xid in this transaction, there's no
4276-
* need to set the PROC_IN_SAFE_IC flag here.
4277-
*/
4278-
42794208
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
42804209
PROGRESS_CREATEIDX_PHASE_WAIT_3);
42814210
WaitForLockersMultiple(lockTags, ShareLock, true);
@@ -4297,10 +4226,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
42974226
*/
42984227
CHECK_FOR_INTERRUPTS();
42994228

4300-
/* Tell concurrent indexing to ignore us, if index qualifies */
4301-
if (newidx->safe)
4302-
set_indexsafe_procflags();
4303-
43044229
/*
43054230
* Updating pg_index might involve TOAST table access, so ensure we
43064231
* have a valid snapshot.
@@ -4336,10 +4261,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
43364261
*/
43374262
CHECK_FOR_INTERRUPTS();
43384263

4339-
/* Tell concurrent indexing to ignore us, if index qualifies */
4340-
if (newidx->safe)
4341-
set_indexsafe_procflags();
4342-
43434264
/*
43444265
* Update progress for the index to build, with the correct parent
43454266
* table involved.
@@ -4367,9 +4288,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
43674288
* interesting tuples. But since it might not contain tuples deleted
43684289
* just before the latest snap was taken, we have to wait out any
43694290
* transactions that might have older snapshots.
4370-
*
4371-
* Because we don't take a snapshot or Xid in this transaction,
4372-
* there's no need to set the PROC_IN_SAFE_IC flag here.
43734291
*/
43744292
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
43754293
PROGRESS_CREATEIDX_PHASE_WAIT_4);
@@ -4391,13 +4309,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
43914309
INJECTION_POINT("reindex_relation_concurrently_before_swap", NULL);
43924310
StartTransactionCommand();
43934311

4394-
/*
4395-
* Because this transaction only does catalog manipulations and doesn't do
4396-
* any index operations, we can set the PROC_IN_SAFE_IC flag here
4397-
* unconditionally.
4398-
*/
4399-
set_indexsafe_procflags();
4400-
44014312
forboth(lc, indexIds, lc2, newIndexIds)
44024313
{
44034314
ReindexIndexInfo *oldidx = lfirst(lc);
@@ -4453,12 +4364,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
44534364
CommitTransactionCommand();
44544365
StartTransactionCommand();
44554366

4456-
/*
4457-
* While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4458-
* real need for that, because we only acquire an Xid after the wait is
4459-
* done, and that lasts for a very short period.
4460-
*/
4461-
44624367
/*
44634368
* Phase 5 of REINDEX CONCURRENTLY
44644369
*
@@ -4522,12 +4427,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
45224427
CommitTransactionCommand();
45234428
StartTransactionCommand();
45244429

4525-
/*
4526-
* While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
4527-
* real need for that, because we only acquire an Xid after the wait is
4528-
* done, and that lasts for a very short period.
4529-
*/
4530-
45314430
/*
45324431
* Phase 6 of REINDEX CONCURRENTLY
45334432
*
@@ -4795,36 +4694,3 @@ update_relispartition(Oid relationId, bool newval)
47954694
table_close(classRel, RowExclusiveLock);
47964695
}
47974696

4798-
/*
4799-
* Set the PROC_IN_SAFE_IC flag in MyProc->statusFlags.
4800-
*
4801-
* When doing concurrent index builds, we can set this flag
4802-
* to tell other processes concurrently running CREATE
4803-
* INDEX CONCURRENTLY or REINDEX CONCURRENTLY to ignore us when
4804-
* doing their waits for concurrent snapshots. On one hand it
4805-
* avoids pointlessly waiting for a process that's not interesting
4806-
* anyway; but more importantly it avoids deadlocks in some cases.
4807-
*
4808-
* This can be done safely only for indexes that don't execute any
4809-
* expressions that could access other tables, so index must not be
4810-
* expressional nor partial. Caller is responsible for only calling
4811-
* this routine when that assumption holds true.
4812-
*
4813-
* (The flag is reset automatically at transaction end, so it must be
4814-
* set for each transaction.)
4815-
*/
4816-
static inline void
4817-
set_indexsafe_procflags(void)
4818-
{
4819-
/*
4820-
* This should only be called before installing xid or xmin in MyProc;
4821-
* otherwise, concurrent processes could see an Xmin that moves backwards.
4822-
*/
4823-
Assert(MyProc->xid == InvalidTransactionId &&
4824-
MyProc->xmin == InvalidTransactionId);
4825-
4826-
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4827-
MyProc->statusFlags |= PROC_IN_SAFE_IC;
4828-
ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
4829-
LWLockRelease(ProcArrayLock);
4830-
}

0 commit comments

Comments
 (0)