PostgreSQL Source Code  git master
attoptcache.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AttributeOpts
 

Typedefs

typedef struct AttributeOpts AttributeOpts
 

Functions

AttributeOptsget_attribute_options (Oid spcid, int attnum)
 

Typedef Documentation

◆ AttributeOpts

typedef struct AttributeOpts AttributeOpts

Function Documentation

◆ get_attribute_options()

AttributeOpts* get_attribute_options ( Oid  spcid,
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:2063
#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