You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postgres/src/backend/utils/cache/attoptcache.c

179 lines
4.5 KiB

/*-------------------------------------------------------------------------
*
* attoptcache.c
* Attribute options cache management.
*
* Attribute options are cached separately from the fixed-size portion of
* pg_attribute entries, which are handled by the relcache.
*
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/utils/cache/attoptcache.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/reloptions.h"
#include "utils/attoptcache.h"
#include "utils/catcache.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/syscache.h"
/* Hash table for informations about each attribute's options */
static HTAB *AttoptCacheHash = NULL;
/* attrelid and attnum form the lookup key, and must appear first */
typedef struct
{
Oid attrelid;
int attnum;
} AttoptCacheKey;
typedef struct
{
AttoptCacheKey key; /* lookup key - must be first */
AttributeOpts *opts; /* options, or NULL if none */
} AttoptCacheEntry;
/*
* InvalidateAttoptCacheCallback
* Flush all cache entries when pg_attribute is updated.
*
* When pg_attribute is updated, we must flush the cache entry at least
* for that attribute. Currently, we just flush them all. Since attribute
* options are not currently used in performance-critical paths (such as
* query execution), this seems OK.
*/
static void
InvalidateAttoptCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
{
HASH_SEQ_STATUS status;
AttoptCacheEntry *attopt;
hash_seq_init(&status, AttoptCacheHash);
while ((attopt = (AttoptCacheEntry *) hash_seq_search(&status)) != NULL)
{
if (attopt->opts)
pfree(attopt->opts);
if (hash_search(AttoptCacheHash,
(void *) &attopt->key,
HASH_REMOVE,
NULL) == NULL)
elog(ERROR, "hash table corrupted");
}
}
/*
* InitializeAttoptCache
* Initialize the attribute options cache.
*/
static void
InitializeAttoptCache(void)
{
HASHCTL ctl;
/* Initialize the hash table. */
MemSet(&ctl, 0, sizeof(ctl));
ctl.keysize = sizeof(AttoptCacheKey);
ctl.entrysize = sizeof(AttoptCacheEntry);
AttoptCacheHash =
hash_create("Attopt cache", 256, &ctl,
Improve hash_create's API for selecting simple-binary-key hash functions. Previously, if you wanted anything besides C-string hash keys, you had to specify a custom hashing function to hash_create(). Nearly all such callers were specifying tag_hash or oid_hash; which is tedious, and rather error-prone, since a caller could easily miss the opportunity to optimize by using hash_uint32 when appropriate. Replace this with a design whereby callers using simple binary-data keys just specify HASH_BLOBS and don't need to mess with specific support functions. hash_create() itself will take care of optimizing when the key size is four bytes. This nets out saving a few hundred bytes of code space, and offers a measurable performance improvement in tidbitmap.c (which was not exploiting the opportunity to use hash_uint32 for its 4-byte keys). There might be some wins elsewhere too, I didn't analyze closely. In future we could look into offering a similar optimized hashing function for 8-byte keys. Under this design that could be done in a centralized and machine-independent fashion, whereas getting it right for keys of platform-dependent sizes would've been notationally painful before. For the moment, the old way still works fine, so as not to break source code compatibility for loadable modules. Eventually we might want to remove tag_hash and friends from the exported API altogether, since there's no real need for them to be explicitly referenced from outside dynahash.c. Teodor Sigaev and Tom Lane
11 years ago
HASH_ELEM | HASH_BLOBS);
/* Make sure we've initialized CacheMemoryContext. */
if (!CacheMemoryContext)
CreateCacheMemoryContext();
/* Watch for invalidation events. */
CacheRegisterSyscacheCallback(ATTNUM,
InvalidateAttoptCacheCallback,
(Datum) 0);
}
/*
* get_attribute_options
* Fetch attribute options for a specified table OID.
*/
AttributeOpts *
get_attribute_options(Oid attrelid, int attnum)
{
AttoptCacheKey key;
AttoptCacheEntry *attopt;
AttributeOpts *result;
HeapTuple tp;
/* Find existing cache entry, if any. */
if (!AttoptCacheHash)
InitializeAttoptCache();
Phase 2 of pgindent updates. Change pg_bsd_indent to follow upstream rules for placement of comments to the right of code, and remove pgindent hack that caused comments following #endif to not obey the general rule. Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using the published version of pg_bsd_indent, but a hacked-up version that tried to minimize the amount of movement of comments to the right of code. The situation of interest is where such a comment has to be moved to the right of its default placement at column 33 because there's code there. BSD indent has always moved right in units of tab stops in such cases --- but in the previous incarnation, indent was working in 8-space tab stops, while now it knows we use 4-space tabs. So the net result is that in about half the cases, such comments are placed one tab stop left of before. This is better all around: it leaves more room on the line for comment text, and it means that in such cases the comment uniformly starts at the next 4-space tab stop after the code, rather than sometimes one and sometimes two tabs after. Also, ensure that comments following #endif are indented the same as comments following other preprocessor commands such as #else. That inconsistency turns out to have been self-inflicted damage from a poorly-thought-through post-indent "fixup" in pgindent. This patch is much less interesting than the first round of indent changes, but also bulkier, so I thought it best to separate the effects. Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
8 years ago
memset(&key, 0, sizeof(key)); /* make sure any padding bits are unset */
key.attrelid = attrelid;
key.attnum = attnum;
attopt =
(AttoptCacheEntry *) hash_search(AttoptCacheHash,
(void *) &key,
HASH_FIND,
NULL);
/* Not found in Attopt cache. Construct new cache entry. */
if (!attopt)
{
AttributeOpts *opts;
tp = SearchSysCache2(ATTNUM,
ObjectIdGetDatum(attrelid),
Int16GetDatum(attnum));
/*
* If we don't find a valid HeapTuple, it must mean someone has
* managed to request attribute details for a non-existent attribute.
* We treat that case as if no options were specified.
*/
if (!HeapTupleIsValid(tp))
opts = NULL;
else
{
Datum datum;
bool isNull;
datum = SysCacheGetAttr(ATTNUM,
tp,
Anum_pg_attribute_attoptions,
&isNull);
if (isNull)
opts = NULL;
else
{
bytea *bytea_opts = attribute_reloptions(datum, false);
opts = MemoryContextAlloc(CacheMemoryContext,
VARSIZE(bytea_opts));
memcpy(opts, bytea_opts, VARSIZE(bytea_opts));
}
ReleaseSysCache(tp);
}
/*
* It's important to create the actual cache entry only after reading
* pg_attribute, since the read could cause a cache flush.
*/
attopt = (AttoptCacheEntry *) hash_search(AttoptCacheHash,
(void *) &key,
HASH_ENTER,
NULL);
attopt->opts = opts;
}
/* Return results in caller's memory context. */
if (attopt->opts == NULL)
return NULL;
result = palloc(VARSIZE(attopt->opts));
memcpy(result, attopt->opts, VARSIZE(attopt->opts));
return result;
}