diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c index 0c86a58..93ee6b9 100644 --- a/src/backend/storage/ipc/ipci.c +++ b/src/backend/storage/ipc/ipci.c @@ -46,7 +46,7 @@ #include "storage/spin.h" #include "utils/backend_random.h" #include "utils/snapmgr.h" - +#include "utils/catcache.h" shmem_startup_hook_type shmem_startup_hook = NULL; @@ -150,6 +150,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port) size = add_size(size, SyncScanShmemSize()); size = add_size(size, AsyncShmemSize()); size = add_size(size, BackendRandomShmemSize()); + size = add_size(size, CatCacheShmemSize()); #ifdef EXEC_BACKEND size = add_size(size, ShmemBackendArraySize()); #endif @@ -270,6 +271,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port) SyncScanShmemInit(); AsyncShmemInit(); BackendRandomShmemInit(); + CatCacheShmemInit(); #ifdef EXEC_BACKEND diff --git a/src/backend/storage/lmgr/lwlocknames.txt b/src/backend/storage/lmgr/lwlocknames.txt index e6025ec..91a2d04 100644 --- a/src/backend/storage/lmgr/lwlocknames.txt +++ b/src/backend/storage/lmgr/lwlocknames.txt @@ -50,3 +50,4 @@ OldSnapshotTimeMapLock 42 BackendRandomLock 43 LogicalRepWorkerLock 44 CLogTruncationLock 45 +CatCacheVarLock 46 diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c index 5ddbf6e..c47d73d 100644 --- a/src/backend/utils/cache/catcache.c +++ b/src/backend/utils/cache/catcache.c @@ -71,6 +71,15 @@ #define CACHE6_elog(a,b,c,d,e,f,g) #endif +/* proto: dummy shared struct data */ +typedef struct CatcacheSharedVar +{ + int32 var; +}CatcacheSharedVar; + + +static CatcacheSharedVar *catcacheSharedVar = NULL; + /* Cache management header --- pointer is NULL until created */ static CatCacheHeader *CacheHdr = NULL; @@ -624,6 +633,33 @@ CatCacheInvalidate(CatCache *cache, uint32 hashValue) * ---------------------------------------------------------------- */ +/* + * Report space needed for dummy shared memory areas + */ +Size +CatCacheShmemSize(void) +{ + Size size; + size = sizeof(CatcacheSharedVar); + return size; +} + + +/* + * Initialize dummy shared memory areas + */ +void +CatCacheShmemInit(void) +{ + bool found; + + catcacheSharedVar = (CatcacheSharedVar *)ShmemInitStruct("Shared CatCache data (dummy)",sizeof(CatcacheSharedVar), &found); + + if (!found) + { + catcacheSharedVar->var = 1; + } +} /* * Standard routine for creating cache context if it doesn't exist yet @@ -1261,6 +1297,8 @@ SearchCatCacheInternal(CatCache *cache, * dlist within the loop, because we don't continue the loop afterwards. */ bucket = &cache->cc_bucket[hashIndex]; + LWLockAcquire(CatCacheVarLock,LW_SHARED); + dlist_foreach(iter, bucket) { ct = dlist_container(CatCTup, cache_elem, iter.cur); @@ -1298,7 +1336,7 @@ SearchCatCacheInternal(CatCache *cache, #ifdef CATCACHE_STATS cache->cc_hits++; #endif - + LWLockRelease(CatCacheVarLock); return &ct->tuple; } else @@ -1309,11 +1347,11 @@ SearchCatCacheInternal(CatCache *cache, #ifdef CATCACHE_STATS cache->cc_neg_hits++; #endif - + LWLockRelease(CatCacheVarLock); return NULL; } } - + LWLockRelease(CatCacheVarLock); return SearchCatCacheMiss(cache, nkeys, hashValue, hashIndex, v1, v2, v3, v4); } @@ -1382,6 +1420,8 @@ SearchCatCacheMiss(CatCache *cache, cur_skey); ct = NULL; + + LWLockAcquire(CatCacheVarLock,LW_EXCLUSIVE); while (HeapTupleIsValid(ntp = systable_getnext(scandesc))) { @@ -1395,6 +1435,8 @@ SearchCatCacheMiss(CatCache *cache, break; /* assume only one match */ } + LWLockRelease(CatCacheVarLock); + systable_endscan(scandesc); heap_close(relation, AccessShareLock); @@ -1414,9 +1456,11 @@ SearchCatCacheMiss(CatCache *cache, if (IsBootstrapProcessingMode()) return NULL; + LWLockAcquire(CatCacheVarLock,LW_EXCLUSIVE); ct = CatalogCacheCreateEntry(cache, NULL, arguments, hashValue, hashIndex, true); + LWLockRelease(CatCacheVarLock); CACHE4_elog(DEBUG2, "SearchCatCache(%s): Contains %d/%d tuples", cache->cc_relname, cache->cc_ntup, CacheHdr->ch_ntup); diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h index 7b22f9c..7fddfec 100644 --- a/src/include/utils/catcache.h +++ b/src/include/utils/catcache.h @@ -189,6 +189,9 @@ typedef struct catcacheheader /* this extern duplicates utils/memutils.h... */ extern PGDLLIMPORT MemoryContext CacheMemoryContext; +extern void CatCacheShmemInit(void); +extern Size CatCacheShmemSize(void); + extern void CreateCacheMemoryContext(void); extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,