1 /*-------------------------------------------------------------------------
4 * Low-level catalog cache definitions.
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.
13 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
16 * $Id: catcache.h,v 1.46 2003/08/04 02:40:15 momjian Exp $
18 *-------------------------------------------------------------------------
23 #include "access/htup.h"
24 #include "lib/dllist.h"
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.
33 typedef struct catcache
35 int id; /* cache identifier --- see syscache.h */
36 struct catcache *cc_next; /* link to next catcache */
37 const char *cc_relname; /* name of relation the tuples come from */
38 const char *cc_indname; /* name of index matching cache keys */
39 Oid cc_reloid; /* OID of relation the tuples come from */
40 bool cc_relisshared; /* is relation shared across databases? */
41 TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */
42 int cc_reloidattr; /* AttrNumber of relation OID attr, or 0 */
43 int cc_ntup; /* # of tuples currently in this cache */
44 int cc_nbuckets; /* # of hash buckets in this cache */
45 int cc_nkeys; /* # of keys (1..4) */
46 int cc_key[4]; /* AttrNumber of each key */
47 PGFunction cc_hashfunc[4]; /* hash function to use for each key */
48 ScanKeyData cc_skey[4]; /* precomputed key info for heap scans */
49 bool cc_isname[4]; /* flag key columns that are NAMEs */
50 Dllist cc_lists; /* list of CatCList structs */
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 */
58 * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of
59 * failed searches, each of which will result in loading a negative
62 long cc_invals; /* # of entries invalidated from cache */
63 long cc_discards; /* # of entries discarded due to overflow */
64 long cc_lsearches; /* total # list-searches */
65 long cc_lhits; /* # of matches against existing lists */
67 Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
68 } CatCache; /* VARIABLE LENGTH STRUCT */
71 typedef struct catctup
73 int ct_magic; /* for identifying CatCTup entries */
74 #define CT_MAGIC 0x57261502
75 CatCache *my_cache; /* link to owning catcache */
78 * Each tuple in a cache is a member of two Dllists: one lists all the
79 * elements in all the caches in LRU order, and the other lists just
80 * the elements in one hashbucket of one cache, also in LRU order.
82 * The tuple may also be a member of at most one CatCList. (If a single
83 * catcache is list-searched with varying numbers of keys, we may have
84 * to make multiple entries for the same tuple because of this
85 * restriction. Currently, that's not expected to be common, so we
86 * accept the potential inefficiency.)
88 Dlelem lrulist_elem; /* list member of global LRU list */
89 Dlelem cache_elem; /* list member of per-bucket list */
90 struct catclist *c_list; /* containing catclist, or NULL if none */
93 * A tuple marked "dead" must not be returned by subsequent searches.
94 * However, it won't be physically deleted from the cache until its
95 * refcount goes to zero.
97 * A negative cache entry is an assertion that there is no tuple matching
98 * a particular key. This is just as useful as a normal entry so far
99 * as avoiding catalog searches is concerned. Management of positive
100 * and negative entries is identical.
102 int refcount; /* number of active references */
103 bool dead; /* dead but not yet removed? */
104 bool negative; /* negative cache entry? */
105 uint32 hash_value; /* hash value for this tuple's keys */
106 HeapTupleData tuple; /* tuple management header */
110 typedef struct catclist
112 int cl_magic; /* for identifying CatCList entries */
113 #define CL_MAGIC 0x52765103
114 CatCache *my_cache; /* link to owning catcache */
117 * A CatCList describes the result of a partial search, ie, a search
118 * using only the first K key columns of an N-key cache. We form the
119 * keys used into a tuple (with other attributes NULL) to represent
120 * the stored key set. The CatCList object contains links to cache
121 * entries for all the table rows satisfying the partial key. (Note:
122 * none of these will be negative cache entries.)
124 * A CatCList is only a member of a per-cache list; we do not do separate
125 * LRU management for CatCLists. Instead, a CatCList is dropped from
126 * the cache as soon as any one of its member tuples ages out due to
127 * tuple-level LRU management.
129 * A list marked "dead" must not be returned by subsequent searches.
130 * However, it won't be physically deleted from the cache until its
131 * refcount goes to zero. (Its member tuples must have refcounts at
132 * least as large, so they won't go away either.)
134 * If "ordered" is true then the member tuples appear in the order of the
135 * cache's underlying index. This will be true in normal operation,
136 * but might not be true during bootstrap or recovery operations.
137 * (namespace.c is able to save some cycles when it is true.)
139 Dlelem cache_elem; /* list member of per-catcache list */
140 int refcount; /* number of active references */
141 bool dead; /* dead but not yet removed? */
142 bool ordered; /* members listed in index order? */
143 short nkeys; /* number of lookup keys specified */
144 uint32 hash_value; /* hash value for lookup keys */
145 HeapTupleData tuple; /* header for tuple holding keys */
146 int n_members; /* number of member tuples */
147 CatCTup *members[1]; /* members --- VARIABLE LENGTH ARRAY */
148 } CatCList; /* VARIABLE LENGTH STRUCT */
151 typedef struct catcacheheader
153 CatCache *ch_caches; /* head of list of CatCache structs */
154 int ch_ntup; /* # of tuples in all caches */
155 int ch_maxtup; /* max # of tuples allowed (LRU) */
156 Dllist ch_lrulist; /* overall LRU list, most recent first */
160 /* this extern duplicates utils/memutils.h... */
161 extern DLLIMPORT MemoryContext CacheMemoryContext;
163 extern void CreateCacheMemoryContext(void);
164 extern void AtEOXact_CatCache(bool isCommit);
166 extern CatCache *InitCatCache(int id, const char *relname, const char *indname,
168 int nkeys, const int *key);
169 extern void InitCatCachePhase2(CatCache *cache);
171 extern HeapTuple SearchCatCache(CatCache *cache,
174 extern void ReleaseCatCache(HeapTuple tuple);
176 extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
179 extern void ReleaseCatCacheList(CatCList *list);
181 extern void ResetCatalogCaches(void);
182 extern void CatalogCacheFlushRelation(Oid relId);
183 extern void CatalogCacheIdInvalidate(int cacheId, uint32 hashValue,
184 ItemPointer pointer);
185 extern void PrepareToInvalidateCacheTuple(Relation relation,
187 void (*function) (int, uint32, ItemPointer, Oid));
189 #endif /* CATCACHE_H */