catcache.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*-------------------------------------------------------------------------
  2. *
  3. * catcache.h
  4. * Low-level catalog cache definitions.
  5. *
  6. * NOTE: every catalog cache must have a corresponding unique index on
  7. * the system table that it caches --- ie, the index must match the keys
  8. * used to do lookups in this cache. All cache fetches are done with
  9. * indexscans (under normal conditions). The index should be unique to
  10. * guarantee that there can only be one matching row for a key combination.
  11. *
  12. *
  13. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  14. * Portions Copyright (c) 1994, Regents of the University of California
  15. *
  16. * src/include/utils/catcache.h
  17. *
  18. *-------------------------------------------------------------------------
  19. */
  20. #ifndef CATCACHE_H
  21. #define CATCACHE_H
  22. #include "access/htup.h"
  23. #include "access/skey.h"
  24. #include "lib/ilist.h"
  25. #include "utils/relcache.h"
  26. /*
  27. * struct catctup: individual tuple in the cache.
  28. * struct catclist: list of tuples matching a partial key.
  29. * struct catcache: information for managing a cache.
  30. * struct catcacheheader: information for managing all the caches.
  31. */
  32. #define CATCACHE_MAXKEYS 4
  33. /* function computing a datum's hash */
  34. typedef uint32 (*CCHashFN) (Datum datum);
  35. /* function computing equality of two datums */
  36. typedef bool (*CCFastEqualFN) (Datum a, Datum b);
  37. typedef struct catcache
  38. {
  39. int id; /* cache identifier --- see syscache.h */
  40. int cc_nbuckets; /* # of hash buckets in this cache */
  41. TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */
  42. dlist_head *cc_bucket; /* hash buckets */
  43. CCHashFN cc_hashfunc[CATCACHE_MAXKEYS]; /* hash function for each key */
  44. CCFastEqualFN cc_fastequal[CATCACHE_MAXKEYS]; /* fast equal function for
  45. * each key */
  46. int cc_keyno[CATCACHE_MAXKEYS]; /* AttrNumber of each key */
  47. dlist_head cc_lists; /* list of CatCList structs */
  48. int cc_ntup; /* # of tuples currently in this cache */
  49. int cc_nkeys; /* # of keys (1..CATCACHE_MAXKEYS) */
  50. const char *cc_relname; /* name of relation the tuples come from */
  51. Oid cc_reloid; /* OID of relation the tuples come from */
  52. Oid cc_indexoid; /* OID of index matching cache keys */
  53. bool cc_relisshared; /* is relation shared across databases? */
  54. slist_node cc_next; /* list link */
  55. ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for heap
  56. * scans */
  57. /*
  58. * Keep these at the end, so that compiling catcache.c with CATCACHE_STATS
  59. * doesn't break ABI for other modules
  60. */
  61. #ifdef CATCACHE_STATS
  62. long cc_searches; /* total # searches against this cache */
  63. long cc_hits; /* # of matches against existing entry */
  64. long cc_neg_hits; /* # of matches against negative entry */
  65. long cc_newloads; /* # of successful loads of new entry */
  66. /*
  67. * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of failed
  68. * searches, each of which will result in loading a negative entry
  69. */
  70. long cc_invals; /* # of entries invalidated from cache */
  71. long cc_lsearches; /* total # list-searches */
  72. long cc_lhits; /* # of matches against existing lists */
  73. #endif
  74. } CatCache;
  75. typedef struct catctup
  76. {
  77. int ct_magic; /* for identifying CatCTup entries */
  78. #define CT_MAGIC 0x57261502
  79. uint32 hash_value; /* hash value for this tuple's keys */
  80. /*
  81. * Lookup keys for the entry. By-reference datums point into the tuple for
  82. * positive cache entries, and are separately allocated for negative ones.
  83. */
  84. Datum keys[CATCACHE_MAXKEYS];
  85. /*
  86. * Each tuple in a cache is a member of a dlist that stores the elements
  87. * of its hash bucket. We keep each dlist in LRU order to speed repeated
  88. * lookups.
  89. */
  90. dlist_node cache_elem; /* list member of per-bucket list */
  91. /*
  92. * A tuple marked "dead" must not be returned by subsequent searches.
  93. * However, it won't be physically deleted from the cache until its
  94. * refcount goes to zero. (If it's a member of a CatCList, the list's
  95. * refcount must go to zero, too; also, remember to mark the list dead at
  96. * the same time the tuple is marked.)
  97. *
  98. * A negative cache entry is an assertion that there is no tuple matching
  99. * a particular key. This is just as useful as a normal entry so far as
  100. * avoiding catalog searches is concerned. Management of positive and
  101. * negative entries is identical.
  102. */
  103. int refcount; /* number of active references */
  104. bool dead; /* dead but not yet removed? */
  105. bool negative; /* negative cache entry? */
  106. HeapTupleData tuple; /* tuple management header */
  107. /*
  108. * The tuple may also be a member of at most one CatCList. (If a single
  109. * catcache is list-searched with varying numbers of keys, we may have to
  110. * make multiple entries for the same tuple because of this restriction.
  111. * Currently, that's not expected to be common, so we accept the potential
  112. * inefficiency.)
  113. */
  114. struct catclist *c_list; /* containing CatCList, or NULL if none */
  115. CatCache *my_cache; /* link to owning catcache */
  116. /* properly aligned tuple data follows, unless a negative entry */
  117. } CatCTup;
  118. /*
  119. * A CatCList describes the result of a partial search, ie, a search using
  120. * only the first K key columns of an N-key cache. We store the keys used
  121. * into the keys attribute to represent the stored key set. The CatCList
  122. * object contains links to cache entries for all the table rows satisfying
  123. * the partial key. (Note: none of these will be negative cache entries.)
  124. *
  125. * A CatCList is only a member of a per-cache list; we do not currently
  126. * divide them into hash buckets.
  127. *
  128. * A list marked "dead" must not be returned by subsequent searches.
  129. * However, it won't be physically deleted from the cache until its
  130. * refcount goes to zero. (A list should be marked dead if any of its
  131. * member entries are dead.)
  132. *
  133. * If "ordered" is true then the member tuples appear in the order of the
  134. * cache's underlying index. This will be true in normal operation, but
  135. * might not be true during bootstrap or recovery operations. (namespace.c
  136. * is able to save some cycles when it is true.)
  137. */
  138. typedef struct catclist
  139. {
  140. int cl_magic; /* for identifying CatCList entries */
  141. #define CL_MAGIC 0x52765103
  142. uint32 hash_value; /* hash value for lookup keys */
  143. dlist_node cache_elem; /* list member of per-catcache list */
  144. /*
  145. * Lookup keys for the entry, with the first nkeys elements being valid.
  146. * All by-reference are separately allocated.
  147. */
  148. Datum keys[CATCACHE_MAXKEYS];
  149. int refcount; /* number of active references */
  150. bool dead; /* dead but not yet removed? */
  151. bool ordered; /* members listed in index order? */
  152. short nkeys; /* number of lookup keys specified */
  153. int n_members; /* number of member tuples */
  154. CatCache *my_cache; /* link to owning catcache */
  155. CatCTup *members[FLEXIBLE_ARRAY_MEMBER]; /* members */
  156. } CatCList;
  157. typedef struct catcacheheader
  158. {
  159. slist_head ch_caches; /* head of list of CatCache structs */
  160. int ch_ntup; /* # of tuples in all caches */
  161. } CatCacheHeader;
  162. /* this extern duplicates utils/memutils.h... */
  163. extern PGDLLIMPORT MemoryContext CacheMemoryContext;
  164. extern void CreateCacheMemoryContext(void);
  165. extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,
  166. int nkeys, const int *key,
  167. int nbuckets);
  168. extern void InitCatCachePhase2(CatCache *cache, bool touch_index);
  169. extern HeapTuple SearchCatCache(CatCache *cache,
  170. Datum v1, Datum v2, Datum v3, Datum v4);
  171. extern HeapTuple SearchCatCache1(CatCache *cache,
  172. Datum v1);
  173. extern HeapTuple SearchCatCache2(CatCache *cache,
  174. Datum v1, Datum v2);
  175. extern HeapTuple SearchCatCache3(CatCache *cache,
  176. Datum v1, Datum v2, Datum v3);
  177. extern HeapTuple SearchCatCache4(CatCache *cache,
  178. Datum v1, Datum v2, Datum v3, Datum v4);
  179. extern void ReleaseCatCache(HeapTuple tuple);
  180. extern uint32 GetCatCacheHashValue(CatCache *cache,
  181. Datum v1, Datum v2,
  182. Datum v3, Datum v4);
  183. extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
  184. Datum v1, Datum v2,
  185. Datum v3);
  186. extern void ReleaseCatCacheList(CatCList *list);
  187. extern void ResetCatalogCaches(void);
  188. extern void CatalogCacheFlushCatalog(Oid catId);
  189. extern void CatCacheInvalidate(CatCache *cache, uint32 hashValue);
  190. extern void PrepareToInvalidateCacheTuple(Relation relation,
  191. HeapTuple tuple,
  192. HeapTuple newtuple,
  193. void (*function) (int, uint32, Oid));
  194. extern void PrintCatCacheLeakWarning(HeapTuple tuple);
  195. extern void PrintCatCacheListLeakWarning(CatCList *list);
  196. #endif /* CATCACHE_H */