Skip to content

Commit 32c893c

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 fc7c21f commit 32c893c

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,8 +3811,10 @@ RelationGetIndexPredicate(Relation relation)
38113811
* we can include system attributes (e.g., OID) in the bitmap representation.
38123812
*
38133813
* Caller had better hold at least RowExclusiveLock on the target relation
3814-
* to ensure that it has a stable set of indexes. This also makes it safe
3815-
* (deadlock-free) for us to take locks on the relation's indexes.
3814+
* to ensure it is safe (deadlock-free) for us to take locks on the relation's
3815+
* indexes. Note that since the introduction of CREATE INDEX CONCURRENTLY,
3816+
* that lock level doesn't guarantee a stable set of indexes, so we have to
3817+
* be prepared to retry here in case of a change in the set of indexes.
38163818
*
38173819
* The returned result is palloc'd in the caller's memory context and should
38183820
* be bms_free'd when not needed anymore.
@@ -3823,6 +3825,7 @@ RelationGetIndexAttrBitmap(Relation relation, bool keyAttrs)
38233825
Bitmapset *indexattrs;
38243826
Bitmapset *uindexattrs;
38253827
List *indexoidlist;
3828+
List *newindexoidlist;
38263829
ListCell *l;
38273830
MemoryContext oldcxt;
38283831

@@ -3835,8 +3838,9 @@ RelationGetIndexAttrBitmap(Relation relation, bool keyAttrs)
38353838
return NULL;
38363839

38373840
/*
3838-
* Get cached list of index OIDs
3841+
* Get cached list of index OIDs. If we have to start over, we do so here.
38393842
*/
3843+
restart:
38403844
indexoidlist = RelationGetIndexList(relation);
38413845

38423846
/* Fall out if no indexes (but relhasindex was set) */
@@ -3897,7 +3901,29 @@ RelationGetIndexAttrBitmap(Relation relation, bool keyAttrs)
38973901
index_close(indexDesc, AccessShareLock);
38983902
}
38993903

3900-
list_free(indexoidlist);
3904+
/*
3905+
* During one of the index_opens in the above loop, we might have received
3906+
* a relcache flush event on this relcache entry, which might have been
3907+
* signaling a change in the rel's index list. If so, we'd better start
3908+
* over to ensure we deliver up-to-date attribute bitmaps.
3909+
*/
3910+
newindexoidlist = RelationGetIndexList(relation);
3911+
if (equal(indexoidlist, newindexoidlist))
3912+
{
3913+
/* Still the same index set, so proceed */
3914+
list_free(newindexoidlist);
3915+
list_free(indexoidlist);
3916+
}
3917+
else
3918+
{
3919+
/* Gotta do it over ... might as well not leak memory */
3920+
list_free(newindexoidlist);
3921+
list_free(indexoidlist);
3922+
bms_free(uindexattrs);
3923+
bms_free(indexattrs);
3924+
3925+
goto restart;
3926+
}
39013927

39023928
/*
39033929
* Now save copies of the bitmaps in the relcache entry. We intentionally

0 commit comments

Comments
 (0)