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-2008, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
18 *-------------------------------------------------------------------------
23 #include "access/htup.h"
24 #include "access/skey.h"
25 #include "lib/dllist.h"
26 #include "utils/relcache.h"
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.
35 typedef struct catcache
37 int id
; /* cache identifier --- see syscache.h */
38 struct catcache
*cc_next
; /* link to next catcache */
39 const char *cc_relname
; /* name of relation the tuples come from */
40 Oid cc_reloid
; /* OID of relation the tuples come from */
41 Oid cc_indexoid
; /* OID of index matching cache keys */
42 bool cc_relisshared
; /* is relation shared across databases? */
43 TupleDesc cc_tupdesc
; /* tuple descriptor (copied from reldesc) */
44 int cc_reloidattr
; /* AttrNumber of relation OID attr, or 0 */
45 int cc_ntup
; /* # of tuples currently in this cache */
46 int cc_nbuckets
; /* # of hash buckets in this cache */
47 int cc_nkeys
; /* # of keys (1..4) */
48 int cc_key
[4]; /* AttrNumber of each key */
49 PGFunction cc_hashfunc
[4]; /* hash function to use for each key */
50 ScanKeyData cc_skey
[4]; /* precomputed key info for heap scans */
51 bool cc_isname
[4]; /* flag key columns that are NAMEs */
52 Dllist cc_lists
; /* list of CatCList structs */
54 long cc_searches
; /* total # searches against this cache */
55 long cc_hits
; /* # of matches against existing entry */
56 long cc_neg_hits
; /* # of matches against negative entry */
57 long cc_newloads
; /* # of successful loads of new entry */
60 * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of failed
61 * searches, each of which will result in loading a negative entry
63 long cc_invals
; /* # of entries invalidated from cache */
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 a Dllist that stores the elements
79 * of its hash bucket. We keep each Dllist in LRU order to speed repeated
82 Dlelem cache_elem
; /* list member of per-bucket list */
85 * The tuple may also be a member of at most one CatCList. (If a single
86 * catcache is list-searched with varying numbers of keys, we may have to
87 * make multiple entries for the same tuple because of this restriction.
88 * Currently, that's not expected to be common, so we accept the potential
91 struct catclist
*c_list
; /* containing CatCList, or NULL if none */
94 * A tuple marked "dead" must not be returned by subsequent searches.
95 * However, it won't be physically deleted from the cache until its
96 * refcount goes to zero. (If it's a member of a CatCList, the list's
97 * refcount must go to zero, too; also, remember to mark the list dead at
98 * the same time the tuple is marked.)
100 * A negative cache entry is an assertion that there is no tuple matching
101 * a particular key. This is just as useful as a normal entry so far as
102 * avoiding catalog searches is concerned. Management of positive and
103 * negative entries is identical.
105 int refcount
; /* number of active references */
106 bool dead
; /* dead but not yet removed? */
107 bool negative
; /* negative cache entry? */
108 uint32 hash_value
; /* hash value for this tuple's keys */
109 HeapTupleData tuple
; /* tuple management header */
113 typedef struct catclist
115 int cl_magic
; /* for identifying CatCList entries */
116 #define CL_MAGIC 0x52765103
117 CatCache
*my_cache
; /* link to owning catcache */
120 * A CatCList describes the result of a partial search, ie, a search using
121 * only the first K key columns of an N-key cache. We form the keys used
122 * into a tuple (with other attributes NULL) to represent the stored key
123 * set. The CatCList object contains links to cache entries for all the
124 * table rows satisfying the partial key. (Note: none of these will be
125 * negative cache entries.)
127 * A CatCList is only a member of a per-cache list; we do not currently
128 * divide them into hash buckets.
130 * A list marked "dead" must not be returned by subsequent searches.
131 * However, it won't be physically deleted from the cache until its
132 * refcount goes to zero. (A list should be marked dead if any of its
133 * member entries are dead.)
135 * If "ordered" is true then the member tuples appear in the order of the
136 * cache's underlying index. This will be true in normal operation, but
137 * might not be true during bootstrap or recovery operations. (namespace.c
138 * is able to save some cycles when it is true.)
140 Dlelem cache_elem
; /* list member of per-catcache list */
141 int refcount
; /* number of active references */
142 bool dead
; /* dead but not yet removed? */
143 bool ordered
; /* members listed in index order? */
144 short nkeys
; /* number of lookup keys specified */
145 uint32 hash_value
; /* hash value for lookup keys */
146 HeapTupleData tuple
; /* header for tuple holding keys */
147 int n_members
; /* number of member tuples */
148 CatCTup
*members
[1]; /* members --- VARIABLE LENGTH ARRAY */
149 } CatCList
; /* VARIABLE LENGTH STRUCT */
152 typedef struct catcacheheader
154 CatCache
*ch_caches
; /* head of list of CatCache structs */
155 int ch_ntup
; /* # of tuples in all caches */
159 /* this extern duplicates utils/memutils.h... */
160 extern PGDLLIMPORT MemoryContext CacheMemoryContext
;
162 extern void CreateCacheMemoryContext(void);
163 extern void AtEOXact_CatCache(bool isCommit
);
165 extern CatCache
*InitCatCache(int id
, Oid reloid
, Oid indexoid
,
167 int nkeys
, const int *key
,
169 extern void InitCatCachePhase2(CatCache
*cache
, bool touch_index
);
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 extern void PrintCatCacheLeakWarning(HeapTuple tuple
);
190 extern void PrintCatCacheListLeakWarning(CatCList
*list
);
192 #endif /* CATCACHE_H */