PostgreSQL Source Code git master
Loading...
Searching...
No Matches
attoptcache.c File Reference
#include "postgres.h"
#include "access/reloptions.h"
#include "utils/attoptcache.h"
#include "utils/catcache.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/syscache.h"
#include "varatt.h"
Include dependency graph for attoptcache.c:

Go to the source code of this file.

Data Structures

struct  AttoptCacheKey
 
struct  AttoptCacheEntry
 

Functions

static void InvalidateAttoptCacheCallback (Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
 
static uint32 relatt_cache_syshash (const void *key, Size keysize)
 
static void InitializeAttoptCache (void)
 
AttributeOptsget_attribute_options (Oid attrelid, int attnum)
 

Variables

static HTABAttoptCacheHash = NULL
 

Function Documentation

◆ get_attribute_options()

AttributeOpts * get_attribute_options ( Oid  attrelid,
int  attnum 
)

Definition at line 132 of file attoptcache.c.

133{
136 AttributeOpts *result;
137 HeapTuple tp;
138
139 /* Find existing cache entry, if any. */
140 if (!AttoptCacheHash)
142 memset(&key, 0, sizeof(key)); /* make sure any padding bits are unset */
143 key.attrelid = attrelid;
144 key.attnum = attnum;
145 attopt =
147 &key,
148 HASH_FIND,
149 NULL);
150
151 /* Not found in Attopt cache. Construct new cache entry. */
152 if (!attopt)
153 {
155
157 ObjectIdGetDatum(attrelid),
159
160 /*
161 * If we don't find a valid HeapTuple, it must mean someone has
162 * managed to request attribute details for a non-existent attribute.
163 * We treat that case as if no options were specified.
164 */
165 if (!HeapTupleIsValid(tp))
166 opts = NULL;
167 else
168 {
169 Datum datum;
170 bool isNull;
171
172 datum = SysCacheGetAttr(ATTNUM,
173 tp,
175 &isNull);
176 if (isNull)
177 opts = NULL;
178 else
179 {
180 bytea *bytea_opts = attribute_reloptions(datum, false);
181
185 }
186 ReleaseSysCache(tp);
187 }
188
189 /*
190 * It's important to create the actual cache entry only after reading
191 * pg_attribute, since the read could cause a cache flush.
192 */
194 &key,
196 NULL);
197 attopt->opts = opts;
198 }
199
200 /* Return results in caller's memory context. */
201 if (attopt->opts == NULL)
202 return NULL;
203 result = palloc(VARSIZE(attopt->opts));
204 memcpy(result, attopt->opts, VARSIZE(attopt->opts));
205 return result;
206}
static HTAB * AttoptCacheHash
Definition attoptcache.c:29
static void InitializeAttoptCache(void)
Definition attoptcache.c:98
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:952
@ HASH_FIND
Definition hsearch.h:113
@ HASH_ENTER
Definition hsearch.h:114
#define HeapTupleIsValid(tuple)
Definition htup.h:78
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void * palloc(Size size)
Definition mcxt.c:1387
MemoryContext CacheMemoryContext
Definition mcxt.c:169
static AmcheckOptions opts
Definition pg_amcheck.c:112
int16 attnum
static Datum Int16GetDatum(int16 X)
Definition postgres.h:182
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition c.h:718
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache2(SysCacheIdentifier cacheId, Datum key1, Datum key2)
Definition syscache.c:230
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:595
static Size VARSIZE(const void *PTR)
Definition varatt.h:298

References attnum, AttoptCacheHash, attribute_reloptions(), CacheMemoryContext, fb(), HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, InitializeAttoptCache(), Int16GetDatum(), MemoryContextAlloc(), ObjectIdGetDatum(), opts, palloc(), ReleaseSysCache(), SearchSysCache2(), SysCacheGetAttr(), and VARSIZE().

Referenced by compute_expr_stats(), and do_analyze_rel().

◆ InitializeAttoptCache()

static void InitializeAttoptCache ( void  )
static

Definition at line 98 of file attoptcache.c.

99{
100 HASHCTL ctl;
101
102 /* Initialize the hash table. */
103 ctl.keysize = sizeof(AttoptCacheKey);
104 ctl.entrysize = sizeof(AttoptCacheEntry);
105
106 /*
107 * AttoptCacheEntry takes hash value from the system cache. For
108 * AttoptCacheHash we use the same hash in order to speedup search by hash
109 * value. This is used by hash_seq_init_with_hash_value().
110 */
112
114 hash_create("Attopt cache", 256, &ctl,
116
117 /* Make sure we've initialized CacheMemoryContext. */
120
121 /* Watch for invalidation events. */
124 (Datum) 0);
125}
static uint32 relatt_cache_syshash(const void *key, Size keysize)
Definition attoptcache.c:85
static void InvalidateAttoptCacheCallback(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
Definition attoptcache.c:53
void CreateCacheMemoryContext(void)
Definition catcache.c:715
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:358
#define HASH_ELEM
Definition hsearch.h:95
#define HASH_FUNCTION
Definition hsearch.h:98
void CacheRegisterSyscacheCallback(SysCacheIdentifier cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
tree ctl
Definition radixtree.h:1838
Size keysize
Definition hsearch.h:75

References AttoptCacheHash, CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), ctl, fb(), hash_create(), HASH_ELEM, HASH_FUNCTION, InvalidateAttoptCacheCallback(), HASHCTL::keysize, and relatt_cache_syshash().

Referenced by get_attribute_options().

◆ InvalidateAttoptCacheCallback()

static void InvalidateAttoptCacheCallback ( Datum  arg,
SysCacheIdentifier  cacheid,
uint32  hashvalue 
)
static

Definition at line 53 of file attoptcache.c.

55{
56 HASH_SEQ_STATUS status;
58
59 /*
60 * By convention, zero hash value is passed to the callback as a sign that
61 * it's time to invalidate the whole cache. See sinval.c, inval.c and
62 * InvalidateSystemCachesExtended().
63 */
64 if (hashvalue == 0)
66 else
68
69 while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
70 {
71 if (attopt->opts)
72 pfree(attopt->opts);
74 &attopt->key,
76 NULL) == NULL)
77 elog(ERROR, "hash table corrupted");
78 }
79}
void hash_seq_init_with_hash_value(HASH_SEQ_STATUS *status, HTAB *hashp, uint32 hashvalue)
Definition dynahash.c:1400
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1415
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1380
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
@ HASH_REMOVE
Definition hsearch.h:115
void pfree(void *pointer)
Definition mcxt.c:1616

References AttoptCacheHash, elog, ERROR, fb(), HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_init_with_hash_value(), hash_seq_search(), and pfree().

Referenced by InitializeAttoptCache().

◆ relatt_cache_syshash()

static uint32 relatt_cache_syshash ( const void key,
Size  keysize 
)
static

Definition at line 85 of file attoptcache.c.

86{
87 const AttoptCacheKey *ckey = key;
88
89 Assert(keysize == sizeof(*ckey));
91}
#define Assert(condition)
Definition c.h:885
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
#define GetSysCacheHashValue2(cacheId, key1, key2)
Definition syscache.h:120

References Assert, fb(), GetSysCacheHashValue2, Int32GetDatum(), and ObjectIdGetDatum().

Referenced by InitializeAttoptCache().

Variable Documentation

◆ AttoptCacheHash

HTAB* AttoptCacheHash = NULL
static