catcache.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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-2016, 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. typedef struct catcache
  34. {
  35. int id; /* cache identifier --- see syscache.h */
  36. slist_node cc_next; /* list link */
  37. const char *cc_relname; /* name of relation the tuples come from */
  38. Oid cc_reloid; /* OID of relation the tuples come from */
  39. Oid cc_indexoid; /* OID of index matching cache keys */
  40. bool cc_relisshared; /* is relation shared across databases? */
  41. TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */
  42. int cc_ntup; /* # of tuples currently in this cache */
  43. int cc_nbuckets; /* # of hash buckets in this cache */
  44. int cc_nkeys; /* # of keys (1..CATCACHE_MAXKEYS) */
  45. int cc_key[CATCACHE_MAXKEYS]; /* AttrNumber of each key */
  46. PGFunction cc_hashfunc[CATCACHE_MAXKEYS]; /* hash function for each key */
  47. ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for
  48. * heap scans */
  49. bool cc_isname[CATCACHE_MAXKEYS]; /* flag "name" key columns */
  50. dlist_head cc_lists; /* list of CatCList structs */
  51. #ifdef CATCACHE_STATS
  52. long cc_searches; /* total # searches against this cache */
  53. long cc_hits; /* # of matches against existing entry */
  54. long cc_neg_hits; /* # of matches against negative entry */
  55. long cc_newloads; /* # of successful loads of new entry */
  56. /*
  57. * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of failed
  58. * searches, each of which will result in loading a negative entry
  59. */
  60. long cc_invals; /* # of entries invalidated from cache */
  61. long cc_lsearches; /* total # list-searches */
  62. long cc_lhits; /* # of matches against existing lists */
  63. #endif
  64. dlist_head *cc_bucket; /* hash buckets */
  65. } CatCache;
  66. typedef struct catctup
  67. {
  68. int ct_magic; /* for identifying CatCTup entries */
  69. #define CT_MAGIC 0x57261502
  70. CatCache *my_cache; /* link to owning catcache */
  71. /*
  72. * Each tuple in a cache is a member of a dlist that stores the elements
  73. * of its hash bucket. We keep each dlist in LRU order to speed repeated
  74. * lookups.
  75. */
  76. dlist_node cache_elem; /* list member of per-bucket list */
  77. /*
  78. * The tuple may also be a member of at most one CatCList. (If a single
  79. * catcache is list-searched with varying numbers of keys, we may have to
  80. * make multiple entries for the same tuple because of this restriction.
  81. * Currently, that's not expected to be common, so we accept the potential
  82. * inefficiency.)
  83. */
  84. struct catclist *c_list; /* containing CatCList, or NULL if none */
  85. /*
  86. * A tuple marked "dead" must not be returned by subsequent searches.
  87. * However, it won't be physically deleted from the cache until its
  88. * refcount goes to zero. (If it's a member of a CatCList, the list's
  89. * refcount must go to zero, too; also, remember to mark the list dead at
  90. * the same time the tuple is marked.)
  91. *
  92. * A negative cache entry is an assertion that there is no tuple matching
  93. * a particular key. This is just as useful as a normal entry so far as
  94. * avoiding catalog searches is concerned. Management of positive and
  95. * negative entries is identical.
  96. */
  97. int refcount; /* number of active references */
  98. bool dead; /* dead but not yet removed? */
  99. bool negative; /* negative cache entry? */
  100. uint32 hash_value; /* hash value for this tuple's keys */
  101. HeapTupleData tuple; /* tuple management header */
  102. } CatCTup;
  103. typedef struct catclist
  104. {
  105. int cl_magic; /* for identifying CatCList entries */
  106. #define CL_MAGIC 0x52765103
  107. CatCache *my_cache; /* link to owning catcache */
  108. /*
  109. * A CatCList describes the result of a partial search, ie, a search using
  110. * only the first K key columns of an N-key cache. We form the keys used
  111. * into a tuple (with other attributes NULL) to represent the stored key
  112. * set. The CatCList object contains links to cache entries for all the
  113. * table rows satisfying the partial key. (Note: none of these will be
  114. * negative cache entries.)
  115. *
  116. * A CatCList is only a member of a per-cache list; we do not currently
  117. * divide them into hash buckets.
  118. *
  119. * A list marked "dead" must not be returned by subsequent searches.
  120. * However, it won't be physically deleted from the cache until its
  121. * refcount goes to zero. (A list should be marked dead if any of its
  122. * member entries are dead.)
  123. *
  124. * If "ordered" is true then the member tuples appear in the order of the
  125. * cache's underlying index. This will be true in normal operation, but
  126. * might not be true during bootstrap or recovery operations. (namespace.c
  127. * is able to save some cycles when it is true.)
  128. */
  129. dlist_node cache_elem; /* list member of per-catcache list */
  130. int refcount; /* number of active references */
  131. bool dead; /* dead but not yet removed? */
  132. bool ordered; /* members listed in index order? */
  133. short nkeys; /* number of lookup keys specified */
  134. uint32 hash_value; /* hash value for lookup keys */
  135. HeapTupleData tuple; /* header for tuple holding keys */
  136. int n_members; /* number of member tuples */
  137. CatCTup *members[FLEXIBLE_ARRAY_MEMBER]; /* members */
  138. } CatCList;
  139. typedef struct catcacheheader
  140. {
  141. slist_head ch_caches; /* head of list of CatCache structs */
  142. int ch_ntup; /* # of tuples in all caches */
  143. } CatCacheHeader;
  144. /* this extern duplicates utils/memutils.h... */
  145. extern PGDLLIMPORT MemoryContext CacheMemoryContext;
  146. extern void CreateCacheMemoryContext(void);
  147. extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,
  148. int nkeys, const int *key,
  149. int nbuckets);
  150. extern void InitCatCachePhase2(CatCache *cache, bool touch_index);
  151. extern HeapTuple SearchCatCache(CatCache *cache,
  152. Datum v1, Datum v2,
  153. Datum v3, Datum v4);
  154. extern void ReleaseCatCache(HeapTuple tuple);
  155. extern uint32 GetCatCacheHashValue(CatCache *cache,
  156. Datum v1, Datum v2,
  157. Datum v3, Datum v4);
  158. extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
  159. Datum v1, Datum v2,
  160. Datum v3, Datum v4);
  161. extern void ReleaseCatCacheList(CatCList *list);
  162. extern void ResetCatalogCaches(void);
  163. extern void CatalogCacheFlushCatalog(Oid catId);
  164. extern void CatCacheInvalidate(CatCache *cache, uint32 hashValue);
  165. extern void PrepareToInvalidateCacheTuple(Relation relation,
  166. HeapTuple tuple,
  167. HeapTuple newtuple,
  168. void (*function) (int, uint32, Oid));
  169. extern void PrintCatCacheLeakWarning(HeapTuple tuple);
  170. extern void PrintCatCacheListLeakWarning(CatCList *list);
  171. #endif /* CATCACHE_H */