Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
catcache.h
Go to the documentation of this file.
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-2025, 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
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 4
36
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 catcache
45{
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 for
52 * each key */
53 int cc_keyno[CATCACHE_MAXKEYS]; /* AttrNumber of each key */
54 int cc_nkeys; /* # of keys (1..CATCACHE_MAXKEYS) */
55 int cc_ntup; /* # of tuples currently in this cache */
56 int cc_nlist; /* # of CatCLists currently in this cache */
57 int cc_nlbuckets; /* # of CatCList hash buckets in this cache */
58 dlist_head *cc_lbucket; /* hash buckets for CatCLists */
59 const char *cc_relname; /* name of relation the tuples come from */
60 Oid cc_reloid; /* OID of relation the tuples come from */
61 Oid cc_indexoid; /* OID of index matching cache keys */
62 bool cc_relisshared; /* is relation shared across databases? */
63 slist_node cc_next; /* list link */
64 ScanKeyData cc_skey[CATCACHE_MAXKEYS]; /* precomputed key info for heap
65 * scans */
66
67 /*
68 * Keep these at the end, so that compiling catcache.c with CATCACHE_STATS
69 * doesn't break ABI for other modules
70 */
71#ifdef CATCACHE_STATS
72 long cc_searches; /* total # searches against this cache */
73 long cc_hits; /* # of matches against existing entry */
74 long cc_neg_hits; /* # of matches against negative entry */
75 long cc_newloads; /* # of successful loads of new entry */
76
77 /*
78 * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of failed
79 * searches, each of which will result in loading a negative entry
80 */
81 long cc_invals; /* # of entries invalidated from cache */
82 long cc_lsearches; /* total # list-searches */
83 long cc_lhits; /* # of matches against existing lists */
84#endif
86
87
88typedef struct catctup
89{
90 /*
91 * Each tuple in a cache is a member of a dlist that stores the elements
92 * of its hash bucket. We keep each dlist in LRU order to speed repeated
93 * lookups. Keep the dlist_node field first so that Valgrind understands
94 * the struct is reachable.
95 */
96 dlist_node cache_elem; /* list member of per-bucket list */
97
98 int ct_magic; /* for identifying CatCTup entries */
99#define CT_MAGIC 0x57261502
100
101 uint32 hash_value; /* hash value for this tuple's keys */
102
103 /*
104 * Lookup keys for the entry. By-reference datums point into the tuple for
105 * positive cache entries, and are separately allocated for negative ones.
106 */
108
109 /*
110 * A tuple marked "dead" must not be returned by subsequent searches.
111 * However, it won't be physically deleted from the cache until its
112 * refcount goes to zero. (If it's a member of a CatCList, the list's
113 * refcount must go to zero, too; also, remember to mark the list dead at
114 * the same time the tuple is marked.)
115 *
116 * A negative cache entry is an assertion that there is no tuple matching
117 * a particular key. This is just as useful as a normal entry so far as
118 * avoiding catalog searches is concerned. Management of positive and
119 * negative entries is identical.
120 */
121 int refcount; /* number of active references */
122 bool dead; /* dead but not yet removed? */
123 bool negative; /* negative cache entry? */
124 HeapTupleData tuple; /* tuple management header */
125
126 /*
127 * The tuple may also be a member of at most one CatCList. (If a single
128 * catcache is list-searched with varying numbers of keys, we may have to
129 * make multiple entries for the same tuple because of this restriction.
130 * Currently, that's not expected to be common, so we accept the potential
131 * inefficiency.)
132 */
133 struct catclist *c_list; /* containing CatCList, or NULL if none */
134
135 CatCache *my_cache; /* link to owning catcache */
136 /* properly aligned tuple data follows, unless a negative entry */
138
139
140/*
141 * A CatCList describes the result of a partial search, ie, a search using
142 * only the first K key columns of an N-key cache. We store the keys used
143 * into the keys attribute to represent the stored key set. The CatCList
144 * object contains links to cache entries for all the table rows satisfying
145 * the partial key. (Note: none of these will be negative cache entries.)
146 *
147 * A CatCList is only a member of a per-cache list; we do not currently
148 * divide them into hash buckets.
149 *
150 * A list marked "dead" must not be returned by subsequent searches.
151 * However, it won't be physically deleted from the cache until its
152 * refcount goes to zero. (A list should be marked dead if any of its
153 * member entries are dead.)
154 *
155 * If "ordered" is true then the member tuples appear in the order of the
156 * cache's underlying index. This will be true in normal operation, but
157 * might not be true during bootstrap or recovery operations. (namespace.c
158 * is able to save some cycles when it is true.)
159 */
160typedef struct catclist
161{
162 /*
163 * Keep the dlist_node field first so that Valgrind understands the struct
164 * is reachable.
165 */
166 dlist_node cache_elem; /* list member of per-catcache list */
167
168 int cl_magic; /* for identifying CatCList entries */
169#define CL_MAGIC 0x52765103
170
171 uint32 hash_value; /* hash value for lookup keys */
172
173 /*
174 * Lookup keys for the entry, with the first nkeys elements being valid.
175 * All by-reference are separately allocated.
176 */
178
179 int refcount; /* number of active references */
180 bool dead; /* dead but not yet removed? */
181 bool ordered; /* members listed in index order? */
182 short nkeys; /* number of lookup keys specified */
183 int n_members; /* number of member tuples */
184 CatCache *my_cache; /* link to owning catcache */
187
188
189typedef struct catcacheheader
190{
191 slist_head ch_caches; /* head of list of CatCache structs */
192 int ch_ntup; /* # of tuples in all caches */
194
195
196/* this extern duplicates utils/memutils.h... */
198
199extern void CreateCacheMemoryContext(void);
200
201extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,
202 int nkeys, const int *key,
203 int nbuckets);
204extern void InitCatCachePhase2(CatCache *cache, bool touch_index);
205
206extern HeapTuple SearchCatCache(CatCache *cache,
207 Datum v1, Datum v2, Datum v3, Datum v4);
209 Datum v1);
211 Datum v1, Datum v2);
213 Datum v1, Datum v2, Datum v3);
215 Datum v1, Datum v2, Datum v3, Datum v4);
216extern void ReleaseCatCache(HeapTuple tuple);
217
219 Datum v1, Datum v2,
220 Datum v3, Datum v4);
221
222extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
223 Datum v1, Datum v2,
224 Datum v3);
225extern void ReleaseCatCacheList(CatCList *list);
226
227extern void ResetCatalogCaches(void);
228extern void ResetCatalogCachesExt(bool debug_discard);
229extern void CatalogCacheFlushCatalog(Oid catId);
230extern void CatCacheInvalidate(CatCache *cache, uint32 hashValue);
231extern void PrepareToInvalidateCacheTuple(Relation relation,
232 HeapTuple tuple,
233 HeapTuple newtuple,
234 void (*function) (int, uint32, Oid, void *),
235 void *context);
236
237#endif /* CATCACHE_H */
#define PGDLLIMPORT
Definition: c.h:1320
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:471
uint32_t uint32
Definition: c.h:539
CatCache * InitCatCache(int id, Oid reloid, Oid indexoid, int nkeys, const int *key, int nbuckets)
Definition: catcache.c:879
HeapTuple SearchCatCache2(CatCache *cache, Datum v1, Datum v2)
Definition: catcache.c:1366
HeapTuple SearchCatCache3(CatCache *cache, Datum v1, Datum v2, Datum v3)
Definition: catcache.c:1374
void ReleaseCatCacheList(CatCList *list)
Definition: catcache.c:2094
void InitCatCachePhase2(CatCache *cache, bool touch_index)
Definition: catcache.c:1225
void ResetCatalogCaches(void)
Definition: catcache.c:799
uint32(* CCHashFN)(Datum datum)
Definition: catcache.h:39
CatCList * SearchCatCacheList(CatCache *cache, int nkeys, Datum v1, Datum v2, Datum v3)
Definition: catcache.c:1720
uint32 GetCatCacheHashValue(CatCache *cache, Datum v1, Datum v2, Datum v3, Datum v4)
Definition: catcache.c:1687
#define CATCACHE_MAXKEYS
Definition: catcache.h:35
HeapTuple SearchCatCache4(CatCache *cache, Datum v1, Datum v2, Datum v3, Datum v4)
Definition: catcache.c:1382
bool(* CCFastEqualFN)(Datum a, Datum b)
Definition: catcache.h:42
void PrepareToInvalidateCacheTuple(Relation relation, HeapTuple tuple, HeapTuple newtuple, void(*function)(int, uint32, Oid, void *), void *context)
Definition: catcache.c:2377
struct catcache CatCache
struct catctup CatCTup
void CreateCacheMemoryContext(void)
Definition: catcache.c:709
struct catclist CatCList
PGDLLIMPORT MemoryContext CacheMemoryContext
Definition: mcxt.c:169
void CatCacheInvalidate(CatCache *cache, uint32 hashValue)
Definition: catcache.c:626
HeapTuple SearchCatCache1(CatCache *cache, Datum v1)
Definition: catcache.c:1358
void CatalogCacheFlushCatalog(Oid catId)
Definition: catcache.c:835
struct catcacheheader CatCacheHeader
void ReleaseCatCache(HeapTuple tuple)
Definition: catcache.c:1648
HeapTuple SearchCatCache(CatCache *cache, Datum v1, Datum v2, Datum v3, Datum v4)
Definition: catcache.c:1341
void ResetCatalogCachesExt(bool debug_discard)
Definition: catcache.c:805
int b
Definition: isn.c:74
int a
Definition: isn.c:73
on_exit_nicely_callback function
uint64_t Datum
Definition: postgres.h:70
unsigned int Oid
Definition: postgres_ext.h:32
const char * cc_relname
Definition: catcache.h:59
CCHashFN cc_hashfunc[CATCACHE_MAXKEYS]
Definition: catcache.h:50
dlist_head * cc_bucket
Definition: catcache.h:49
slist_node cc_next
Definition: catcache.h:63
Oid cc_reloid
Definition: catcache.h:60
int cc_nkeys
Definition: catcache.h:54
int cc_keyno[CATCACHE_MAXKEYS]
Definition: catcache.h:53
CCFastEqualFN cc_fastequal[CATCACHE_MAXKEYS]
Definition: catcache.h:51
Oid cc_indexoid
Definition: catcache.h:61
int cc_nbuckets
Definition: catcache.h:47
bool cc_relisshared
Definition: catcache.h:62
int cc_ntup
Definition: catcache.h:55
ScanKeyData cc_skey[CATCACHE_MAXKEYS]
Definition: catcache.h:64
int cc_nlist
Definition: catcache.h:56
int id
Definition: catcache.h:46
TupleDesc cc_tupdesc
Definition: catcache.h:48
int cc_nlbuckets
Definition: catcache.h:57
dlist_head * cc_lbucket
Definition: catcache.h:58
slist_head ch_caches
Definition: catcache.h:191
dlist_node cache_elem
Definition: catcache.h:166
int refcount
Definition: catcache.h:179
CatCache * my_cache
Definition: catcache.h:184
int cl_magic
Definition: catcache.h:168
bool dead
Definition: catcache.h:180
short nkeys
Definition: catcache.h:182
Datum keys[CATCACHE_MAXKEYS]
Definition: catcache.h:177
bool ordered
Definition: catcache.h:181
CatCTup * members[FLEXIBLE_ARRAY_MEMBER]
Definition: catcache.h:185
uint32 hash_value
Definition: catcache.h:171
int n_members
Definition: catcache.h:183
int ct_magic
Definition: catcache.h:98
int refcount
Definition: catcache.h:121
bool negative
Definition: catcache.h:123
dlist_node cache_elem
Definition: catcache.h:96
HeapTupleData tuple
Definition: catcache.h:124
CatCache * my_cache
Definition: catcache.h:135
struct catclist * c_list
Definition: catcache.h:133
Datum keys[CATCACHE_MAXKEYS]
Definition: catcache.h:107
bool dead
Definition: catcache.h:122
uint32 hash_value
Definition: catcache.h:101