PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 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 void InitializeAttoptCache (void)
 
AttributeOptsget_attribute_options (Oid attrelid, int attnum)
 

Variables

static HTABAttoptCacheHash = NULL
 

Function Documentation

AttributeOpts* get_attribute_options ( Oid  attrelid,
int  attnum 
)

Definition at line 104 of file attoptcache.c.

References Anum_pg_attribute_attoptions, AttoptCacheKey::attnum, ATTNUM, AttoptCacheKey::attrelid, attribute_reloptions(), CacheMemoryContext, HASH_ENTER, HASH_FIND, hash_search(), HeapTupleIsValid, InitializeAttoptCache(), Int16GetDatum, MemoryContextAlloc(), NULL, ObjectIdGetDatum, AttoptCacheEntry::opts, palloc(), ReleaseSysCache(), SearchSysCache2, SysCacheGetAttr(), and VARSIZE.

Referenced by compute_index_stats(), and do_analyze_rel().

105 {
106  AttoptCacheKey key;
107  AttoptCacheEntry *attopt;
108  AttributeOpts *result;
109  HeapTuple tp;
110 
111  /* Find existing cache entry, if any. */
112  if (!AttoptCacheHash)
114  memset(&key, 0, sizeof(key)); /* make sure any padding bits are
115  * unset */
116  key.attrelid = attrelid;
117  key.attnum = attnum;
118  attopt =
120  (void *) &key,
121  HASH_FIND,
122  NULL);
123 
124  /* Not found in Attopt cache. Construct new cache entry. */
125  if (!attopt)
126  {
127  AttributeOpts *opts;
128 
129  tp = SearchSysCache2(ATTNUM,
130  ObjectIdGetDatum(attrelid),
131  Int16GetDatum(attnum));
132 
133  /*
134  * If we don't find a valid HeapTuple, it must mean someone has
135  * managed to request attribute details for a non-existent attribute.
136  * We treat that case as if no options were specified.
137  */
138  if (!HeapTupleIsValid(tp))
139  opts = NULL;
140  else
141  {
142  Datum datum;
143  bool isNull;
144 
145  datum = SysCacheGetAttr(ATTNUM,
146  tp,
148  &isNull);
149  if (isNull)
150  opts = NULL;
151  else
152  {
153  bytea *bytea_opts = attribute_reloptions(datum, false);
154 
156  VARSIZE(bytea_opts));
157  memcpy(opts, bytea_opts, VARSIZE(bytea_opts));
158  }
159  ReleaseSysCache(tp);
160  }
161 
162  /*
163  * It's important to create the actual cache entry only after reading
164  * pg_attribute, since the read could cause a cache flush.
165  */
167  (void *) &key,
168  HASH_ENTER,
169  NULL);
170  attopt->opts = opts;
171  }
172 
173  /* Return results in caller's memory context. */
174  if (attopt->opts == NULL)
175  return NULL;
176  result = palloc(VARSIZE(attopt->opts));
177  memcpy(result, attopt->opts, VARSIZE(attopt->opts));
178  return result;
179 }
#define VARSIZE(PTR)
Definition: postgres.h:304
#define Int16GetDatum(X)
Definition: postgres.h:457
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:1453
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
static HTAB * AttoptCacheHash
Definition: attoptcache.c:28
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1093
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1255
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
AttributeOpts * opts
Definition: attoptcache.c:40
#define Anum_pg_attribute_attoptions
Definition: pg_attribute.h:211
void * palloc(Size size)
Definition: mcxt.c:849
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
static void InitializeAttoptCache(void)
Definition: attoptcache.c:77
Definition: c.h:439
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
#define SearchSysCache2(cacheId, key1, key2)
Definition: syscache.h:152
static void InitializeAttoptCache ( void  )
static

Definition at line 77 of file attoptcache.c.

References ATTNUM, CacheMemoryContext, CacheRegisterSyscacheCallback(), CreateCacheMemoryContext(), HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, InvalidateAttoptCacheCallback(), HASHCTL::keysize, and MemSet.

Referenced by get_attribute_options().

78 {
79  HASHCTL ctl;
80 
81  /* Initialize the hash table. */
82  MemSet(&ctl, 0, sizeof(ctl));
83  ctl.keysize = sizeof(AttoptCacheKey);
84  ctl.entrysize = sizeof(AttoptCacheEntry);
86  hash_create("Attopt cache", 256, &ctl,
88 
89  /* Make sure we've initialized CacheMemoryContext. */
90  if (!CacheMemoryContext)
92 
93  /* Watch for invalidation events. */
96  (Datum) 0);
97 }
#define HASH_ELEM
Definition: hsearch.h:87
Size entrysize
Definition: hsearch.h:73
#define MemSet(start, val, len)
Definition: c.h:857
static HTAB * AttoptCacheHash
Definition: attoptcache.c:28
#define HASH_BLOBS
Definition: hsearch.h:88
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1381
uintptr_t Datum
Definition: postgres.h:372
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
static void InvalidateAttoptCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: attoptcache.c:54
void CreateCacheMemoryContext(void)
Definition: catcache.c:525
MemoryContext CacheMemoryContext
Definition: mcxt.c:46
static void InvalidateAttoptCacheCallback ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 54 of file attoptcache.c.

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

Referenced by InitializeAttoptCache().

55 {
57  AttoptCacheEntry *attopt;
58 
60  while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
61  {
62  if (attopt->opts)
63  pfree(attopt->opts);
65  (void *) &attopt->key,
67  NULL) == NULL)
68  elog(ERROR, "hash table corrupted");
69  }
70 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
AttoptCacheKey key
Definition: attoptcache.c:39
void pfree(void *pointer)
Definition: mcxt.c:950
#define ERROR
Definition: elog.h:43
static HTAB * AttoptCacheHash
Definition: attoptcache.c:28
#define NULL
Definition: c.h:229
AttributeOpts * opts
Definition: attoptcache.c:40
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
#define elog
Definition: elog.h:219
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224

Variable Documentation

HTAB* AttoptCacheHash = NULL
static

Definition at line 28 of file attoptcache.c.