1 /*-------------------------------------------------------------------------
4 * Attribute options cache management.
6 * Attribute options are cached separately from the fixed-size portion of
7 * pg_attribute entries, which are handled by the relcache.
9 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/backend/utils/cache/attoptcache.c
15 *-------------------------------------------------------------------------
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"
27 /* Hash table for informations about each attribute's options */
28 static HTAB *AttoptCacheHash = NULL;
30 /* attrelid and attnum form the lookup key, and must appear first */
39 AttoptCacheKey key; /* lookup key - must be first */
40 AttributeOpts *opts; /* options, or NULL if none */
45 * InvalidateAttoptCacheCallback
46 * Flush all cache entries when pg_attribute is updated.
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.
54 InvalidateAttoptCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
56 HASH_SEQ_STATUS status;
57 AttoptCacheEntry *attopt;
59 hash_seq_init(&status, AttoptCacheHash);
60 while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
64 if (hash_search(AttoptCacheHash,
65 (void *) &attopt->key,
68 elog(ERROR, "hash table corrupted");
73 * InitializeAttoptCache
74 * Initialize the tablespace cache.
77 InitializeAttoptCache(void)
81 /* Initialize the hash table. */
82 MemSet(&ctl, 0, sizeof(ctl));
83 ctl.keysize = sizeof(AttoptCacheKey);
84 ctl.entrysize = sizeof(AttoptCacheEntry);
87 hash_create("Attopt cache", 256, &ctl,
88 HASH_ELEM | HASH_FUNCTION);
90 /* Make sure we've initialized CacheMemoryContext. */
91 if (!CacheMemoryContext)
92 CreateCacheMemoryContext();
94 /* Watch for invalidation events. */
95 CacheRegisterSyscacheCallback(ATTNUM,
96 InvalidateAttoptCacheCallback,
101 * get_attribute_options
102 * Fetch attribute options for a specified table OID.
105 get_attribute_options(Oid attrelid, int attnum)
108 AttoptCacheEntry *attopt;
109 AttributeOpts *result;
112 /* Find existing cache entry, if any. */
113 if (!AttoptCacheHash)
114 InitializeAttoptCache();
115 memset(&key, 0, sizeof(key)); /* make sure any padding bits are
117 key.attrelid = attrelid;
120 (AttoptCacheEntry *) hash_search(AttoptCacheHash,
125 /* Not found in Attopt cache. Construct new cache entry. */
130 tp = SearchSysCache2(ATTNUM,
131 ObjectIdGetDatum(attrelid),
132 Int16GetDatum(attnum));
135 * If we don't find a valid HeapTuple, it must mean someone has
136 * managed to request attribute details for a non-existent attribute.
137 * We treat that case as if no options were specified.
139 if (!HeapTupleIsValid(tp))
146 datum = SysCacheGetAttr(ATTNUM,
148 Anum_pg_attribute_attoptions,
154 bytea *bytea_opts = attribute_reloptions(datum, false);
156 opts = MemoryContextAlloc(CacheMemoryContext,
157 VARSIZE(bytea_opts));
158 memcpy(opts, bytea_opts, VARSIZE(bytea_opts));
164 * It's important to create the actual cache entry only after reading
165 * pg_attribute, since the read could cause a cache flush.
167 attopt = (AttoptCacheEntry *) hash_search(AttoptCacheHash,
174 /* Return results in caller's memory context. */
175 if (attopt->opts == NULL)
177 result = palloc(VARSIZE(attopt->opts));
178 memcpy(result, attopt->opts, VARSIZE(attopt->opts));