mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Improve hash_create()'s API for some added robustness.
Invent a new flag bit HASH_STRINGS to specify C-string hashing, which
was formerly the default; and add assertions insisting that exactly
one of the bits HASH_STRINGS, HASH_BLOBS, and HASH_FUNCTION be set.
This is in hopes of preventing recurrences of the type of oversight
fixed in commit a1b8aa1e4
(i.e., mistakenly omitting HASH_BLOBS).
Also, when HASH_STRINGS is specified, insist that the keysize be
more than 8 bytes. This is a heuristic, but it should catch
accidental use of HASH_STRINGS for integer or pointer keys.
(Nearly all existing use-cases set the keysize to NAMEDATALEN or
more, so there's little reason to think this restriction should
be problematic.)
Tweak hash_create() to insist that the HASH_ELEM flag be set, and
remove the defaults it had for keysize and entrysize. Since those
defaults were undocumented and basically useless, no callers
omitted HASH_ELEM anyway.
Also, remove memset's zeroing the HASHCTL parameter struct from
those callers that had one. This has never been really necessary,
and while it wasn't a bad coding convention it was confusing that
some callers did it and some did not. We might as well save a few
cycles by standardizing on "not".
Also improve the documentation for hash_create().
In passing, improve reinit.c's usage of a hash table by storing
the key as a binary Oid rather than a string; and, since that's
a temporary hash table, allocate it in CurrentMemoryContext for
neatness.
Discussion: https://postgr.es/m/590625.1607878171@sss.pgh.pa.us
This commit is contained in:
1
src/backend/utils/cache/attoptcache.c
vendored
1
src/backend/utils/cache/attoptcache.c
vendored
@ -79,7 +79,6 @@ InitializeAttoptCache(void)
|
||||
HASHCTL ctl;
|
||||
|
||||
/* Initialize the hash table. */
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(AttoptCacheKey);
|
||||
ctl.entrysize = sizeof(AttoptCacheEntry);
|
||||
AttoptCacheHash =
|
||||
|
1
src/backend/utils/cache/evtcache.c
vendored
1
src/backend/utils/cache/evtcache.c
vendored
@ -118,7 +118,6 @@ BuildEventTriggerCache(void)
|
||||
EventTriggerCacheState = ETCS_REBUILD_STARTED;
|
||||
|
||||
/* Create new hash table. */
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(EventTriggerEvent);
|
||||
ctl.entrysize = sizeof(EventTriggerCacheEntry);
|
||||
ctl.hcxt = EventTriggerCacheContext;
|
||||
|
2
src/backend/utils/cache/relcache.c
vendored
2
src/backend/utils/cache/relcache.c
vendored
@ -1607,7 +1607,6 @@ LookupOpclassInfo(Oid operatorClassOid,
|
||||
/* First time through: initialize the opclass cache */
|
||||
HASHCTL ctl;
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(OpClassCacheEnt);
|
||||
OpClassCache = hash_create("Operator class cache", 64,
|
||||
@ -3775,7 +3774,6 @@ RelationCacheInitialize(void)
|
||||
/*
|
||||
* create hashtable that indexes the relcache
|
||||
*/
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(RelIdCacheEnt);
|
||||
RelationIdCache = hash_create("Relcache by OID", INITRELCACHESIZE,
|
||||
|
10
src/backend/utils/cache/relfilenodemap.c
vendored
10
src/backend/utils/cache/relfilenodemap.c
vendored
@ -110,17 +110,15 @@ InitializeRelfilenodeMap(void)
|
||||
relfilenode_skey[0].sk_attno = Anum_pg_class_reltablespace;
|
||||
relfilenode_skey[1].sk_attno = Anum_pg_class_relfilenode;
|
||||
|
||||
/* Initialize the hash table. */
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(RelfilenodeMapKey);
|
||||
ctl.entrysize = sizeof(RelfilenodeMapEntry);
|
||||
ctl.hcxt = CacheMemoryContext;
|
||||
|
||||
/*
|
||||
* Only create the RelfilenodeMapHash now, so we don't end up partially
|
||||
* initialized when fmgr_info_cxt() above ERRORs out with an out of memory
|
||||
* error.
|
||||
*/
|
||||
ctl.keysize = sizeof(RelfilenodeMapKey);
|
||||
ctl.entrysize = sizeof(RelfilenodeMapEntry);
|
||||
ctl.hcxt = CacheMemoryContext;
|
||||
|
||||
RelfilenodeMapHash =
|
||||
hash_create("RelfilenodeMap cache", 64, &ctl,
|
||||
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
|
||||
|
1
src/backend/utils/cache/spccache.c
vendored
1
src/backend/utils/cache/spccache.c
vendored
@ -79,7 +79,6 @@ InitializeTableSpaceCache(void)
|
||||
HASHCTL ctl;
|
||||
|
||||
/* Initialize the hash table. */
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(TableSpaceCacheEntry);
|
||||
TableSpaceCacheHash =
|
||||
|
3
src/backend/utils/cache/ts_cache.c
vendored
3
src/backend/utils/cache/ts_cache.c
vendored
@ -117,7 +117,6 @@ lookup_ts_parser_cache(Oid prsId)
|
||||
/* First time through: initialize the hash table */
|
||||
HASHCTL ctl;
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(TSParserCacheEntry);
|
||||
TSParserCacheHash = hash_create("Tsearch parser cache", 4,
|
||||
@ -215,7 +214,6 @@ lookup_ts_dictionary_cache(Oid dictId)
|
||||
/* First time through: initialize the hash table */
|
||||
HASHCTL ctl;
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(TSDictionaryCacheEntry);
|
||||
TSDictionaryCacheHash = hash_create("Tsearch dictionary cache", 8,
|
||||
@ -365,7 +363,6 @@ init_ts_config_cache(void)
|
||||
{
|
||||
HASHCTL ctl;
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(TSConfigCacheEntry);
|
||||
TSConfigCacheHash = hash_create("Tsearch configuration cache", 16,
|
||||
|
2
src/backend/utils/cache/typcache.c
vendored
2
src/backend/utils/cache/typcache.c
vendored
@ -341,7 +341,6 @@ lookup_type_cache(Oid type_id, int flags)
|
||||
/* First time through: initialize the hash table */
|
||||
HASHCTL ctl;
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(TypeCacheEntry);
|
||||
TypeCacheHash = hash_create("Type information cache", 64,
|
||||
@ -1874,7 +1873,6 @@ assign_record_type_typmod(TupleDesc tupDesc)
|
||||
/* First time through: initialize the hash table */
|
||||
HASHCTL ctl;
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(TupleDesc); /* just the pointer */
|
||||
ctl.entrysize = sizeof(RecordCacheEntry);
|
||||
ctl.hash = record_type_typmod_hash;
|
||||
|
Reference in New Issue
Block a user