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-2005, PostgreSQL Global Development Group
14
* Portions Copyright (c) 1994, Regents of the University of California
16
* $PostgreSQL: pgsql/src/include/utils/catcache.h,v 1.52 2004-12-31 22:03:45 pgsql Exp $
18
*-------------------------------------------------------------------------
23
#include "access/htup.h"
24
#include "access/skey.h"
25
#include "lib/dllist.h"
28
* struct catctup: individual tuple in the cache.
29
* struct catclist: list of tuples matching a partial key.
30
* struct catcache: information for managing a cache.
31
* struct catcacheheader: information for managing all the caches.
34
typedef struct catcache
36
int id; /* cache identifier --- see syscache.h */
37
struct catcache *cc_next; /* link to next catcache */
38
const char *cc_relname; /* name of relation the tuples come from */
39
const char *cc_indname; /* name of index matching cache keys */
40
Oid cc_reloid; /* OID of relation the tuples come from */
41
bool cc_relisshared; /* is relation shared across databases? */
42
TupleDesc cc_tupdesc; /* tuple descriptor (copied from reldesc) */
43
int cc_reloidattr; /* AttrNumber of relation OID attr, or 0 */
44
int cc_ntup; /* # of tuples currently in this cache */
45
int cc_nbuckets; /* # of hash buckets in this cache */
46
int cc_nkeys; /* # of keys (1..4) */
47
int cc_key[4]; /* AttrNumber of each key */
48
PGFunction cc_hashfunc[4]; /* hash function to use for each key */
49
ScanKeyData cc_skey[4]; /* precomputed key info for heap scans */
50
bool cc_isname[4]; /* flag key columns that are NAMEs */
51
Dllist cc_lists; /* list of CatCList structs */
53
long cc_searches; /* total # searches against this cache */
54
long cc_hits; /* # of matches against existing entry */
55
long cc_neg_hits; /* # of matches against negative entry */
56
long cc_newloads; /* # of successful loads of new entry */
59
* cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of
60
* failed searches, each of which will result in loading a negative
63
long cc_invals; /* # of entries invalidated from cache */
64
long cc_discards; /* # of entries discarded due to overflow */
65
long cc_lsearches; /* total # list-searches */
66
long cc_lhits; /* # of matches against existing lists */
68
Dllist cc_bucket[1]; /* hash buckets --- VARIABLE LENGTH ARRAY */
69
} CatCache; /* VARIABLE LENGTH STRUCT */
72
typedef struct catctup
74
int ct_magic; /* for identifying CatCTup entries */
75
#define CT_MAGIC 0x57261502
76
CatCache *my_cache; /* link to owning catcache */
79
* Each tuple in a cache is a member of two Dllists: one lists all the
80
* elements in all the caches in LRU order, and the other lists just
81
* the elements in one hashbucket of one cache, also in LRU order.
83
* The tuple may also be a member of at most one CatCList. (If a single
84
* catcache is list-searched with varying numbers of keys, we may have
85
* to make multiple entries for the same tuple because of this
86
* restriction. Currently, that's not expected to be common, so we
87
* accept the potential inefficiency.)
89
Dlelem lrulist_elem; /* list member of global LRU list */
90
Dlelem cache_elem; /* list member of per-bucket list */
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.
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
100
* as avoiding catalog searches is concerned. Management of positive
101
* and negative entries is identical.
103
int refcount; /* number of active references */
104
bool dead; /* dead but not yet removed? */
105
bool negative; /* negative cache entry? */
106
uint32 hash_value; /* hash value for this tuple's keys */
107
HeapTupleData tuple; /* tuple management header */
111
typedef struct catclist
113
int cl_magic; /* for identifying CatCList entries */
114
#define CL_MAGIC 0x52765103
115
CatCache *my_cache; /* link to owning catcache */
118
* A CatCList describes the result of a partial search, ie, a search
119
* using only the first K key columns of an N-key cache. We form the
120
* keys used into a tuple (with other attributes NULL) to represent
121
* the stored key set. The CatCList object contains links to cache
122
* entries for all the table rows satisfying the partial key. (Note:
123
* none of these will be negative cache entries.)
125
* A CatCList is only a member of a per-cache list; we do not do separate
126
* LRU management for CatCLists. Instead, a CatCList is dropped from
127
* the cache as soon as any one of its member tuples ages out due to
128
* tuple-level LRU management.
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. (Its member tuples must have refcounts at
133
* least as large, so they won't go away either.)
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,
137
* but might not be true during bootstrap or recovery operations.
138
* (namespace.c 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 */
156
int ch_maxtup; /* max # of tuples allowed (LRU) */
157
Dllist ch_lrulist; /* overall LRU list, most recent first */
161
/* this extern duplicates utils/memutils.h... */
162
extern DLLIMPORT MemoryContext CacheMemoryContext;
164
extern void CreateCacheMemoryContext(void);
165
extern void AtEOXact_CatCache(bool isCommit);
167
extern CatCache *InitCatCache(int id, const char *relname, const char *indname,
169
int nkeys, const int *key);
170
extern void InitCatCachePhase2(CatCache *cache);
172
extern HeapTuple SearchCatCache(CatCache *cache,
175
extern void ReleaseCatCache(HeapTuple tuple);
177
extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
180
extern void ReleaseCatCacheList(CatCList *list);
182
extern void ResetCatalogCaches(void);
183
extern void CatalogCacheFlushRelation(Oid relId);
184
extern void CatalogCacheIdInvalidate(int cacheId, uint32 hashValue,
185
ItemPointer pointer);
186
extern void PrepareToInvalidateCacheTuple(Relation relation,
188
void (*function) (int, uint32, ItemPointer, Oid));
190
#endif /* CATCACHE_H */