PostgreSQL Source Code  git master
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, int 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 131 of file attoptcache.c.

132 {
134  AttoptCacheEntry *attopt;
135  AttributeOpts *result;
136  HeapTuple tp;
137 
138  /* Find existing cache entry, if any. */
139  if (!AttoptCacheHash)
141  memset(&key, 0, sizeof(key)); /* make sure any padding bits are unset */
142  key.attrelid = attrelid;
143  key.attnum = attnum;
144  attopt =
146  &key,
147  HASH_FIND,
148  NULL);
149 
150  /* Not found in Attopt cache. Construct new cache entry. */
151  if (!attopt)
152  {
154 
155  tp = SearchSysCache2(ATTNUM,
156  ObjectIdGetDatum(attrelid),
158 
159  /*
160  * If we don't find a valid HeapTuple, it must mean someone has
161  * managed to request attribute details for a non-existent attribute.
162  * We treat that case as if no options were specified.
163  */
164  if (!HeapTupleIsValid(tp))
165  opts = NULL;
166  else
167  {
168  Datum datum;
169  bool isNull;
170 
171  datum = SysCacheGetAttr(ATTNUM,
172  tp,
173  Anum_pg_attribute_attoptions,
174  &isNull);
175  if (isNull)
176  opts = NULL;
177  else
178  {
179  bytea *bytea_opts = attribute_reloptions(datum, false);
180 
182  VARSIZE(bytea_opts));
183  memcpy(opts, bytea_opts, VARSIZE(bytea_opts));
184  }
185  ReleaseSysCache(tp);
186  }
187 
188  /*
189  * It's important to create the actual cache entry only after reading
190  * pg_attribute, since the read could cause a cache flush.
191  */
193  &key,
194  HASH_ENTER,
195  NULL);
196  attopt->opts = opts;
197  }
198 
199  /* Return results in caller's memory context. */
200  if (attopt->opts == NULL)
201  return NULL;
202  result = palloc(VARSIZE(attopt->opts));
203  memcpy(result, attopt->opts, VARSIZE(attopt->opts));
204  return result;
205 }
static HTAB * AttoptCacheHash
Definition: attoptcache.c:29
static void InitializeAttoptCache(void)
Definition: attoptcache.c:97
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
@ 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:1181
MemoryContext CacheMemoryContext
Definition: mcxt.c:152
void * palloc(Size size)
Definition: mcxt.c:1317
static AmcheckOptions opts
Definition: pg_amcheck.c:111
int16 attnum
Definition: pg_attribute.h:74
uintptr_t Datum
Definition: postgres.h:64
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:2069
AttributeOpts * opts
Definition: attoptcache.c:41
Definition: c.h:690
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:596
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:232
#define VARSIZE(PTR)
Definition: varatt.h:279

References attnum, AttoptCacheHash, attribute_reloptions(), CacheMemoryContext, HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, InitializeAttoptCache(), Int16GetDatum(), sort-test::key, MemoryContextAlloc(), ObjectIdGetDatum(), AttoptCacheEntry::opts, 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 97 of file attoptcache.c.

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

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

Referenced by get_attribute_options().

◆ InvalidateAttoptCacheCallback()

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

Definition at line 53 of file attoptcache.c.

54 {
55  HASH_SEQ_STATUS status;
56  AttoptCacheEntry *attopt;
57 
58  /*
59  * By convention, zero hash value is passed to the callback as a sign that
60  * it's time to invalidate the whole cache. See sinval.c, inval.c and
61  * InvalidateSystemCachesExtended().
62  */
63  if (hashvalue == 0)
65  else
67 
68  while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
69  {
70  if (attopt->opts)
71  pfree(attopt->opts);
73  &attopt->key,
75  NULL) == NULL)
76  elog(ERROR, "hash table corrupted");
77  }
78 }
void hash_seq_init_with_hash_value(HASH_SEQ_STATUS *status, HTAB *hashp, uint32 hashvalue)
Definition: dynahash.c:1405
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1420
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
@ HASH_REMOVE
Definition: hsearch.h:115
void pfree(void *pointer)
Definition: mcxt.c:1521
AttoptCacheKey key
Definition: attoptcache.c:40

References AttoptCacheHash, elog, ERROR, HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_init_with_hash_value(), hash_seq_search(), AttoptCacheEntry::key, AttoptCacheEntry::opts, and pfree().

Referenced by InitializeAttoptCache().

◆ relatt_cache_syshash()

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

Definition at line 84 of file attoptcache.c.

85 {
86  const AttoptCacheKey *ckey = key;
87 
88  Assert(keysize == sizeof(*ckey));
89  return GetSysCacheHashValue2(ATTNUM, ckey->attrelid, ckey->attnum);
90 }
#define Assert(condition)
Definition: c.h:861
#define GetSysCacheHashValue2(cacheId, key1, key2)
Definition: syscache.h:120

References Assert, AttoptCacheKey::attnum, AttoptCacheKey::attrelid, GetSysCacheHashValue2, and sort-test::key.

Referenced by InitializeAttoptCache().

Variable Documentation

◆ AttoptCacheHash

HTAB* AttoptCacheHash = NULL
static