Fix SysCacheGetAttr() to handle the case where the specified syscache has not
authorTom Lane <[email protected]>
Fri, 6 Oct 2006 18:23:48 +0000 (18:23 +0000)
committerTom Lane <[email protected]>
Fri, 6 Oct 2006 18:23:48 +0000 (18:23 +0000)
been initialized yet.  This can happen because there are code paths that call
SysCacheGetAttr() on a tuple originally fetched from a different syscache
(hopefully on the same catalog) than the one specified in the call.  It
doesn't seem useful or robust to try to prevent that from happening, so just
improve the function to cope instead.  Per bug#2678 from Jeff Trout.  The
specific example shown by Jeff is new in 8.1, but to be on the safe side
I'm backpatching 8.0 as well.  We could patch 7.x similarly but I think
that's probably overkill, given the lack of evidence of old bugs of this ilk.

src/backend/utils/cache/catcache.c
src/backend/utils/cache/syscache.c
src/include/utils/catcache.h

index cc96e0a793200c775d0d0fa33bbc8bb85f2dce92..2d7bdef79f92b5a74ac3ab0d1d09ff46a5b5e641 100644 (file)
@@ -972,7 +972,7 @@ CatalogCacheInitializeCache(CatCache *cache)
                cache->cc_skey[i].sk_strategy = BTEqualStrategyNumber;
                cache->cc_skey[i].sk_subtype = InvalidOid;
 
-               CACHE4_elog(DEBUG2, "CatalogCacheInit %s %d %p",
+               CACHE4_elog(DEBUG2, "CatalogCacheInitializeCache %s %d %p",
                                        cache->cc_relname,
                                        i,
                                        cache);
@@ -987,18 +987,20 @@ CatalogCacheInitializeCache(CatCache *cache)
 /*
  * InitCatCachePhase2 -- external interface for CatalogCacheInitializeCache
  *
- * The only reason to call this routine is to ensure that the relcache
- * has created entries for all the catalogs and indexes referenced by
- * catcaches.  Therefore, open the index too.  An exception is the indexes
- * on pg_am, which we don't use (cf. IndexScanOK).
+ * One reason to call this routine is to ensure that the relcache has
+ * created entries for all the catalogs and indexes referenced by catcaches.
+ * Therefore, provide an option to open the index as well as fixing the
+ * cache itself.  An exception is the indexes on pg_am, which we don't use
+ * (cf. IndexScanOK).
  */
 void
-InitCatCachePhase2(CatCache *cache)
+InitCatCachePhase2(CatCache *cache, bool touch_index)
 {
        if (cache->cc_tupdesc == NULL)
                CatalogCacheInitializeCache(cache);
 
-       if (cache->id != AMOID &&
+       if (touch_index &&
+               cache->id != AMOID &&
                cache->id != AMNAME)
        {
                Relation        idesc;
index d741d54d9dd77d8ad59ac3bbc22b5246a6fd8308..a70b6415e4f0cb103674805491877aa12e90162a 100644 (file)
@@ -495,7 +495,7 @@ InitCatalogCachePhase2(void)
        Assert(CacheInitialized);
 
        for (cacheId = 0; cacheId < SysCacheSize; cacheId++)
-               InitCatCachePhase2(SysCache[cacheId]);
+               InitCatCachePhase2(SysCache[cacheId], true);
 }
 
 
@@ -692,6 +692,9 @@ SearchSysCacheExistsAttName(Oid relid, const char *attname)
  * As with heap_getattr(), if the attribute is of a pass-by-reference type
  * then a pointer into the tuple data area is returned --- the caller must
  * not modify or pfree the datum!
+ *
+ * Note: it is legal to use SysCacheGetAttr() with a cacheId referencing
+ * a different cache for the same catalog the tuple was fetched from.
  */
 Datum
 SysCacheGetAttr(int cacheId, HeapTuple tup,
@@ -699,16 +702,19 @@ SysCacheGetAttr(int cacheId, HeapTuple tup,
                                bool *isNull)
 {
        /*
-        * We just need to get the TupleDesc out of the cache entry, and then
-        * we can apply heap_getattr().  We expect that the cache control data
-        * is currently valid --- if the caller recently fetched the tuple,
-        * then it should be.
+        * We just need to get the TupleDesc out of the cache entry, and then we
+        * can apply heap_getattr().  Normally the cache control data is already
+        * valid (because the caller recently fetched the tuple via this same
+        * cache), but there are cases where we have to initialize the cache here.
         */
-       if (cacheId < 0 || cacheId >= SysCacheSize)
+       if (cacheId < 0 || cacheId >= SysCacheSize ||
+               !PointerIsValid(SysCache[cacheId]))
                elog(ERROR, "invalid cache id: %d", cacheId);
-       if (!PointerIsValid(SysCache[cacheId]) ||
-               !PointerIsValid(SysCache[cacheId]->cc_tupdesc))
-               elog(ERROR, "missing cache data for cache id %d", cacheId);
+       if (!PointerIsValid(SysCache[cacheId]->cc_tupdesc))
+       {
+               InitCatCachePhase2(SysCache[cacheId], false);
+               Assert(PointerIsValid(SysCache[cacheId]->cc_tupdesc));
+       }
 
        return heap_getattr(tup, attributeNumber,
                                                SysCache[cacheId]->cc_tupdesc,
index f1523d24369c44b4bd2fe8c5d3944653f1e7366e..70b1f2edd1c584ab9e371588459e9899f15d4022 100644 (file)
@@ -167,7 +167,7 @@ extern void AtEOXact_CatCache(bool isCommit);
 extern CatCache *InitCatCache(int id, const char *relname, const char *indname,
                         int reloidattr,
                         int nkeys, const int *key);
-extern void InitCatCachePhase2(CatCache *cache);
+extern void InitCatCachePhase2(CatCache *cache, bool touch_index);
 
 extern HeapTuple SearchCatCache(CatCache *cache,
                           Datum v1, Datum v2,