Skip to content

Commit bcd7b47

Browse files
committed
Avoid returning stale attribute bitmaps in RelationGetIndexAttrBitmap().
The problem with the original coding here is that we might receive (and clear) a relcache invalidation signal for the target relation down inside one of the index_open calls we're doing. Since the target is open, we would not drop the relcache entry, just reset its rd_indexvalid and rd_indexlist fields. But RelationGetIndexAttrBitmap() kept going, and would eventually cache and return potentially-obsolete attribute bitmaps. The case where this matters is where the inval signal was from a CREATE INDEX CONCURRENTLY telling us about a new index on a formerly-unindexed column. (In all other cases, the lock we hold on the target rel should prevent any concurrent change in index state.) Even just returning the stale attribute bitmap is not such a problem, because it shouldn't matter during the transaction in which we receive the signal. What hurts is caching the stale data, because it can survive into later transactions, breaking CREATE INDEX CONCURRENTLY's expectation that later transactions will not create new broken HOT chains. The upshot is that there's a window for building corrupted indexes during CREATE INDEX CONCURRENTLY. This patch fixes the problem by rechecking that the set of index OIDs is still the same at the end of RelationGetIndexAttrBitmap() as it was at the start. If not, we loop back and try again. That's a little more than is strictly necessary to fix the bug --- in principle, we could return the stale data but not cache it --- but it seems like a bad idea on general principles for relcache to return data it knows is stale. There might be more hazards of the same ilk, or there might be a better way to fix this one, but this patch definitely improves matters and seems unlikely to make anything worse. So let's push it into today's releases even as we continue to study the problem. Pavan Deolasee and myself Discussion: https://postgr.es/m/CABOikdM2MUq9cyZJi1KyLmmkCereyGp5JQ4fuwKoyKEde_mzkQ@mail.gmail.com
1 parent e39a63a commit bcd7b47

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,8 +3700,10 @@ RelationGetIndexPredicate(Relation relation)
37003700
* we can include system attributes (e.g., OID) in the bitmap representation.
37013701
*
37023702
* Caller had better hold at least RowExclusiveLock on the target relation
3703-
* to ensure that it has a stable set of indexes. This also makes it safe
3704-
* (deadlock-free) for us to take locks on the relation's indexes.
3703+
* to ensure it is safe (deadlock-free) for us to take locks on the relation's
3704+
* indexes. Note that since the introduction of CREATE INDEX CONCURRENTLY,
3705+
* that lock level doesn't guarantee a stable set of indexes, so we have to
3706+
* be prepared to retry here in case of a change in the set of indexes.
37053707
*
37063708
* The returned result is palloc'd in the caller's memory context and should
37073709
* be bms_free'd when not needed anymore.
@@ -3711,6 +3713,7 @@ RelationGetIndexAttrBitmap(Relation relation)
37113713
{
37123714
Bitmapset *indexattrs;
37133715
List *indexoidlist;
3716+
List *newindexoidlist;
37143717
ListCell *l;
37153718
MemoryContext oldcxt;
37163719

@@ -3723,8 +3726,9 @@ RelationGetIndexAttrBitmap(Relation relation)
37233726
return NULL;
37243727

37253728
/*
3726-
* Get cached list of index OIDs
3729+
* Get cached list of index OIDs. If we have to start over, we do so here.
37273730
*/
3731+
restart:
37283732
indexoidlist = RelationGetIndexList(relation);
37293733

37303734
/* Fall out if no indexes (but relhasindex was set) */
@@ -3773,7 +3777,28 @@ RelationGetIndexAttrBitmap(Relation relation)
37733777
index_close(indexDesc, AccessShareLock);
37743778
}
37753779

3776-
list_free(indexoidlist);
3780+
/*
3781+
* During one of the index_opens in the above loop, we might have received
3782+
* a relcache flush event on this relcache entry, which might have been
3783+
* signaling a change in the rel's index list. If so, we'd better start
3784+
* over to ensure we deliver up-to-date attribute bitmaps.
3785+
*/
3786+
newindexoidlist = RelationGetIndexList(relation);
3787+
if (equal(indexoidlist, newindexoidlist))
3788+
{
3789+
/* Still the same index set, so proceed */
3790+
list_free(newindexoidlist);
3791+
list_free(indexoidlist);
3792+
}
3793+
else
3794+
{
3795+
/* Gotta do it over ... might as well not leak memory */
3796+
list_free(newindexoidlist);
3797+
list_free(indexoidlist);
3798+
bms_free(indexattrs);
3799+
3800+
goto restart;
3801+
}
37773802

37783803
/* Now save a copy of the bitmap in the relcache entry. */
37793804
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);

0 commit comments

Comments
 (0)