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-2024, 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 #include "varatt.h"
26 
27 
28 /* Hash table for information about each attribute's options */
29 static HTAB *AttoptCacheHash = NULL;
30 
31 /* attrelid and attnum form the lookup key, and must appear first */
32 typedef struct
33 {
35  int attnum;
37 
38 typedef struct
39 {
40  AttoptCacheKey key; /* lookup key - must be first */
41  AttributeOpts *opts; /* options, or NULL if none */
43 
44 
45 /*
46  * InvalidateAttoptCacheCallback
47  * Flush all cache entries when pg_attribute is updated.
48  *
49  * When pg_attribute is updated, we must flush the cache entry at least
50  * for that attribute. Currently, we just flush them all. Since attribute
51  * options are not currently used in performance-critical paths (such as
52  * query execution), this seems OK.
53  */
54 static void
56 {
57  HASH_SEQ_STATUS status;
58  AttoptCacheEntry *attopt;
59 
61  while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
62  {
63  if (attopt->opts)
64  pfree(attopt->opts);
66  &attopt->key,
68  NULL) == NULL)
69  elog(ERROR, "hash table corrupted");
70  }
71 }
72 
73 /*
74  * InitializeAttoptCache
75  * Initialize the attribute options cache.
76  */
77 static void
79 {
80  HASHCTL ctl;
81 
82  /* Initialize the hash table. */
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 }
98 
99 /*
100  * get_attribute_options
101  * Fetch attribute options for a specified table OID.
102  */
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 }
AttributeOpts * get_attribute_options(Oid attrelid, int attnum)
Definition: attoptcache.c:104
static HTAB * AttoptCacheHash
Definition: attoptcache.c:29
static void InvalidateAttoptCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: attoptcache.c:55
static void InitializeAttoptCache(void)
Definition: attoptcache.c:78
unsigned int uint32
Definition: c.h:506
void CreateCacheMemoryContext(void)
Definition: catcache.c:679
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1395
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1385
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
@ HASH_FIND
Definition: hsearch.h:113
@ HASH_REMOVE
Definition: hsearch.h:115
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1516
void pfree(void *pointer)
Definition: mcxt.c:1520
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1180
MemoryContext CacheMemoryContext
Definition: mcxt.c:152
void * palloc(Size size)
Definition: mcxt.c:1316
static AmcheckOptions opts
Definition: pg_amcheck.c:111
int16 attnum
Definition: pg_attribute.h:74
void * arg
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
unsigned int Oid
Definition: postgres_ext.h:31
tree ctl
Definition: radixtree.h:1847
bytea * attribute_reloptions(Datum reloptions, bool validate)
Definition: reloptions.c:2069
AttributeOpts * opts
Definition: attoptcache.c:41
AttoptCacheKey key
Definition: attoptcache.c:40
Definition: dynahash.c:220
Definition: c.h:687
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