catcache.c
来自「PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统」· C语言 代码 · 共 1,927 行 · 第 1/4 页
C
1,927 行
* We have to bump the member refcounts temporarily to ensure they won't * get dropped from the cache while loading other members. We use a PG_TRY * block to ensure we can undo those refcounts if we get an error before * we finish constructing the CatCList. */ ResourceOwnerEnlargeCatCacheListRefs(CurrentResourceOwner); ctlist = NIL; PG_TRY(); { Relation relation; SysScanDesc scandesc; relation = heap_open(cache->cc_reloid, AccessShareLock); scandesc = systable_beginscan(relation, cache->cc_indexoid, true, SnapshotNow, nkeys, cur_skey); /* The list will be ordered iff we are doing an index scan */ ordered = (scandesc->irel != NULL); while (HeapTupleIsValid(ntp = systable_getnext(scandesc))) { uint32 hashValue; Index hashIndex; /* * See if there's an entry for this tuple already. */ ct = NULL; hashValue = CatalogCacheComputeTupleHashValue(cache, ntp); hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets); for (elt = DLGetHead(&cache->cc_bucket[hashIndex]); elt; elt = DLGetSucc(elt)) { ct = (CatCTup *) DLE_VAL(elt); if (ct->dead || ct->negative) continue; /* ignore dead and negative entries */ if (ct->hash_value != hashValue) continue; /* quickly skip entry if wrong hash val */ if (!ItemPointerEquals(&(ct->tuple.t_self), &(ntp->t_self))) continue; /* not same tuple */ /* * Found a match, but can't use it if it belongs to another * list already */ if (ct->c_list) continue; /* Found a match, so move it to front */ DLMoveToFront(&ct->lrulist_elem); break; } if (elt == NULL) { /* We didn't find a usable entry, so make a new one */ ct = CatalogCacheCreateEntry(cache, ntp, hashValue, hashIndex, false); } /* Careful here: add entry to ctlist, then bump its refcount */ /* This way leaves state correct if lappend runs out of memory */ ctlist = lappend(ctlist, ct); ct->refcount++; } systable_endscan(scandesc); heap_close(relation, AccessShareLock); /* * Now we can build the CatCList entry. First we need a dummy tuple * containing the key values... */ ntp = build_dummy_tuple(cache, nkeys, cur_skey); oldcxt = MemoryContextSwitchTo(CacheMemoryContext); nmembers = list_length(ctlist); cl = (CatCList *) palloc(sizeof(CatCList) + nmembers * sizeof(CatCTup *)); heap_copytuple_with_tuple(ntp, &cl->tuple); MemoryContextSwitchTo(oldcxt); heap_freetuple(ntp); /* * We are now past the last thing that could trigger an elog before we * have finished building the CatCList and remembering it in the * resource owner. So it's OK to fall out of the PG_TRY, and indeed * we'd better do so before we start marking the members as belonging * to the list. */ } PG_CATCH(); { foreach(ctlist_item, ctlist) { ct = (CatCTup *) lfirst(ctlist_item); Assert(ct->c_list == NULL); Assert(ct->refcount > 0); ct->refcount--; if (#ifndef CATCACHE_FORCE_RELEASE ct->dead &&#endif ct->refcount == 0 && (ct->c_list == NULL || ct->c_list->refcount == 0)) CatCacheRemoveCTup(cache, ct); } PG_RE_THROW(); } PG_END_TRY(); cl->cl_magic = CL_MAGIC; cl->my_cache = cache; DLInitElem(&cl->cache_elem, cl); cl->refcount = 0; /* for the moment */ cl->dead = false; cl->ordered = ordered; cl->touched = false; /* we already moved members to front */ cl->nkeys = nkeys; cl->hash_value = lHashValue; cl->n_members = nmembers; i = 0; foreach(ctlist_item, ctlist) { cl->members[i++] = ct = (CatCTup *) lfirst(ctlist_item); Assert(ct->c_list == NULL); ct->c_list = cl; /* release the temporary refcount on the member */ Assert(ct->refcount > 0); ct->refcount--; /* mark list dead if any members already dead */ if (ct->dead) cl->dead = true; } Assert(i == nmembers); DLAddHead(&cache->cc_lists, &cl->cache_elem); /* Finally, bump the list's refcount and return it */ cl->refcount++; ResourceOwnerRememberCatCacheListRef(CurrentResourceOwner, cl); CACHE3_elog(DEBUG2, "SearchCatCacheList(%s): made list of %d members", cache->cc_relname, nmembers); return cl;}/* * ReleaseCatCacheList * * Decrement the reference count of a catcache list. */voidReleaseCatCacheList(CatCList *list){ /* Safety checks to ensure we were handed a cache entry */ Assert(list->cl_magic == CL_MAGIC); Assert(list->refcount > 0); list->refcount--; ResourceOwnerForgetCatCacheListRef(CurrentResourceOwner, list); if (#ifndef CATCACHE_FORCE_RELEASE list->dead &&#endif list->refcount == 0) CatCacheRemoveCList(list->my_cache, list);}/* * CatalogCacheCreateEntry * Create a new CatCTup entry, copying the given HeapTuple and other * supplied data into it. The new entry initially has refcount 0. */static CatCTup *CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, uint32 hashValue, Index hashIndex, bool negative){ CatCTup *ct; MemoryContext oldcxt; /* * Allocate CatCTup header in cache memory, and copy the tuple there too. */ oldcxt = MemoryContextSwitchTo(CacheMemoryContext); ct = (CatCTup *) palloc(sizeof(CatCTup)); heap_copytuple_with_tuple(ntp, &ct->tuple); MemoryContextSwitchTo(oldcxt); /* * Finish initializing the CatCTup header, and add it to the cache's * linked lists and counts. */ ct->ct_magic = CT_MAGIC; ct->my_cache = cache; DLInitElem(&ct->lrulist_elem, (void *) ct); DLInitElem(&ct->cache_elem, (void *) ct); ct->c_list = NULL; ct->refcount = 0; /* for the moment */ ct->dead = false; ct->negative = negative; ct->hash_value = hashValue; DLAddHead(&CacheHdr->ch_lrulist, &ct->lrulist_elem); DLAddHead(&cache->cc_bucket[hashIndex], &ct->cache_elem); cache->cc_ntup++; CacheHdr->ch_ntup++; /* * If we've exceeded the desired size of the caches, try to throw away the * least recently used entry(s). NB: be careful not to throw away the * newly-built entry... */ if (CacheHdr->ch_ntup > CacheHdr->ch_maxtup) CatalogCacheCleanup(ct); return ct;}/* * CatalogCacheCleanup * Try to reduce the size of the catcaches when they get too big * * savect can be NULL, or a specific CatCTup not to remove even if it * has zero refcount. */static voidCatalogCacheCleanup(CatCTup *savect){ int tup_target; CatCache *ccp; Dlelem *elt, *prevelt; /* * Each time we have to do this, try to cut the cache size down to about * 90% of the maximum. */ tup_target = (CacheHdr->ch_maxtup * 9) / 10; /* * Our strategy for managing CatCLists is that, each time we have to throw * away some cache entries, we first move-to-front all the members of * CatCLists that have been touched since the last cleanup sweep. Then we * do strict LRU elimination by individual tuples, zapping a list if any * of its members gets zapped. Before PostgreSQL 8.1, we moved members to * front each time their owning list was touched, which was arguably more * fair in balancing list members against standalone tuples --- but the * overhead for large lists was horrendous. This scheme is more heavily * biased towards preserving lists, but that is not necessarily bad * either. */ for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next) { for (elt = DLGetHead(&ccp->cc_lists); elt; elt = DLGetSucc(elt)) { CatCList *cl = (CatCList *) DLE_VAL(elt); Assert(cl->cl_magic == CL_MAGIC); if (cl->touched && !cl->dead) { int i; for (i = 0; i < cl->n_members; i++) DLMoveToFront(&cl->members[i]->lrulist_elem); } cl->touched = false; } } /* Now get rid of unreferenced tuples in reverse global LRU order */ for (elt = DLGetTail(&CacheHdr->ch_lrulist); elt; elt = prevelt) { CatCTup *ct = (CatCTup *) DLE_VAL(elt); prevelt = DLGetPred(elt); if (ct->refcount == 0 && (ct->c_list == NULL || ct->c_list->refcount == 0) && ct != savect) {#ifdef CATCACHE_STATS ct->my_cache->cc_discards++;#endif CatCacheRemoveCTup(ct->my_cache, ct); /* Quit when we've removed enough tuples */ if (CacheHdr->ch_ntup <= tup_target) break; } }}/* * build_dummy_tuple * Generate a palloc'd HeapTuple that contains the specified key * columns, and NULLs for other columns. * * This is used to store the keys for negative cache entries and CatCList * entries, which don't have real tuples associated with them. */static HeapTuplebuild_dummy_tuple(CatCache *cache, int nkeys, ScanKey skeys){ HeapTuple ntp; TupleDesc tupDesc = cache->cc_tupdesc; Datum *values; char *nulls; Oid tupOid = InvalidOid; NameData tempNames[4]; int i; values = (Datum *) palloc(tupDesc->natts * sizeof(Datum)); nulls = (char *) palloc(tupDesc->natts * sizeof(char)); memset(values, 0, tupDesc->natts * sizeof(Datum)); memset(nulls, 'n', tupDesc->natts * sizeof(char)); for (i = 0; i < nkeys; i++) { int attindex = cache->cc_key[i]; Datum keyval = skeys[i].sk_argument; if (attindex > 0) { /* * Here we must be careful in case the caller passed a C string * where a NAME is wanted: convert the given argument to a * correctly padded NAME. Otherwise the memcpy() done in * heap_formtuple could fall off the end of memory. */ if (cache->cc_isname[i]) { Name newval = &tempNames[i]; namestrcpy(newval, DatumGetCString(keyval)); keyval = NameGetDatum(newval); } values[attindex - 1] = keyval; nulls[attindex - 1] = ' '; } else { Assert(attindex == ObjectIdAttributeNumber); tupOid = DatumGetObjectId(keyval); } } ntp = heap_formtuple(tupDesc, values, nulls); if (tupOid != InvalidOid) HeapTupleSetOid(ntp, tupOid); pfree(values); pfree(nulls); return ntp;}/* * PrepareToInvalidateCacheTuple() * * This is part of a rather subtle chain of events, so pay attention: * * When a tuple is inserted or deleted, it cannot be flushed from the * catcaches immediately, for reasons explained at the top of cache/inval.c. * Instead we have to add entry(s) for the tuple to a list of pending tuple * invalidations that will be done at the end of the command or transaction. * * The lists of tuples that need to be flushed are kept by inval.c. This * routine is a helper routine for inval.c. Given a tuple belonging to * the specified relation, find all catcaches it could be in, compute the * correct hash value for each such catcache, and call the specified function * to record the cache id, hash value, and tuple ItemPointer in inval.c's * lists. CatalogCacheIdInvalidate will be called later, if appropriate, * using the recorded information. * * Note that it is irrelevant whether the given tuple is actually loaded * into the catcache at the moment. Even if it's not there now, it might * be by the end of the command, or there might be a matching negative entry * to flush --- or other backends' caches might have such entries --- so * we have to make list entries to flush it later. * * Also note that it's not an error if there are no catcaches for the * specified relation. inval.c doesn't know exactly which rels have * catcaches --- it will call this routine for any tuple that's in a * system relation. */voidPrepareToInvalidateCacheTuple(Relation relation, HeapTuple tuple, void (*function) (int, uint32, ItemPointer, Oid)){ CatCache *ccp; Oid reloid; CACHE1_elog(DEBUG2, "PrepareToInvalidateCacheTuple: called"); /* * sanity checks */ Assert(RelationIsValid(relation)); Assert(HeapTupleIsValid(tuple)); Assert(PointerIsValid(function)); Assert(CacheHdr != NULL); reloid = RelationGetRelid(relation); /* ---------------- * for each cache * if the cache contains tuples from the specified relation * compute the tuple's hash value in this cache, * and call the passed function to register the information. * ---------------- */ for (ccp = CacheHdr->ch_caches; ccp; ccp = ccp->cc_next) { /* Just in case cache hasn't finished initialization yet... */ if (ccp->cc_tupdesc == NULL) CatalogCacheInitializeCache(ccp); if (ccp->cc_reloid != reloid) continue; (*function) (ccp->id, CatalogCacheComputeTupleHashValue(ccp, tuple), &tuple->t_self, ccp->cc_relisshared ? (Oid) 0 : MyDatabaseId); }}/* * Subroutines for warning about reference leaks. These are exported so * that resowner.c can call them. */voidPrintCatCacheLeakWarning(HeapTuple tuple){ CatCTup *ct = (CatCTup *) (((char *) tuple) - offsetof(CatCTup, tuple)); /* Safety check to ensure we were handed a cache entry */ Assert(ct->ct_magic == CT_MAGIC); elog(WARNING, "cache reference leak: cache %s (%d), tuple %u/%u has count %d", ct->my_cache->cc_relname, ct->my_cache->id, ItemPointerGetBlockNumber(&(tuple->t_self)), ItemPointerGetOffsetNumber(&(tuple->t_self)), ct->refcount);}voidPrintCatCacheListLeakWarning(CatCList *list){ elog(WARNING, "cache reference leak: cache %s (%d), list %p has count %d", list->my_cache->cc_relname, list->my_cache->id, list, list->refcount);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?