• Tom Lane's avatar
    Avoid returning stale attribute bitmaps in RelationGetIndexAttrBitmap(). · 2aaec654
    Tom Lane authored
    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
    2aaec654
relcache.c 188 KB