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 attrelid, int attnum)
 

Typedef Documentation

◆ AttributeOpts

typedef struct AttributeOpts AttributeOpts

Function Documentation

◆ get_attribute_options()

AttributeOpts* get_attribute_options ( Oid  attrelid,
int  attnum 
)

Definition at line 104 of file attoptcache.c.

105 {
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 unset */
115  key.attrelid = attrelid;
116  key.attnum = attnum;
117  attopt =
119  &key,
120  HASH_FIND,
121  NULL);
122 
123  /* Not found in Attopt cache. Construct new cache entry. */
124  if (!attopt)
125  {
127 
128  tp = SearchSysCache2(ATTNUM,
129  ObjectIdGetDatum(attrelid),
131 
132  /*
133  * If we don't find a valid HeapTuple, it must mean someone has
134  * managed to request attribute details for a non-existent attribute.
135  * We treat that case as if no options were specified.
136  */
137  if (!HeapTupleIsValid(tp))
138  opts = NULL;
139  else
140  {
141  Datum datum;
142  bool isNull;
143 
144  datum = SysCacheGetAttr(ATTNUM,
145  tp,
146  Anum_pg_attribute_attoptions,
147  &isNull);
148  if (isNull)
149  opts = NULL;
150  else
151  {
152  bytea *bytea_opts = attribute_reloptions(datum, false);
153 
155  VARSIZE(bytea_opts));
156  memcpy(opts, bytea_opts, VARSIZE(bytea_opts));
157  }
158  ReleaseSysCache(tp);
159  }
160 
161  /*
162  * It's important to create the actual cache entry only after reading
163  * pg_attribute, since the read could cause a cache flush.
164  */
166  &key,
167  HASH_ENTER,
168  NULL);
169  attopt->opts = opts;
170  }
171 
172  /* Return results in caller's memory context. */
173  if (attopt->opts == NULL)
174  return NULL;
175  result = palloc(VARSIZE(attopt->opts));
176  memcpy(result, attopt->opts, VARSIZE(attopt->opts));
177  return result;
178 }
static HTAB * AttoptCacheHash
Definition: attoptcache.c:29
static void InitializeAttoptCache(void)
Definition: attoptcache.c:78
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:1168
MemoryContext CacheMemoryContext
Definition: mcxt.c:140
void * palloc(Size size)
Definition: mcxt.c:1304
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:674
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:479
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:229
#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().