Assert lack of hazardous buffer locks before possible catalog read. REL_17_STABLE github/REL_17_STABLE
authorNoah Misch <[email protected]>
Wed, 17 Dec 2025 00:13:54 +0000 (16:13 -0800)
committerNoah Misch <[email protected]>
Wed, 17 Dec 2025 00:13:54 +0000 (16:13 -0800)
Commit 0bada39c83a150079567a6e97b1a25a198f30ea3 fixed a bug of this kind,
which existed in all branches for six days before detection.  While the
probability of reaching the trouble was low, the disruption was extreme.  No
new backends could start, and service restoration needed an immediate
shutdown.  Hence, add this to catch the next bug like it.

The new check in RelationIdGetRelation() suffices to make autovacuum detect
the bug in commit 243e9b40f1b2dd09d6e5bf91ebf6e822a2cd3704 that led to commit
0bada39.  This also checks in a number of similar places.  It replaces each
Assert(IsTransactionState()) that pertained to a conditional catalog read.

Back-patch to v14 - v17.  This a back-patch of commit
f4ece891fc2f3f96f0571720a1ae30db8030681b (from before v18 branched) to
all supported branches, to accompany the back-patch of commits 243e9b4
and 0bada39.  For catalog indexes, the bttextcmp() behavior that
motivated IsCatalogTextUniqueIndexOid() was v18-specific.  Hence, this
back-patch doesn't need that or its correction from commit
4a4ee0c2c1e53401924101945ac3d517c0a8a559.

Reported-by: Alexander Lakhin <[email protected]>
Discussion: https://postgr.es/m/20250410191830[email protected]
Discussion: https://postgr.es/m/10ec0bc3-5933-1189-6bb8-5dec4114558e@gmail.com
Backpatch-through: 14-17

src/backend/storage/buffer/bufmgr.c
src/backend/storage/lmgr/lwlock.c
src/backend/utils/adt/pg_locale.c
src/backend/utils/cache/catcache.c
src/backend/utils/cache/inval.c
src/backend/utils/cache/relcache.c
src/backend/utils/mb/mbutils.c
src/include/storage/bufmgr.h
src/include/storage/lwlock.h
src/include/utils/relcache.h

index f8d30bf71e1a87c21e3a9777fc2f6df653cc8eaa..7daf1bed0418eca2574ec0d760d441ae686b0472 100644 (file)
@@ -40,6 +40,9 @@
 #include "access/tableam.h"
 #include "access/xloginsert.h"
 #include "access/xlogutils.h"
+#ifdef USE_ASSERT_CHECKING
+#include "catalog/pg_tablespace_d.h"
+#endif
 #include "catalog/storage.h"
 #include "catalog/storage_xlog.h"
 #include "executor/instrument.h"
@@ -535,6 +538,10 @@ static void RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
                                           ForkNumber forkNum, bool permanent);
 static void AtProcExit_Buffers(int code, Datum arg);
 static void CheckForBufferLeaks(void);
+#ifdef USE_ASSERT_CHECKING
+static void AssertNotCatalogBufferLock(LWLock *lock, LWLockMode mode,
+                                      void *unused_context);
+#endif
 static int rlocator_comparator(const void *p1, const void *p2);
 static inline int buffertag_comparator(const BufferTag *ba, const BufferTag *bb);
 static inline int ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b);
@@ -3647,6 +3654,66 @@ CheckForBufferLeaks(void)
 #endif
 }
 
+#ifdef USE_ASSERT_CHECKING
+/*
+ * Check for exclusive-locked catalog buffers.  This is the core of
+ * AssertCouldGetRelation().
+ *
+ * A backend would self-deadlock on LWLocks if the catalog scan read the
+ * exclusive-locked buffer.  The main threat is exclusive-locked buffers of
+ * catalogs used in relcache, because a catcache search on any catalog may
+ * build that catalog's relcache entry.  We don't have an inventory of
+ * catalogs relcache uses, so just check buffers of most catalogs.
+ *
+ * It's better to minimize waits while holding an exclusive buffer lock, so it
+ * would be nice to broaden this check not to be catalog-specific.  However,
+ * bttextcmp() accesses pg_collation, and non-core opclasses might similarly
+ * read tables.  That is deadlock-free as long as there's no loop in the
+ * dependency graph: modifying table A may cause an opclass to read table B,
+ * but it must not cause a read of table A.
+ */
+void
+AssertBufferLocksPermitCatalogRead(void)
+{
+   ForEachLWLockHeldByMe(AssertNotCatalogBufferLock, NULL);
+}
+
+static void
+AssertNotCatalogBufferLock(LWLock *lock, LWLockMode mode,
+                          void *unused_context)
+{
+   BufferDesc *bufHdr;
+   BufferTag   tag;
+   Oid         relid;
+
+   if (mode != LW_EXCLUSIVE)
+       return;
+
+   if (!((BufferDescPadded *) lock > BufferDescriptors &&
+         (BufferDescPadded *) lock < BufferDescriptors + NBuffers))
+       return;                 /* not a buffer lock */
+
+   bufHdr = (BufferDesc *)
+       ((char *) lock - offsetof(BufferDesc, content_lock));
+   tag = bufHdr->tag;
+
+   /*
+    * This relNumber==relid assumption holds until a catalog experiences
+    * VACUUM FULL or similar.  After a command like that, relNumber will be
+    * in the normal (non-catalog) range, and we lose the ability to detect
+    * hazardous access to that catalog.  Calling RelidByRelfilenumber() would
+    * close that gap, but RelidByRelfilenumber() might then deadlock with a
+    * held lock.
+    */
+   relid = tag.relNumber;
+
+   Assert(!IsCatalogRelationOid(relid));
+   /* Shared rels are always catalogs: detect even after VACUUM FULL. */
+   Assert(tag.spcOid != GLOBALTABLESPACE_OID);
+}
+#endif
+
+
 /*
  * Helper routine to issue warnings when a buffer is unexpectedly pinned
  */
index e4865bfd44d1d8a08f5c3faa96bf7c7412d451f1..05a09ad4b4ae63b2f3ce7607bbba52b63cc6e82a 100644 (file)
@@ -1886,6 +1886,21 @@ LWLockReleaseAll(void)
 }
 
 
+/*
+ * ForEachLWLockHeldByMe - run a callback for each held lock
+ *
+ * This is meant as debug support only.
+ */
+void
+ForEachLWLockHeldByMe(void (*callback) (LWLock *, LWLockMode, void *),
+                     void *context)
+{
+   int         i;
+
+   for (i = 0; i < num_held_lwlocks; i++)
+       callback(held_lwlocks[i].lock, held_lwlocks[i].mode, context);
+}
+
 /*
  * LWLockHeldByMe - test whether my process holds a lock in any mode
  *
index 4c85a01b28479f8e7714c72ff0667f946fb425a2..d2b16d81fe3bac99afc57d33ee12c1315dc493c0 100644 (file)
@@ -66,6 +66,7 @@
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/pg_locale.h"
+#include "utils/relcache.h"
 #include "utils/syscache.h"
 
 #ifdef USE_ICU
@@ -1258,6 +1259,8 @@ lookup_collation_cache(Oid collation, bool set_flags)
    Assert(OidIsValid(collation));
    Assert(collation != DEFAULT_COLLATION_OID);
 
+   AssertCouldGetRelation();
+
    if (collation_cache == NULL)
    {
        /* First time through, initialize the hash table */
index 5169ca72bc087c0214f92598eec443b2a4430f32..7a67b46a6b8d83fa25847a51f418b48df869a611 100644 (file)
@@ -1054,12 +1054,41 @@ RehashCatCacheLists(CatCache *cp)
    cp->cc_lbucket = newbucket;
 }
 
+/*
+ *     ConditionalCatalogCacheInitializeCache
+ *
+ * Call CatalogCacheInitializeCache() if not yet done.
+ */
+pg_attribute_always_inline
+static void
+ConditionalCatalogCacheInitializeCache(CatCache *cache)
+{
+#ifdef USE_ASSERT_CHECKING
+   /*
+    * TypeCacheRelCallback() runs outside transactions and relies on TYPEOID
+    * for hashing.  This isn't ideal.  Since lookup_type_cache() both
+    * registers the callback and searches TYPEOID, reaching trouble likely
+    * requires OOM at an unlucky moment.
+    *
+    * InvalidateAttoptCacheCallback() runs outside transactions and likewise
+    * relies on ATTNUM.  InitPostgres() initializes ATTNUM, so it's reliable.
+    */
+   if (!(cache->id == TYPEOID || cache->id == ATTNUM) ||
+       IsTransactionState())
+       AssertCouldGetRelation();
+   else
+       Assert(cache->cc_tupdesc != NULL);
+#endif
+
+   if (unlikely(cache->cc_tupdesc == NULL))
+       CatalogCacheInitializeCache(cache);
+}
+
 /*
  *     CatalogCacheInitializeCache
  *
  * This function does final initialization of a catcache: obtain the tuple
- * descriptor and set up the hash and equality function links.  We assume
- * that the relcache entry can be opened at this point!
+ * descriptor and set up the hash and equality function links.
  */
 #ifdef CACHEDEBUG
 #define CatalogCacheInitializeCache_DEBUG1 \
@@ -1194,8 +1223,7 @@ CatalogCacheInitializeCache(CatCache *cache)
 void
 InitCatCachePhase2(CatCache *cache, bool touch_index)
 {
-   if (cache->cc_tupdesc == NULL)
-       CatalogCacheInitializeCache(cache);
+   ConditionalCatalogCacheInitializeCache(cache);
 
    if (touch_index &&
        cache->id != AMOID &&
@@ -1374,16 +1402,12 @@ SearchCatCacheInternal(CatCache *cache,
    dlist_head *bucket;
    CatCTup    *ct;
 
-   /* Make sure we're in an xact, even if this ends up being a cache hit */
-   Assert(IsTransactionState());
-
    Assert(cache->cc_nkeys == nkeys);
 
    /*
     * one-time startup overhead for each cache
     */
-   if (unlikely(cache->cc_tupdesc == NULL))
-       CatalogCacheInitializeCache(cache);
+   ConditionalCatalogCacheInitializeCache(cache);
 
 #ifdef CATCACHE_STATS
    cache->cc_searches++;
@@ -1669,8 +1693,7 @@ GetCatCacheHashValue(CatCache *cache,
    /*
     * one-time startup overhead for each cache
     */
-   if (cache->cc_tupdesc == NULL)
-       CatalogCacheInitializeCache(cache);
+   ConditionalCatalogCacheInitializeCache(cache);
 
    /*
     * calculate the hash value
@@ -1721,8 +1744,7 @@ SearchCatCacheList(CatCache *cache,
    /*
     * one-time startup overhead for each cache
     */
-   if (unlikely(cache->cc_tupdesc == NULL))
-       CatalogCacheInitializeCache(cache);
+   ConditionalCatalogCacheInitializeCache(cache);
 
    Assert(nkeys > 0 && nkeys < cache->cc_nkeys);
 
@@ -2392,8 +2414,7 @@ PrepareToInvalidateCacheTuple(Relation relation,
            continue;
 
        /* Just in case cache hasn't finished initialization yet... */
-       if (ccp->cc_tupdesc == NULL)
-           CatalogCacheInitializeCache(ccp);
+       ConditionalCatalogCacheInitializeCache(ccp);
 
        hashvalue = CatalogCacheComputeTupleHashValue(ccp, ccp->cc_nkeys, tuple);
        dbid = ccp->cc_relisshared ? (Oid) 0 : MyDatabaseId;
index 4c8715bf6721925f21f23b656e1ba7adb17799ea..92f9bf6f46634c0a7cb04fc9bb677638f206ba56 100644 (file)
@@ -631,7 +631,8 @@ PrepareInvalidationState(void)
 {
    TransInvalidationInfo *myInfo;
 
-   Assert(IsTransactionState());
+   /* PrepareToInvalidateCacheTuple() needs relcache */
+   AssertCouldGetRelation();
    /* Can't queue transactional message while collecting inplace messages. */
    Assert(inplaceInvalInfo == NULL);
 
@@ -700,7 +701,7 @@ PrepareInplaceInvalidationState(void)
 {
    InvalidationInfo *myInfo;
 
-   Assert(IsTransactionState());
+   AssertCouldGetRelation();
    /* limit of one inplace update under assembly */
    Assert(inplaceInvalInfo == NULL);
 
@@ -863,6 +864,12 @@ InvalidateSystemCaches(void)
 void
 AcceptInvalidationMessages(void)
 {
+#ifdef USE_ASSERT_CHECKING
+   /* message handlers shall access catalogs only during transactions */
+   if (IsTransactionState())
+       AssertCouldGetRelation();
+#endif
+
    ReceiveSharedInvalidMessages(LocalExecuteInvalidationMessage,
                                 InvalidateSystemCaches);
 
@@ -1327,6 +1334,9 @@ CacheInvalidateHeapTupleCommon(Relation relation,
    Oid         databaseId;
    Oid         relationId;
 
+   /* PrepareToInvalidateCacheTuple() needs relcache */
+   AssertCouldGetRelation();
+
    /* Do nothing during bootstrap */
    if (IsBootstrapProcessingMode())
        return;
index 3f1e8ce1f5f6222abba6c4c1517b47076086c5a9..9e0519cd1d02e700c8c1ce2810180bccb28c2eee 100644 (file)
@@ -2037,6 +2037,23 @@ formrdesc(const char *relationName, Oid relationReltype,
    relation->rd_isvalid = true;
 }
 
+#ifdef USE_ASSERT_CHECKING
+/*
+ *     AssertCouldGetRelation
+ *
+ *     Check safety of calling RelationIdGetRelation().
+ *
+ *     In code that reads catalogs in the event of a cache miss, call this
+ *     before checking the cache.
+ */
+void
+AssertCouldGetRelation(void)
+{
+   Assert(IsTransactionState());
+   AssertBufferLocksPermitCatalogRead();
+}
+#endif
+
 
 /* ----------------------------------------------------------------
  *              Relation Descriptor Lookup Interface
@@ -2064,8 +2081,7 @@ RelationIdGetRelation(Oid relationId)
 {
    Relation    rd;
 
-   /* Make sure we're in an xact, even if this ends up being a cache hit */
-   Assert(IsTransactionState());
+   AssertCouldGetRelation();
 
    /*
     * first try to find reldesc in the cache
index 97a4d6951598dbb0b409437c461e623c03569862..6af3b8effac4cff9fe5e2cde01be21fb2c5523d9 100644 (file)
@@ -39,6 +39,7 @@
 #include "mb/pg_wchar.h"
 #include "utils/fmgrprotos.h"
 #include "utils/memutils.h"
+#include "utils/relcache.h"
 #include "varatt.h"
 
 /*
@@ -310,7 +311,7 @@ InitializeClientEncoding(void)
    {
        Oid         utf8_to_server_proc;
 
-       Assert(IsTransactionState());
+       AssertCouldGetRelation();
        utf8_to_server_proc =
            FindDefaultConversionProc(PG_UTF8,
                                      current_server_encoding);
index a1e71013d328f2afd54f1fb2085b436604a32ce7..09c43dd9c72dcc04339277d293b4405a9b8dcc8e 100644 (file)
@@ -255,6 +255,9 @@ extern Buffer ExtendBufferedRelTo(BufferManagerRelation bmr,
 
 extern void InitBufferPoolAccess(void);
 extern void AtEOXact_Buffers(bool isCommit);
+#ifdef USE_ASSERT_CHECKING
+extern void AssertBufferLocksPermitCatalogRead(void);
+#endif
 extern char *DebugPrintBufferRefcount(Buffer buffer);
 extern void CheckPointBuffers(int flags);
 extern BlockNumber BufferGetBlockNumber(Buffer buffer);
index d70e6d37e097a0aca73ef297d40e3a42fd450f8b..5435948a1cd0000c110994501be2db71c2dd9d69 100644 (file)
@@ -129,6 +129,8 @@ extern bool LWLockAcquireOrWait(LWLock *lock, LWLockMode mode);
 extern void LWLockRelease(LWLock *lock);
 extern void LWLockReleaseClearVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val);
 extern void LWLockReleaseAll(void);
+extern void ForEachLWLockHeldByMe(void (*callback) (LWLock *, LWLockMode, void *),
+                                 void *context);
 extern bool LWLockHeldByMe(LWLock *lock);
 extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
 extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
index 18c32ea7008d404c3935bb8652ee0f0459264bfe..6cb829e59ce3e1eec90ef5f72e2ba9eddd8459b0 100644 (file)
@@ -37,6 +37,14 @@ typedef Relation *RelationPtr;
 /*
  * Routines to open (lookup) and close a relcache entry
  */
+#ifdef USE_ASSERT_CHECKING
+extern void AssertCouldGetRelation(void);
+#else
+static inline void
+AssertCouldGetRelation(void)
+{
+}
+#endif
 extern Relation RelationIdGetRelation(Oid relationId);
 extern void RelationClose(Relation relation);