Header And Logo

PostgreSQL
| The world's most advanced open source database.

catcache.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * catcache.h
00004  *    Low-level catalog cache definitions.
00005  *
00006  * NOTE: every catalog cache must have a corresponding unique index on
00007  * the system table that it caches --- ie, the index must match the keys
00008  * used to do lookups in this cache.  All cache fetches are done with
00009  * indexscans (under normal conditions).  The index should be unique to
00010  * guarantee that there can only be one matching row for a key combination.
00011  *
00012  *
00013  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00014  * Portions Copyright (c) 1994, Regents of the University of California
00015  *
00016  * src/include/utils/catcache.h
00017  *
00018  *-------------------------------------------------------------------------
00019  */
00020 #ifndef CATCACHE_H
00021 #define CATCACHE_H
00022 
00023 #include "access/htup.h"
00024 #include "access/skey.h"
00025 #include "lib/ilist.h"
00026 #include "utils/relcache.h"
00027 
00028 /*
00029  *      struct catctup:         individual tuple in the cache.
00030  *      struct catclist:        list of tuples matching a partial key.
00031  *      struct catcache:        information for managing a cache.
00032  *      struct catcacheheader:  information for managing all the caches.
00033  */
00034 
00035 #define CATCACHE_MAXKEYS        4
00036 
00037 typedef struct catcache
00038 {
00039     int         id;             /* cache identifier --- see syscache.h */
00040     slist_node  cc_next;        /* list link */
00041     const char *cc_relname;     /* name of relation the tuples come from */
00042     Oid         cc_reloid;      /* OID of relation the tuples come from */
00043     Oid         cc_indexoid;    /* OID of index matching cache keys */
00044     bool        cc_relisshared; /* is relation shared across databases? */
00045     TupleDesc   cc_tupdesc;     /* tuple descriptor (copied from reldesc) */
00046     int         cc_ntup;        /* # of tuples currently in this cache */
00047     int         cc_nbuckets;    /* # of hash buckets in this cache */
00048     int         cc_nkeys;       /* # of keys (1..CATCACHE_MAXKEYS) */
00049     int         cc_key[CATCACHE_MAXKEYS];       /* AttrNumber of each key */
00050     PGFunction  cc_hashfunc[CATCACHE_MAXKEYS];  /* hash function for each key */
00051     ScanKeyData cc_skey[CATCACHE_MAXKEYS];      /* precomputed key info for
00052                                                  * heap scans */
00053     bool        cc_isname[CATCACHE_MAXKEYS];    /* flag "name" key columns */
00054     dlist_head  cc_lists;       /* list of CatCList structs */
00055 #ifdef CATCACHE_STATS
00056     long        cc_searches;    /* total # searches against this cache */
00057     long        cc_hits;        /* # of matches against existing entry */
00058     long        cc_neg_hits;    /* # of matches against negative entry */
00059     long        cc_newloads;    /* # of successful loads of new entry */
00060 
00061     /*
00062      * cc_searches - (cc_hits + cc_neg_hits + cc_newloads) is number of failed
00063      * searches, each of which will result in loading a negative entry
00064      */
00065     long        cc_invals;      /* # of entries invalidated from cache */
00066     long        cc_lsearches;   /* total # list-searches */
00067     long        cc_lhits;       /* # of matches against existing lists */
00068 #endif
00069     dlist_head  cc_bucket[1];   /* hash buckets --- VARIABLE LENGTH ARRAY */
00070 } CatCache;                     /* VARIABLE LENGTH STRUCT */
00071 
00072 
00073 typedef struct catctup
00074 {
00075     int         ct_magic;       /* for identifying CatCTup entries */
00076 #define CT_MAGIC   0x57261502
00077     CatCache   *my_cache;       /* link to owning catcache */
00078 
00079     /*
00080      * Each tuple in a cache is a member of a dlist that stores the elements
00081      * of its hash bucket.  We keep each dlist in LRU order to speed repeated
00082      * lookups.
00083      */
00084     dlist_node  cache_elem;     /* list member of per-bucket list */
00085 
00086     /*
00087      * The tuple may also be a member of at most one CatCList.  (If a single
00088      * catcache is list-searched with varying numbers of keys, we may have to
00089      * make multiple entries for the same tuple because of this restriction.
00090      * Currently, that's not expected to be common, so we accept the potential
00091      * inefficiency.)
00092      */
00093     struct catclist *c_list;    /* containing CatCList, or NULL if none */
00094 
00095     /*
00096      * A tuple marked "dead" must not be returned by subsequent searches.
00097      * However, it won't be physically deleted from the cache until its
00098      * refcount goes to zero.  (If it's a member of a CatCList, the list's
00099      * refcount must go to zero, too; also, remember to mark the list dead at
00100      * the same time the tuple is marked.)
00101      *
00102      * A negative cache entry is an assertion that there is no tuple matching
00103      * a particular key.  This is just as useful as a normal entry so far as
00104      * avoiding catalog searches is concerned.  Management of positive and
00105      * negative entries is identical.
00106      */
00107     int         refcount;       /* number of active references */
00108     bool        dead;           /* dead but not yet removed? */
00109     bool        negative;       /* negative cache entry? */
00110     uint32      hash_value;     /* hash value for this tuple's keys */
00111     HeapTupleData tuple;        /* tuple management header */
00112 } CatCTup;
00113 
00114 
00115 typedef struct catclist
00116 {
00117     int         cl_magic;       /* for identifying CatCList entries */
00118 #define CL_MAGIC   0x52765103
00119     CatCache   *my_cache;       /* link to owning catcache */
00120 
00121     /*
00122      * A CatCList describes the result of a partial search, ie, a search using
00123      * only the first K key columns of an N-key cache.  We form the keys used
00124      * into a tuple (with other attributes NULL) to represent the stored key
00125      * set.  The CatCList object contains links to cache entries for all the
00126      * table rows satisfying the partial key.  (Note: none of these will be
00127      * negative cache entries.)
00128      *
00129      * A CatCList is only a member of a per-cache list; we do not currently
00130      * divide them into hash buckets.
00131      *
00132      * A list marked "dead" must not be returned by subsequent searches.
00133      * However, it won't be physically deleted from the cache until its
00134      * refcount goes to zero.  (A list should be marked dead if any of its
00135      * member entries are dead.)
00136      *
00137      * If "ordered" is true then the member tuples appear in the order of the
00138      * cache's underlying index.  This will be true in normal operation, but
00139      * might not be true during bootstrap or recovery operations. (namespace.c
00140      * is able to save some cycles when it is true.)
00141      */
00142     dlist_node  cache_elem;     /* list member of per-catcache list */
00143     int         refcount;       /* number of active references */
00144     bool        dead;           /* dead but not yet removed? */
00145     bool        ordered;        /* members listed in index order? */
00146     short       nkeys;          /* number of lookup keys specified */
00147     uint32      hash_value;     /* hash value for lookup keys */
00148     HeapTupleData tuple;        /* header for tuple holding keys */
00149     int         n_members;      /* number of member tuples */
00150     CatCTup    *members[1];     /* members --- VARIABLE LENGTH ARRAY */
00151 } CatCList;                     /* VARIABLE LENGTH STRUCT */
00152 
00153 
00154 typedef struct catcacheheader
00155 {
00156     slist_head  ch_caches;      /* head of list of CatCache structs */
00157     int         ch_ntup;        /* # of tuples in all caches */
00158 } CatCacheHeader;
00159 
00160 
00161 /* this extern duplicates utils/memutils.h... */
00162 extern PGDLLIMPORT MemoryContext CacheMemoryContext;
00163 
00164 extern void CreateCacheMemoryContext(void);
00165 extern void AtEOXact_CatCache(bool isCommit);
00166 
00167 extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,
00168              int nkeys, const int *key,
00169              int nbuckets);
00170 extern void InitCatCachePhase2(CatCache *cache, bool touch_index);
00171 
00172 extern HeapTuple SearchCatCache(CatCache *cache,
00173                Datum v1, Datum v2,
00174                Datum v3, Datum v4);
00175 extern void ReleaseCatCache(HeapTuple tuple);
00176 
00177 extern uint32 GetCatCacheHashValue(CatCache *cache,
00178                      Datum v1, Datum v2,
00179                      Datum v3, Datum v4);
00180 
00181 extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
00182                    Datum v1, Datum v2,
00183                    Datum v3, Datum v4);
00184 extern void ReleaseCatCacheList(CatCList *list);
00185 
00186 extern void ResetCatalogCaches(void);
00187 extern void CatalogCacheFlushCatalog(Oid catId);
00188 extern void CatalogCacheIdInvalidate(int cacheId, uint32 hashValue);
00189 extern void PrepareToInvalidateCacheTuple(Relation relation,
00190                               HeapTuple tuple,
00191                               HeapTuple newtuple,
00192                               void (*function) (int, uint32, Oid));
00193 
00194 extern void PrintCatCacheLeakWarning(HeapTuple tuple);
00195 extern void PrintCatCacheListLeakWarning(CatCList *list);
00196 
00197 #endif   /* CATCACHE_H */