codekingpro/portable-devtools
116k
1/*-------------------------------------------------------------------------2 *3 * catcache.h4 * Low-level catalog cache definitions.5 *6 * NOTE: every catalog cache must have a corresponding unique index on7 * the system table that it caches --- ie, the index must match the keys8 * used to do lookups in this cache. All cache fetches are done with9 * indexscans (under normal conditions). The index should be unique to10 * guarantee that there can only be one matching row for a key combination.11 *12 *13 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group14 * Portions Copyright (c) 1994, Regents of the University of California15 *16 * src/include/utils/catcache.h17 *18 *-------------------------------------------------------------------------19 */20#ifndef CATCACHE_H21#define CATCACHE_H22 23#include "access/htup.h"24#include "access/skey.h"25#include "lib/ilist.h"26#include "utils/relcache.h"27 28/*29 * struct catctup: individual tuple in the cache.30 * struct catclist: list of tuples matching a partial key.31 * struct catcache: information for managing a cache.32 * struct catcacheheader: information for managing all the caches.33 */34 35#define CATCACHE_MAXKEYS 436 37 38/* function computing a datum's hash */39typedef uint32 (*CCHashFN) (Datum datum);40 41/* function computing equality of two datums */42typedef bool (*CCFastEqualFN) (Datum a, Datum b);43 44typedef struct catcache45{46 int id; /* cache identifier --- see syscache.h */47 int cc_nbuckets; /* # of hash buckets in this cache */48 TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */49 dlist_head *cc_bucket; /* hash buckets */50 CCHashFN cc_hashfunc[CATCACHE_MAXKEYS]; /* hash function for each key */51 CCFastEqualFN cc_fastequal[CATCACHE_MAXKEYS]; /* fast equal function for52 * each key */53 int cc_keyno[CATCACHE_MAXKEYS]; /* AttrNumber of each key */54 dlist_head cc_lists; /* list of CatCList structs */55 int cc_ntup; /* # of tuples currently in this cache */56 int cc_nkeys; /* # of keys (1..CATCACHE_MAXKEYS) */57 const char *cc_relname; /* name of relation the tuples come from */58 Oid cc_reloid; /* OID of relation the tuples come from */59 Oid cc_indexoid; /* OID of index matching cache keys */60 bool cc_relisshared; /* is relation shared across databases? */61 slist_node cc_next; /* list link */62 ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for heap63 * scans */64 65 /* These fields are placed here to avoid ABI breakage in v16 */66 int cc_nlist; /* # of CatCLists currently in this cache */67 int cc_nlbuckets; /* # of CatCList hash buckets in this cache */68 dlist_head *cc_lbucket; /* hash buckets for CatCLists */69 70 /*71 * Keep these at the end, so that compiling catcache.c with CATCACHE_STATS72 * doesn't break ABI for other modules73 */74#ifdef CATCACHE_STATS75 long cc_searches; /* total # searches against this cache */76 long cc_hits; /* # of matches against existing entry */77 long cc_neg_hits; /* # of matches against negative entry */78 long cc_newloads; /* # of successful loads of new entry */79 80 /*81 * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of failed82 * searches, each of which will result in loading a negative entry83 */84 long cc_invals; /* # of entries invalidated from cache */85 long cc_lsearches; /* total # list-searches */86 long cc_lhits; /* # of matches against existing lists */87#endif88} CatCache;89 90 91typedef struct catctup92{93 int ct_magic; /* for identifying CatCTup entries */94#define CT_MAGIC 0x5726150295 96 uint32 hash_value; /* hash value for this tuple's keys */97 98 /*99 * Lookup keys for the entry. By-reference datums point into the tuple for100 * positive cache entries, and are separately allocated for negative ones.101 */102 Datum keys[CATCACHE_MAXKEYS];103 104 /*105 * Each tuple in a cache is a member of a dlist that stores the elements106 * of its hash bucket. We keep each dlist in LRU order to speed repeated107 * lookups.108 */109 dlist_node cache_elem; /* list member of per-bucket list */110 111 /*112 * A tuple marked "dead" must not be returned by subsequent searches.113 * However, it won't be physically deleted from the cache until its114 * refcount goes to zero. (If it's a member of a CatCList, the list's115 * refcount must go to zero, too; also, remember to mark the list dead at116 * the same time the tuple is marked.)117 *118 * A negative cache entry is an assertion that there is no tuple matching119 * a particular key. This is just as useful as a normal entry so far as120 * avoiding catalog searches is concerned. Management of positive and121 * negative entries is identical.122 */123 int refcount; /* number of active references */124 bool dead; /* dead but not yet removed? */125 bool negative; /* negative cache entry? */126 HeapTupleData tuple; /* tuple management header */127 128 /*129 * The tuple may also be a member of at most one CatCList. (If a single130 * catcache is list-searched with varying numbers of keys, we may have to131 * make multiple entries for the same tuple because of this restriction.132 * Currently, that's not expected to be common, so we accept the potential133 * inefficiency.)134 */135 struct catclist *c_list; /* containing CatCList, or NULL if none */136 137 CatCache *my_cache; /* link to owning catcache */138 /* properly aligned tuple data follows, unless a negative entry */139} CatCTup;140 141 142/*143 * A CatCList describes the result of a partial search, ie, a search using144 * only the first K key columns of an N-key cache. We store the keys used145 * into the keys attribute to represent the stored key set. The CatCList146 * object contains links to cache entries for all the table rows satisfying147 * the partial key. (Note: none of these will be negative cache entries.)148 *149 * A CatCList is only a member of a per-cache list; we do not currently150 * divide them into hash buckets.151 *152 * A list marked "dead" must not be returned by subsequent searches.153 * However, it won't be physically deleted from the cache until its154 * refcount goes to zero. (A list should be marked dead if any of its155 * member entries are dead.)156 *157 * If "ordered" is true then the member tuples appear in the order of the158 * cache's underlying index. This will be true in normal operation, but159 * might not be true during bootstrap or recovery operations. (namespace.c160 * is able to save some cycles when it is true.)161 */162typedef struct catclist163{164 int cl_magic; /* for identifying CatCList entries */165#define CL_MAGIC 0x52765103166 167 uint32 hash_value; /* hash value for lookup keys */168 169 dlist_node cache_elem; /* list member of per-catcache list */170 171 /*172 * Lookup keys for the entry, with the first nkeys elements being valid.173 * All by-reference are separately allocated.174 */175 Datum keys[CATCACHE_MAXKEYS];176 177 int refcount; /* number of active references */178 bool dead; /* dead but not yet removed? */179 bool ordered; /* members listed in index order? */180 short nkeys; /* number of lookup keys specified */181 int n_members; /* number of member tuples */182 CatCache *my_cache; /* link to owning catcache */183 CatCTup *members[FLEXIBLE_ARRAY_MEMBER]; /* members */184} CatCList;185 186 187typedef struct catcacheheader188{189 slist_head ch_caches; /* head of list of CatCache structs */190 int ch_ntup; /* # of tuples in all caches */191} CatCacheHeader;192 193 194/* this extern duplicates utils/memutils.h... */195extern PGDLLIMPORT MemoryContext CacheMemoryContext;196 197extern void CreateCacheMemoryContext(void);198 199extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,200 int nkeys, const int *key,201 int nbuckets);202extern void InitCatCachePhase2(CatCache *cache, bool touch_index);203 204extern HeapTuple SearchCatCache(CatCache *cache,205 Datum v1, Datum v2, Datum v3, Datum v4);206extern HeapTuple SearchCatCache1(CatCache *cache,207 Datum v1);208extern HeapTuple SearchCatCache2(CatCache *cache,209 Datum v1, Datum v2);210extern HeapTuple SearchCatCache3(CatCache *cache,211 Datum v1, Datum v2, Datum v3);212extern HeapTuple SearchCatCache4(CatCache *cache,213 Datum v1, Datum v2, Datum v3, Datum v4);214extern void ReleaseCatCache(HeapTuple tuple);215 216extern uint32 GetCatCacheHashValue(CatCache *cache,217 Datum v1, Datum v2,218 Datum v3, Datum v4);219 220extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,221 Datum v1, Datum v2,222 Datum v3);223extern void ReleaseCatCacheList(CatCList *list);224 225extern void ResetCatalogCaches(void);226extern void CatalogCacheFlushCatalog(Oid catId);227extern void CatCacheInvalidate(CatCache *cache, uint32 hashValue);228extern void PrepareToInvalidateCacheTuple(Relation relation,229 HeapTuple tuple,230 HeapTuple newtuple,231 void (*function) (int, uint32, Oid));232 233extern void PrintCatCacheLeakWarning(HeapTuple tuple);234extern void PrintCatCacheListLeakWarning(CatCList *list);235 236#endif /* CATCACHE_H */237 