PostgreSQL Source Code  git master
attoptcache.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * attoptcache.c
4  * Attribute options cache management.
5  *
6  * Attribute options are cached separately from the fixed-size portion of
7  * pg_attribute entries, which are handled by the relcache.
8  *
9  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * IDENTIFICATION
13  * src/backend/utils/cache/attoptcache.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 #include "postgres.h"
18 
19 #include "access/reloptions.h"
20 #include "utils/attoptcache.h"
21 #include "utils/catcache.h"
22 #include "utils/hsearch.h"
23 #include "utils/inval.h"
24 #include "utils/syscache.h"
25 
26 
27 /* Hash table for information about each attribute's options */
28 static HTAB *AttoptCacheHash = NULL;
29 
30 /* attrelid and attnum form the lookup key, and must appear first */
31 typedef struct
32 {
34  int attnum;
36 
37 typedef struct
38 {
39  AttoptCacheKey key; /* lookup key - must be first */
40  AttributeOpts *opts; /* options, or NULL if none */
42 
43 
44 /*
45  * InvalidateAttoptCacheCallback
46  * Flush all cache entries when pg_attribute is updated.
47  *
48  * When pg_attribute is updated, we must flush the cache entry at least
49  * for that attribute. Currently, we just flush them all. Since attribute
50  * options are not currently used in performance-critical paths (such as
51  * query execution), this seems OK.
52  */
53 static void
55 {
57  AttoptCacheEntry *attopt;
58 
59  hash_seq_init(&status, AttoptCacheHash);
60  while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
61  {
62  if (attopt->opts)
63  pfree(attopt->opts);
64  if (hash_search(AttoptCacheHash,
65  (void *) &attopt->key,
67  NULL) == NULL)
68  elog(ERROR, "hash table corrupted");
69  }
70 }
71 
72 /*
73  * InitializeAttoptCache
74  * Initialize the attribute options cache.
75  */
76 static void
78 {
79  HASHCTL ctl;
80 
81  /* Initialize the hash table. */
82  ctl.keysize = sizeof(AttoptCacheKey);
83  ctl.entrysize = sizeof(AttoptCacheEntry);
84  AttoptCacheHash =
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 }
97 
98 /*
99  * get_attribute_options
100  * Fetch attribute options for a specified table OID.
101  */
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 =
117  (AttoptCacheEntry *) hash_search(AttoptCacheHash,
118  (void *) &key,
119  HASH_FIND,
120  NULL);
121 
122  /* Not found in Attopt cache. Construct new cache entry. */
123  if (!attopt)
124  {
125  AttributeOpts *opts;
126 
127  tp = SearchSysCache2(ATTNUM,
128  ObjectIdGetDatum(attrelid),
129  Int16GetDatum(attnum));
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  */
164  attopt = (AttoptCacheEntry *) hash_search(AttoptCacheHash,
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 }
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
Definition: attoptcache.c:103
#define HASH_ELEM
Definition: hsearch.h:95
#define VARSIZE(PTR)
Definition: postgres.h:303
#define Int16GetDatum(X)
Definition: postgres.h:451
Size entrysize
Definition: hsearch.h:76
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
unsigned int Oid
Definition: postgres_ext.h:31
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:2046
AttoptCacheKey key
Definition: attoptcache.c:39
Definition: dynahash.c:219
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
static HTAB * AttoptCacheHash
Definition: attoptcache.c:28
unsigned int uint32
Definition: c.h:429
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1434
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
Size keysize
Definition: hsearch.h:75
static void InvalidateAttoptCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: attoptcache.c:54
int16 attnum
Definition: pg_attribute.h:79
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
AttributeOpts * opts
Definition: attoptcache.c:40
void CreateCacheMemoryContext(void)
Definition: catcache.c:620
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
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
void * palloc(Size size)
Definition: mcxt.c:950
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:228
void * arg
static void InitializeAttoptCache(void)
Definition: attoptcache.c:77
Definition: c.h:609
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
MemoryContext CacheMemoryContext
Definition: mcxt.c:47