Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdccfb72

Browse files
committed
Reset reindex-in-progress state before reverifying an exclusion constraint.
This avoids an Assert failure when we try to use ordinary index fetcheswhile checking for exclusion conflicts. Per report from Noah Misch.No need for back-patch because the Assert wasn't there before 9.1.
1 parentccd69b8 commitdccfb72

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

‎src/backend/catalog/index.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static void validate_index_heapscan(Relation heapRelation,
115115
Snapshotsnapshot,
116116
v_i_state*state);
117117
staticOidIndexGetRelation(OidindexId);
118+
staticboolReindexIsCurrentlyProcessingIndex(OidindexOid);
118119
staticvoidSetReindexProcessing(OidheapOid,OidindexOid);
119120
staticvoidResetReindexProcessing(void);
120121
staticvoidSetReindexPending(List*indexes);
@@ -1747,8 +1748,8 @@ index_build(Relation heapRelation,
17471748
* created it, or truncated twice in a subsequent transaction, the
17481749
* relfilenode won't change, and nothing needs to be done here.
17491750
*/
1750-
if (heapRelation->rd_rel->relpersistence==RELPERSISTENCE_UNLOGGED
1751-
&&!smgrexists(indexRelation->rd_smgr,INIT_FORKNUM))
1751+
if (heapRelation->rd_rel->relpersistence==RELPERSISTENCE_UNLOGGED&&
1752+
!smgrexists(indexRelation->rd_smgr,INIT_FORKNUM))
17521753
{
17531754
RegProcedureambuildempty=indexRelation->rd_am->ambuildempty;
17541755

@@ -1757,19 +1758,6 @@ index_build(Relation heapRelation,
17571758
OidFunctionCall1(ambuildempty,PointerGetDatum(indexRelation));
17581759
}
17591760

1760-
/*
1761-
* If it's for an exclusion constraint, make a second pass over the heap
1762-
* to verify that the constraint is satisfied.
1763-
*/
1764-
if (indexInfo->ii_ExclusionOps!=NULL)
1765-
IndexCheckExclusion(heapRelation,indexRelation,indexInfo);
1766-
1767-
/* Roll back any GUC changes executed by index functions */
1768-
AtEOXact_GUC(false,save_nestlevel);
1769-
1770-
/* Restore userid and security context */
1771-
SetUserIdAndSecContext(save_userid,save_sec_context);
1772-
17731761
/*
17741762
* If we found any potentially broken HOT chains, mark the index as not
17751763
* being usable until the current transaction is below the event horizon.
@@ -1824,8 +1812,23 @@ index_build(Relation heapRelation,
18241812
InvalidOid,
18251813
stats->index_tuples);
18261814

1827-
/* Make the updated versions visible */
1815+
/* Make the updatedcatalog rowversions visible */
18281816
CommandCounterIncrement();
1817+
1818+
/*
1819+
* If it's for an exclusion constraint, make a second pass over the heap
1820+
* to verify that the constraint is satisfied. We must not do this until
1821+
* the index is fully valid. (Broken HOT chains shouldn't matter, though;
1822+
* see comments for IndexCheckExclusion.)
1823+
*/
1824+
if (indexInfo->ii_ExclusionOps!=NULL)
1825+
IndexCheckExclusion(heapRelation,indexRelation,indexInfo);
1826+
1827+
/* Roll back any GUC changes executed by index functions */
1828+
AtEOXact_GUC(false,save_nestlevel);
1829+
1830+
/* Restore userid and security context */
1831+
SetUserIdAndSecContext(save_userid,save_sec_context);
18291832
}
18301833

18311834

@@ -2269,6 +2272,15 @@ IndexCheckExclusion(Relation heapRelation,
22692272
EState*estate;
22702273
ExprContext*econtext;
22712274

2275+
/*
2276+
* If we are reindexing the target index, mark it as no longer being
2277+
* reindexed, to forestall an Assert in index_beginscan when we try to
2278+
* use the index for probes. This is OK because the index is now
2279+
* fully valid.
2280+
*/
2281+
if (ReindexIsCurrentlyProcessingIndex(RelationGetRelid(indexRelation)))
2282+
ResetReindexProcessing();
2283+
22722284
/*
22732285
* Need an EState for evaluation of index expressions and partial-index
22742286
* predicates.Also a slot to hold the current tuple.
@@ -2989,8 +3001,8 @@ reindex_relation(Oid relid, int flags)
29893001

29903002
CommandCounterIncrement();
29913003

2992-
if (flags&REINDEX_REL_SUPPRESS_INDEX_USE)
2993-
RemoveReindexPending(indexOid);
3004+
/* Index should no longer be in the pending list */
3005+
Assert(!ReindexIsProcessingIndex(indexOid));
29943006

29953007
if (is_pg_class)
29963008
doneIndexes=lappend_oid(doneIndexes,indexOid);
@@ -3030,7 +3042,9 @@ reindex_relation(Oid relid, int flags)
30303042
*System index reindexing support
30313043
*
30323044
* When we are busy reindexing a system index, this code provides support
3033-
* for preventing catalog lookups from using that index.
3045+
* for preventing catalog lookups from using that index. We also make use
3046+
* of this to catch attempted uses of user indexes during reindexing of
3047+
* those indexes.
30343048
* ----------------------------------------------------------------
30353049
*/
30363050

@@ -3048,6 +3062,16 @@ ReindexIsProcessingHeap(Oid heapOid)
30483062
returnheapOid==currentlyReindexedHeap;
30493063
}
30503064

3065+
/*
3066+
* ReindexIsCurrentlyProcessingIndex
3067+
*True if index specified by OID is currently being reindexed.
3068+
*/
3069+
staticbool
3070+
ReindexIsCurrentlyProcessingIndex(OidindexOid)
3071+
{
3072+
returnindexOid==currentlyReindexedIndex;
3073+
}
3074+
30513075
/*
30523076
* ReindexIsProcessingIndex
30533077
*True if index specified by OID is currently being reindexed,
@@ -3075,6 +3099,8 @@ SetReindexProcessing(Oid heapOid, Oid indexOid)
30753099
elog(ERROR,"cannot reindex while reindexing");
30763100
currentlyReindexedHeap=heapOid;
30773101
currentlyReindexedIndex=indexOid;
3102+
/* Index is no longer "pending" reindex. */
3103+
RemoveReindexPending(indexOid);
30783104
}
30793105

30803106
/*

‎src/test/regress/input/constraints.source

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ INSERT INTO circles VALUES('<(20,20), 10>', '<(10,10), 5>');
397397
ALTER TABLE circles ADD EXCLUDE USING gist
398398
(c1 WITH &&, (c2::circle) WITH &&);
399399

400+
-- try reindexing an existing constraint
401+
REINDEX INDEX circles_c1_c2_excl;
402+
400403
DROP TABLE circles;
401404

402405
-- Check deferred exclusion constraint

‎src/test/regress/output/constraints.source

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ ALTER TABLE circles ADD EXCLUDE USING gist
543543
NOTICE: ALTER TABLE / ADD EXCLUDE will create implicit index "circles_c1_c2_excl1" for table "circles"
544544
ERROR: could not create exclusion constraint "circles_c1_c2_excl1"
545545
DETAIL: Key (c1, (c2::circle))=(<(0,0),5>, <(0,0),5>) conflicts with key (c1, (c2::circle))=(<(0,0),5>, <(0,0),4>).
546+
-- try reindexing an existing constraint
547+
REINDEX INDEX circles_c1_c2_excl;
546548
DROP TABLE circles;
547549
-- Check deferred exclusion constraint
548550
CREATE TABLE deferred_excl (

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp