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 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

◆ get_attribute_options()

AttributeOpts* get_attribute_options ( Oid  attrelid,
int  attnum 
)

Definition at line 103 of file attoptcache.c.

References AttoptCacheKey::attnum, ATTNUM, attnum, AttoptCacheKey::attrelid, 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(), compute_index_stats(), and do_analyze_rel().

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

◆ InitializeAttoptCache()

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(), and HASHCTL::keysize.

Referenced by get_attribute_options().

78 {
79  HASHCTL ctl;
80 
81  /* Initialize the hash table. */
82  ctl.keysize = sizeof(AttoptCacheKey);
83  ctl.entrysize = sizeof(AttoptCacheEntry);
85  hash_create("Attopt cache", 256, &ctl,
87 
88  /* Make sure we've initialized CacheMemoryContext. */
89  if (!CacheMemoryContext)
91 
92  /* Watch for invalidation events. */
95  (Datum) 0);
96 }
#define HASH_ELEM
Definition: hsearch.h:95
Size entrysize
Definition: hsearch.h:76
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
static HTAB * AttoptCacheHash
Definition: attoptcache.c:28
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1435
uintptr_t Datum
Definition: postgres.h:411
Size keysize
Definition: hsearch.h:75
static void InvalidateAttoptCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: attoptcache.c:54
void CreateCacheMemoryContext(void)
Definition: catcache.c:620
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ InvalidateAttoptCacheCallback()

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, 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:954
AttoptCacheKey key
Definition: attoptcache.c:39
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
static HTAB * AttoptCacheHash
Definition: attoptcache.c:28
AttributeOpts * opts
Definition: attoptcache.c:40
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
#define elog(elevel,...)
Definition: elog.h:232
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

Variable Documentation

◆ AttoptCacheHash

HTAB* AttoptCacheHash = NULL
static

Definition at line 28 of file attoptcache.c.