Commit 07dfe977 authored by Tom Lane's avatar Tom Lane

Don't run in CacheMemoryContext more than necessary.

parent d0032132
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.68 2000/06/28 03:32:24 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/catcache.c,v 1.69 2000/07/02 05:38:40 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -190,57 +190,32 @@ CatalogCacheInitializeCache(CatCache * cache, ...@@ -190,57 +190,32 @@ CatalogCacheInitializeCache(CatCache * cache,
Relation relation) Relation relation)
{ {
MemoryContext oldcxt; MemoryContext oldcxt;
short didopen = 0; bool didopen = false;
short i; short i;
TupleDesc tupdesc; TupleDesc tupdesc;
CatalogCacheInitializeCache_DEBUG1; CatalogCacheInitializeCache_DEBUG1;
/* ----------------
* first switch to the cache context so our allocations
* do not vanish at the end of a transaction
* ----------------
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/* ---------------- /* ----------------
* If no relation was passed we must open it to get access to * If no relation was passed we must open it to get access to
* its fields. If one of the other caches has already opened * its fields.
* it we use heap_open() instead of heap_openr().
* XXX is that really worth the trouble of checking?
* ---------------- * ----------------
*/ */
if (!RelationIsValid(relation)) if (!RelationIsValid(relation))
{ {
CatCache *cp; relation = heap_openr(cache->cc_relname, NoLock);
didopen = true;
/* ---------------- }
* scan the caches to see if any other cache has opened the relation
* ----------------
*/
for (cp = Caches; cp; cp = cp->cc_next)
{
if (strncmp(cp->cc_relname, cache->cc_relname, NAMEDATALEN) == 0)
{
if (cp->relationId != InvalidOid)
break;
}
}
/* ---------------- /* ----------------
* open the relation by name or by id * switch to the cache context so our allocations
* ---------------- * do not vanish at the end of a transaction
*/ * ----------------
if (cp) */
relation = heap_open(cp->relationId, NoLock); if (!CacheMemoryContext)
else CreateCacheMemoryContext();
relation = heap_openr(cache->cc_relname, NoLock);
didopen = 1; oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
}
/* ---------------- /* ----------------
* initialize the cache's relation id and tuple descriptor * initialize the cache's relation id and tuple descriptor
...@@ -285,6 +260,12 @@ CatalogCacheInitializeCache(CatCache * cache, ...@@ -285,6 +260,12 @@ CatalogCacheInitializeCache(CatCache * cache,
} }
} }
/* ----------------
* return to the caller's memory context
* ----------------
*/
MemoryContextSwitchTo(oldcxt);
/* ---------------- /* ----------------
* close the relation if we opened it * close the relation if we opened it
* ---------------- * ----------------
...@@ -313,12 +294,6 @@ CatalogCacheInitializeCache(CatCache * cache, ...@@ -313,12 +294,6 @@ CatalogCacheInitializeCache(CatCache * cache,
else else
cache->cc_indname = NULL; cache->cc_indname = NULL;
} }
/* ----------------
* return to the proper memory context
* ----------------
*/
MemoryContextSwitchTo(oldcxt);
} }
/* -------------------------------- /* --------------------------------
...@@ -433,8 +408,6 @@ CatalogCacheComputeTupleHashIndex(CatCache * cacheInOutP, ...@@ -433,8 +408,6 @@ CatalogCacheComputeTupleHashIndex(CatCache * cacheInOutP,
/* -------------------------------- /* --------------------------------
* CatCacheRemoveCTup * CatCacheRemoveCTup
*
* NB: assumes caller has switched to CacheMemoryContext
* -------------------------------- * --------------------------------
*/ */
static void static void
...@@ -482,7 +455,6 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */ ...@@ -482,7 +455,6 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */
CatCache *ccp; CatCache *ccp;
CatCTup *ct; CatCTup *ct;
Dlelem *elt; Dlelem *elt;
MemoryContext oldcxt;
/* ---------------- /* ----------------
* sanity checks * sanity checks
...@@ -492,15 +464,6 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */ ...@@ -492,15 +464,6 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */
Assert(ItemPointerIsValid(pointer)); Assert(ItemPointerIsValid(pointer));
CACHE1_elog(DEBUG, "CatalogCacheIdInvalidate: called"); CACHE1_elog(DEBUG, "CatalogCacheIdInvalidate: called");
/* ----------------
* switch to the cache context for our memory allocations
* ----------------
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/* ---------------- /* ----------------
* inspect every cache that could contain the tuple * inspect every cache that could contain the tuple
* ---------------- * ----------------
...@@ -537,12 +500,6 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */ ...@@ -537,12 +500,6 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */
if (cacheId != InvalidCatalogCacheId) if (cacheId != InvalidCatalogCacheId)
break; break;
} }
/* ----------------
* return to the proper memory context
* ----------------
*/
MemoryContextSwitchTo(oldcxt);
} }
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
...@@ -562,21 +519,10 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */ ...@@ -562,21 +519,10 @@ CatalogCacheIdInvalidate(int cacheId, /* XXX */
void void
ResetSystemCache() ResetSystemCache()
{ {
MemoryContext oldcxt;
CatCache *cache; CatCache *cache;
CACHE1_elog(DEBUG, "ResetSystemCache called"); CACHE1_elog(DEBUG, "ResetSystemCache called");
/* ----------------
* first switch to the cache context so our allocations
* do not vanish at the end of a transaction
* ----------------
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/* ---------------- /* ----------------
* here we purge the contents of all the caches * here we purge the contents of all the caches
* *
...@@ -609,12 +555,6 @@ ResetSystemCache() ...@@ -609,12 +555,6 @@ ResetSystemCache()
} }
CACHE1_elog(DEBUG, "end of ResetSystemCache call"); CACHE1_elog(DEBUG, "end of ResetSystemCache call");
/* ----------------
* back to the old context before we return...
* ----------------
*/
MemoryContextSwitchTo(oldcxt);
} }
/* -------------------------------- /* --------------------------------
...@@ -682,7 +622,7 @@ do { \ ...@@ -682,7 +622,7 @@ do { \
CatCache * CatCache *
InitSysCache(char *relname, InitSysCache(char *relname,
char *iname, char *indname,
int id, int id,
int nkeys, int nkeys,
int *key, int *key,
...@@ -692,10 +632,6 @@ InitSysCache(char *relname, ...@@ -692,10 +632,6 @@ InitSysCache(char *relname,
int i; int i;
MemoryContext oldcxt; MemoryContext oldcxt;
char *indname;
indname = (iname) ? iname : NULL;
/* ---------------- /* ----------------
* first switch to the cache context so our allocations * first switch to the cache context so our allocations
* do not vanish at the end of a transaction * do not vanish at the end of a transaction
...@@ -805,6 +741,7 @@ InitSysCache(char *relname, ...@@ -805,6 +741,7 @@ InitSysCache(char *relname,
* ---------------- * ----------------
*/ */
MemoryContextSwitchTo(oldcxt); MemoryContextSwitchTo(oldcxt);
return cp; return cp;
} }
...@@ -927,7 +864,7 @@ SearchSysCache(CatCache * cache, ...@@ -927,7 +864,7 @@ SearchSysCache(CatCache * cache,
CatCTup *nct; CatCTup *nct;
CatCTup *nct2; CatCTup *nct2;
Dlelem *elt; Dlelem *elt;
HeapTuple ntp = NULL; HeapTuple ntp;
Relation relation; Relation relation;
MemoryContext oldcxt; MemoryContext oldcxt;
...@@ -950,7 +887,7 @@ SearchSysCache(CatCache * cache, ...@@ -950,7 +887,7 @@ SearchSysCache(CatCache * cache,
/* /*
* resolve self referencing informtion * resolve self referencing informtion
*/ */
if ((ntp = SearchSelfReferences(cache))) if ((ntp = SearchSelfReferences(cache)) != NULL)
return ntp; return ntp;
/* ---------------- /* ----------------
...@@ -1034,16 +971,6 @@ SearchSysCache(CatCache * cache, ...@@ -1034,16 +971,6 @@ SearchSysCache(CatCache * cache,
CACHE2_elog(DEBUG, "SearchSysCache(%s)", CACHE2_elog(DEBUG, "SearchSysCache(%s)",
RelationGetRelationName(relation)); RelationGetRelationName(relation));
/* ----------------
* Switch to the cache memory context.
* ----------------
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/* ---------------- /* ----------------
* Scan the relation to find the tuple. If there's an index, and * Scan the relation to find the tuple. If there's an index, and
* if this isn't bootstrap (initdb) time, use the index. * if this isn't bootstrap (initdb) time, use the index.
...@@ -1056,14 +983,6 @@ SearchSysCache(CatCache * cache, ...@@ -1056,14 +983,6 @@ SearchSysCache(CatCache * cache,
{ {
HeapTuple indextp; HeapTuple indextp;
/* ----------
* Switch back to old memory context so memory not freed
* in the scan function will go away at transaction end.
* wieck - 10/18/1996
* ----------
*/
MemoryContextSwitchTo(oldcxt);
/* We call the scanfunc with all four arguments to satisfy the /* We call the scanfunc with all four arguments to satisfy the
* declared prototype, even though the function will likely not * declared prototype, even though the function will likely not
* use all four. * use all four.
...@@ -1071,53 +990,39 @@ SearchSysCache(CatCache * cache, ...@@ -1071,53 +990,39 @@ SearchSysCache(CatCache * cache,
indextp = cache->cc_iscanfunc(relation, v1, v2, v3, v4); indextp = cache->cc_iscanfunc(relation, v1, v2, v3, v4);
/* ---------- /* ----------
* Back to Cache context. If we got a tuple copy it * If we got a tuple copy it into our context. wieck - 10/18/1996
* into our context. wieck - 10/18/1996
* And free the tuple that was allocated in the * And free the tuple that was allocated in the
* transaction's context. tgl - 02/03/2000 * transaction's context. tgl - 02/03/2000
* ---------- * ----------
*/ */
if (HeapTupleIsValid(indextp)) if (HeapTupleIsValid(indextp))
{ {
MemoryContextSwitchTo(CacheMemoryContext); oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
ntp = heap_copytuple(indextp); ntp = heap_copytuple(indextp);
/* this switch is probably not needed anymore: */
MemoryContextSwitchTo(oldcxt); MemoryContextSwitchTo(oldcxt);
heap_freetuple(indextp); heap_freetuple(indextp);
} }
MemoryContextSwitchTo(CacheMemoryContext);
} }
else else
{ {
HeapScanDesc sd; HeapScanDesc sd;
/* ----------
* As above do the lookup in the callers memory
* context.
* wieck - 10/18/1996
* ----------
*/
MemoryContextSwitchTo(oldcxt);
sd = heap_beginscan(relation, 0, SnapshotNow, sd = heap_beginscan(relation, 0, SnapshotNow,
cache->cc_nkeys, cache->cc_skey); cache->cc_nkeys, cache->cc_skey);
ntp = heap_getnext(sd, 0); ntp = heap_getnext(sd, 0);
MemoryContextSwitchTo(CacheMemoryContext);
if (HeapTupleIsValid(ntp)) if (HeapTupleIsValid(ntp))
{ {
CACHE1_elog(DEBUG, "SearchSysCache: found tuple"); CACHE1_elog(DEBUG, "SearchSysCache: found tuple");
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
ntp = heap_copytuple(ntp); ntp = heap_copytuple(ntp);
MemoryContextSwitchTo(oldcxt);
/* We should not free the result of heap_getnext... */ /* We should not free the result of heap_getnext... */
} }
MemoryContextSwitchTo(oldcxt);
heap_endscan(sd); heap_endscan(sd);
MemoryContextSwitchTo(CacheMemoryContext);
} }
cache->busy = false; cache->busy = false;
...@@ -1136,6 +1041,8 @@ SearchSysCache(CatCache * cache, ...@@ -1136,6 +1041,8 @@ SearchSysCache(CatCache * cache,
*/ */
Dlelem *lru_elt; Dlelem *lru_elt;
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/* /*
* this is a little cumbersome here because we want the Dlelem's * this is a little cumbersome here because we want the Dlelem's
* in both doubly linked lists to point to one another. That makes * in both doubly linked lists to point to one another. That makes
...@@ -1154,6 +1061,8 @@ SearchSysCache(CatCache * cache, ...@@ -1154,6 +1061,8 @@ SearchSysCache(CatCache * cache,
DLAddHead(cache->cc_lrulist, lru_elt); DLAddHead(cache->cc_lrulist, lru_elt);
DLAddHead(cache->cc_cache[hash], elt); DLAddHead(cache->cc_cache[hash], elt);
MemoryContextSwitchTo(oldcxt);
/* ---------------- /* ----------------
* If we've exceeded the desired size of this cache, * If we've exceeded the desired size of this cache,
* throw away the least recently used entry. * throw away the least recently used entry.
...@@ -1183,14 +1092,11 @@ SearchSysCache(CatCache * cache, ...@@ -1183,14 +1092,11 @@ SearchSysCache(CatCache * cache,
} }
/* ---------------- /* ----------------
* close the relation, switch back to the original memory context * close the relation and return the tuple we found (or NULL)
* and return the tuple we found (or NULL)
* ---------------- * ----------------
*/ */
heap_close(relation, AccessShareLock); heap_close(relation, AccessShareLock);
MemoryContextSwitchTo(oldcxt);
return ntp; return ntp;
} }
...@@ -1208,8 +1114,7 @@ RelationInvalidateCatalogCacheTuple(Relation relation, ...@@ -1208,8 +1114,7 @@ RelationInvalidateCatalogCacheTuple(Relation relation,
HeapTuple tuple, HeapTuple tuple,
void (*function) (int, Index, ItemPointer)) void (*function) (int, Index, ItemPointer))
{ {
CatCache *ccp; CatCache *ccp;
MemoryContext oldcxt;
Oid relationId; Oid relationId;
/* ---------------- /* ----------------
...@@ -1221,15 +1126,6 @@ RelationInvalidateCatalogCacheTuple(Relation relation, ...@@ -1221,15 +1126,6 @@ RelationInvalidateCatalogCacheTuple(Relation relation,
Assert(PointerIsValid(function)); Assert(PointerIsValid(function));
CACHE1_elog(DEBUG, "RelationInvalidateCatalogCacheTuple: called"); CACHE1_elog(DEBUG, "RelationInvalidateCatalogCacheTuple: called");
/* ----------------
* switch to the cache memory context
* ----------------
*/
if (!CacheMemoryContext)
CreateCacheMemoryContext();
oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
/* ---------------- /* ----------------
* for each cache * for each cache
* if the cache contains tuples from the specified relation * if the cache contains tuples from the specified relation
...@@ -1244,20 +1140,8 @@ RelationInvalidateCatalogCacheTuple(Relation relation, ...@@ -1244,20 +1140,8 @@ RelationInvalidateCatalogCacheTuple(Relation relation,
if (relationId != ccp->relationId) if (relationId != ccp->relationId)
continue; continue;
#ifdef NOT_USED
/* OPT inline simplification of CatalogCacheIdInvalidate */
if (!PointerIsValid(function))
function = CatalogCacheIdInvalidate;
#endif
(*function) (ccp->id, (*function) (ccp->id,
CatalogCacheComputeTupleHashIndex(ccp, relation, tuple), CatalogCacheComputeTupleHashIndex(ccp, relation, tuple),
&tuple->t_self); &tuple->t_self);
} }
/* ----------------
* return to the proper memory context
* ----------------
*/
MemoryContextSwitchTo(oldcxt);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment