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

Commit 7fcddbd

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 5853b94 commit 7fcddbd

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,8 +4363,10 @@ RelationGetIndexPredicate(Relation relation)
43634363
* we can include system attributes (e.g., OID) in the bitmap representation.
43644364
*
43654365
* Caller had better hold at least RowExclusiveLock on the target relation
4366-
* to ensure that it has a stable set of indexes. This also makes it safe
4367-
* (deadlock-free) for us to take locks on the relation's indexes.
4366+
* to ensure it is safe (deadlock-free) for us to take locks on the relation's
4367+
* indexes. Note that since the introduction of CREATE INDEX CONCURRENTLY,
4368+
* that lock level doesn't guarantee a stable set of indexes, so we have to
4369+
* be prepared to retry here in case of a change in the set of indexes.
43684370
*
43694371
* The returned result is palloc'd in the caller's memory context and should
43704372
* be bms_free'd when not needed anymore.
@@ -4376,6 +4378,7 @@ RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
43764378
Bitmapset *uindexattrs; /* columns in unique indexes */
43774379
Bitmapset *idindexattrs; /* columns in the replica identity */
43784380
List *indexoidlist;
4381+
List *newindexoidlist;
43794382
Oid relreplindex;
43804383
ListCell *l;
43814384
MemoryContext oldcxt;
@@ -4401,8 +4404,9 @@ RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
44014404
return NULL;
44024405

44034406
/*
4404-
* Get cached list of index OIDs
4407+
* Get cached list of index OIDs. If we have to start over, we do so here.
44054408
*/
4409+
restart:
44064410
indexoidlist = RelationGetIndexList(relation);
44074411

44084412
/* Fall out if no indexes (but relhasindex was set) */
@@ -4413,8 +4417,7 @@ RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
44134417
* Copy the rd_replidindex value computed by RelationGetIndexList before
44144418
* proceeding. This is needed because a relcache flush could occur inside
44154419
* index_open below, resetting the fields managed by RelationGetIndexList.
4416-
* (The values we're computing will still be valid, assuming that caller
4417-
* has a sufficient lock on the relation.)
4420+
* We need to do the work with a stable value for relreplindex.
44184421
*/
44194422
relreplindex = relation->rd_replidindex;
44204423

@@ -4482,7 +4485,31 @@ RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
44824485
index_close(indexDesc, AccessShareLock);
44834486
}
44844487

4485-
list_free(indexoidlist);
4488+
/*
4489+
* During one of the index_opens in the above loop, we might have received
4490+
* a relcache flush event on this relcache entry, which might have been
4491+
* signaling a change in the rel's index list. If so, we'd better start
4492+
* over to ensure we deliver up-to-date attribute bitmaps.
4493+
*/
4494+
newindexoidlist = RelationGetIndexList(relation);
4495+
if (equal(indexoidlist, newindexoidlist) &&
4496+
relreplindex == relation->rd_replidindex)
4497+
{
4498+
/* Still the same index set, so proceed */
4499+
list_free(newindexoidlist);
4500+
list_free(indexoidlist);
4501+
}
4502+
else
4503+
{
4504+
/* Gotta do it over ... might as well not leak memory */
4505+
list_free(newindexoidlist);
4506+
list_free(indexoidlist);
4507+
bms_free(uindexattrs);
4508+
bms_free(idindexattrs);
4509+
bms_free(indexattrs);
4510+
4511+
goto restart;
4512+
}
44864513

44874514
/* Don't leak the old values of these bitmaps, if any */
44884515
bms_free(relation->rd_indexattr);

0 commit comments

Comments
 (0)