*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.42 1999/05/31 23:48:04 tgl Exp $
- *
- * Notes:
- * XXX This needs to use exception.h to handle recovery when
- * an abort occurs during DisableCache.
+ * $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.43 1999/06/04 02:19:45 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#define CACHE6_elog(a,b,c,d,e,f,g)
#endif
-CatCache *Caches = NULL;
-GlobalMemory CacheCxt;
+static CatCache *Caches = NULL; /* head of list of caches */
+
+GlobalMemory CacheCxt; /* context in which caches are allocated */
+/* CacheCxt is global because relcache uses it too. */
-static int DisableCache;
/* ----------------
* EQPROC is used in CatalogCacheInitializeCache
MemoryContext oldcxt;
struct catcache *cache;
- /* ----------------
- * sanity checks
- * ----------------
- */
CACHE1_elog(DEBUG, "ResetSystemCache called");
- if (DisableCache)
- {
- elog(ERROR, "ResetSystemCache: Called while cache disabled");
- return;
- }
/* ----------------
* first switch to the cache context so our allocations
{
nextelt = DLGetSucc(elt);
CatCacheRemoveCTup(cache, elt);
- if (cache->cc_ntup == -1)
- elog(ERROR, "ResetSystemCache: cc_ntup<0 (software error)");
+ if (cache->cc_ntup < 0)
+ elog(NOTICE,
+ "ResetSystemCache: cc_ntup<0 (software error)");
}
}
cache->cc_ntup = 0; /* in case of WARN error above */
+ cache->busy = false; /* to recover from recursive-use error */
}
CACHE1_elog(DEBUG, "end of ResetSystemCache call");
/* --------------------------------
* SystemCacheRelationFlushed
*
- * RelationFlushRelation() frees some information referenced in the
- * cache structures. So we get informed when this is done and arrange
- * for the next SearchSysCache() call that this information is setup
- * again.
+ * This is called by RelationFlushRelation() to clear out cached information
+ * about a relation being dropped. (This could be a DROP TABLE command,
+ * or a temp table being dropped at end of transaction, or a table created
+ * during the current transaction that is being dropped because of abort.)
+ * Remove all cache entries relevant to the specified relation OID.
+ *
+ * A special case occurs when relId is itself one of the cacheable system
+ * tables --- although those'll never be dropped, they can get flushed from
+ * the relcache (VACUUM causes this, for example). In that case we need to
+ * force the next SearchSysCache() call to reinitialize the cache itself,
+ * because we have info (such as cc_tupdesc) that is pointing at the about-
+ * to-be-deleted relcache entry.
* --------------------------------
*/
void
{
struct catcache *cache;
+ /*
+ * XXX Ideally we'd search the caches and just zap entries that actually
+ * refer to the indicated relation. For now, we take the brute-force
+ * approach: just flush the caches entirely.
+ */
+ ResetSystemCache();
+
+ /*
+ * If relcache is dropping a system relation's cache entry, mark the
+ * associated cache structures invalid, so we can rebuild them from
+ * scratch (not just repopulate them) next time they are used.
+ */
for (cache = Caches; PointerIsValid(cache); cache = cache->cc_next)
{
if (cache->relationId == relId)
cp->cc_indname = indname;
cp->cc_tupdesc = (TupleDesc) NULL;
cp->id = id;
+ cp->busy = false;
cp->cc_maxtup = MAXTUP;
cp->cc_size = NCCBUCK;
cp->cc_nkeys = nkeys;
/* ----------------
* Tuple was not found in cache, so we have to try and
* retrieve it directly from the relation. If it's found,
- * we add it to the cache. We must avoid recursion here,
- * so we disable cache operations. If operations are
- * currently disabled and we couldn't find the requested item
- * in the cache, then this may be a recursive request, and we
- * abort with an error.
+ * we add it to the cache.
+ *
+ * To guard against possible infinite recursion, we mark this cache
+ * "busy" while trying to load a new entry for it. It is OK to
+ * recursively invoke SearchSysCache for a different cache, but
+ * a recursive call for the same cache will error out. (We could
+ * store the specific key(s) being looked for, and consider only
+ * a recursive request for the same key to be an error, but this
+ * simple scheme is sufficient for now.)
* ----------------
*/
- if (DisableCache)
+ if (cache->busy)
{
- elog(ERROR, "SearchSysCache: Called while cache disabled");
- return (HeapTuple) NULL;
+ elog(ERROR, "SearchSysCache: recursive use of cache %d", cache->id);
}
+ cache->busy = true;
/* ----------------
* open the relation associated with the cache
RelationGetRelationName(relation));
/* ----------------
- * DisableCache and then switch to the cache memory context.
+ * Switch to the cache memory context.
* ----------------
*/
- DisableCache = 1;
if (!CacheCxt)
CacheCxt = CreateGlobalMemory("Cache");
MemoryContextSwitchTo((MemoryContext) CacheCxt);
}
- DisableCache = 0;
+ cache->busy = false;
/* ----------------
* scan is complete. if tup is valid, we copy it and add the copy to
DLAddHead(cache->cc_cache[hash], elt);
/* ----------------
- * deal with hash bucket overflow
+ * If we've exceeded the desired size of this cache,
+ * throw away the least recently used entry.
* ----------------
*/
if (++cache->cc_ntup > cache->cc_maxtup)
elt = DLGetTail(cache->cc_lrulist);
ct = (CatCTup *) DLE_VAL(elt);
- if (ct != nct)
+ if (ct != nct) /* shouldn't be possible, but be safe... */
{
CACHE2_elog(DEBUG, "SearchSysCache(%s): Overflow, LRU removal",
RelationGetRelationName(relation));
CatCacheRemoveCTup(cache, elt);
-
}
}
*
* Copyright (c) 1994, Regents of the University of California
*
- * $Id: catcache.h,v 1.14 1999/02/13 23:22:16 momjian Exp $
+ * $Id: catcache.h,v 1.15 1999/06/04 02:19:44 tgl Exp $
*
*-------------------------------------------------------------------------
*/
typedef struct catctup
{
HeapTuple ct_tup; /* A pointer to a tuple */
- Dlelem *ct_node; /* points to LRU list is the CatCTup is in
- * the cache, else, points to the cache if
- * the CatCTup is in LRU list */
+ /* Each tuple in the cache has two catctup items, one in the LRU list
+ * and one in the hashbucket list for its hash value. ct_node in each
+ * one points to the other one.
+ */
+ Dlelem *ct_node; /* the other catctup for this tuple */
} CatCTup;
/* voodoo constants */
HeapTuple (*cc_iscanfunc) (); /* index scanfunction */
TupleDesc cc_tupdesc; /* tuple descriptor from reldesc */
int id; /* XXX could be improved -hirohama */
+ bool busy; /* for detecting recursive lookups */
short cc_ntup; /* # of tuples in this cache */
short cc_maxtup; /* max # of tuples allowed (LRU) */
short cc_nkeys;
ScanKeyData cc_skey[4];
struct catcache *cc_next;
Dllist *cc_lrulist; /* LRU list, most recent first */
- Dllist *cc_cache[NCCBUCK + 1];
+ Dllist *cc_cache[NCCBUCK + 1]; /* hash buckets */
} CatCache;
#define InvalidCatalogCacheId (-1)
-extern struct catcache *Caches;
extern GlobalMemory CacheCxt;
extern void CatalogCacheIdInvalidate(int cacheId, Index hashIndex,