mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +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:
@@ -419,7 +419,6 @@ InitLocks(void)
|
||||
* Allocate hash table for LOCK structs. This stores per-locked-object
|
||||
* information.
|
||||
*/
|
||||
MemSet(&info, 0, sizeof(info));
|
||||
info.keysize = sizeof(LOCKTAG);
|
||||
info.entrysize = sizeof(LOCK);
|
||||
info.num_partitions = NUM_LOCK_PARTITIONS;
|
||||
|
@@ -342,7 +342,6 @@ init_lwlock_stats(void)
|
||||
ALLOCSET_DEFAULT_SIZES);
|
||||
MemoryContextAllowInCriticalSection(lwlock_stats_cxt, true);
|
||||
|
||||
MemSet(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(lwlock_stats_key);
|
||||
ctl.entrysize = sizeof(lwlock_stats);
|
||||
ctl.hcxt = lwlock_stats_cxt;
|
||||
|
@@ -1096,7 +1096,6 @@ InitPredicateLocks(void)
|
||||
* Allocate hash table for PREDICATELOCKTARGET structs. This stores
|
||||
* per-predicate-lock-target information.
|
||||
*/
|
||||
MemSet(&info, 0, sizeof(info));
|
||||
info.keysize = sizeof(PREDICATELOCKTARGETTAG);
|
||||
info.entrysize = sizeof(PREDICATELOCKTARGET);
|
||||
info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
|
||||
@@ -1129,7 +1128,6 @@ InitPredicateLocks(void)
|
||||
* Allocate hash table for PREDICATELOCK structs. This stores per
|
||||
* xact-lock-of-a-target information.
|
||||
*/
|
||||
MemSet(&info, 0, sizeof(info));
|
||||
info.keysize = sizeof(PREDICATELOCKTAG);
|
||||
info.entrysize = sizeof(PREDICATELOCK);
|
||||
info.hash = predicatelock_hash;
|
||||
@@ -1212,7 +1210,6 @@ InitPredicateLocks(void)
|
||||
* Allocate hash table for SERIALIZABLEXID structs. This stores per-xid
|
||||
* information for serializable transactions which have accessed data.
|
||||
*/
|
||||
MemSet(&info, 0, sizeof(info));
|
||||
info.keysize = sizeof(SERIALIZABLEXIDTAG);
|
||||
info.entrysize = sizeof(SERIALIZABLEXID);
|
||||
|
||||
@@ -1853,7 +1850,6 @@ CreateLocalPredicateLockHash(void)
|
||||
|
||||
/* Initialize the backend-local hash table of parent locks */
|
||||
Assert(LocalPredicateLockHash == NULL);
|
||||
MemSet(&hash_ctl, 0, sizeof(hash_ctl));
|
||||
hash_ctl.keysize = sizeof(PREDICATELOCKTARGETTAG);
|
||||
hash_ctl.entrysize = sizeof(LOCALPREDICATELOCK);
|
||||
LocalPredicateLockHash = hash_create("Local predicate lock",
|
||||
|
Reference in New Issue
Block a user