1.Abstract
When accessing database tables, some information needs to be obtained from system tables. In order to improve retrieval efficiency, PostgreSQL provides caches, including SysCache and RelCache.
- SysCache
- Recently used system table tuple
- RelCache
- Schema information for recently accessed tables
2.SysCache
2.1 Some important data structures
- struct cachedesc information defining a single syscache
- struct catctup
- individual tuple in the cache.
- struct catclist
- list of tuples matching a partial key.
- struct catcache
- information for managing a cache.
- struct catcacheheader
- information for managing all the caches.
SysCache is a CatCache array.
The SysCache structure formed by the above data structure is as follows:
2.2 Some important operation functions
- Init operation
- InitCatalogCache
Use the cacheinfo array to initialize the SysCache array, call the InitCatCache function to create and initialize the CacheHeader. But after this round of initialization is completed, there are no tuples in CatCache. Therefore, there is a second stage initialization.
- InitCatalogCachePhase2
cc_skey and cc_tupdesc will be filled
Search operation
Find by different number of keys,they will invoke SearchCatCacheInternal function.
- step1: init
- step2: get a hash bucket
- step3: Iterate over hash bucket
- step4: cache miss
key1: For each iteration, the catcup structure is obtained. If we get catcup, similar to LRU, the node in the two-way list is deleted and inserted into the head (increase the priority of the next access).
key2: Negative tuple design: Solve the problem of cache penetration and improve the cache hit rate. Specifically, invalid tuples are also cached.
- cache miss
Tuple was not found in cache, so we have to try to retrieve it directly from the relation. If found, we will add it to the cache; if not found, we will add a negative cache entry instead.
- step1: init
- step2: tuple found
- step3: tuple not found and build a negative cache entry containing a fake tuple.
- Remove operation
- CatCacheRemoveCTup(red strikethrough)
When refcount is zero, it will remove cur CatCTup and invoke CatCacheRemoveCList to remove list. - CatCacheRemoveCList(blue strikethrough)
When refcount is zero, it will remove all CatCTup(member field).
3.RelCache
- RelationCacheInitialize
initializes the relation descriptor cache
- RelationCacheInitializePhase2
load the shared relcache cache file
- RelationCacheInitializePhase3
This stage will ensure that the information of pg_class, pg_attribute, pg_proc, pg_type related indexes is added to RelCache.First, the file pg_internal.init will be read and loaded. If the file does not exist, the RelationData structure will be rebuilt, added to the Hash table on RelCache, and the file will be rewritten. - Insert、Lookup and delete operations
RelationCacheInsert、RelationIdCacheLookup、RelationCacheDelete invoke hash_search.hash_search is the external interface, and the input action is distinguished according to the following behaviors.
- HASH_REMOVE
- look up key in table
- HASH_ENTER_NULL
- look up key in table, creating entry if not present
- HASH_ENTER
- same, but return NULL if out of memory
- HASH_FIND
- look up key in table, remove entry if present
end