mirror of
https://github.com/postgres/postgres.git
synced 2025-08-31 17:02:12 +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:
@@ -292,7 +292,6 @@ void
|
||||
InitShmemIndex(void)
|
||||
{
|
||||
HASHCTL info;
|
||||
int hash_flags;
|
||||
|
||||
/*
|
||||
* Create the shared memory shmem index.
|
||||
@@ -304,11 +303,11 @@ InitShmemIndex(void)
|
||||
*/
|
||||
info.keysize = SHMEM_INDEX_KEYSIZE;
|
||||
info.entrysize = sizeof(ShmemIndexEnt);
|
||||
hash_flags = HASH_ELEM;
|
||||
|
||||
ShmemIndex = ShmemInitHash("ShmemIndex",
|
||||
SHMEM_INDEX_SIZE, SHMEM_INDEX_SIZE,
|
||||
&info, hash_flags);
|
||||
&info,
|
||||
HASH_ELEM | HASH_STRINGS);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -329,6 +328,11 @@ InitShmemIndex(void)
|
||||
* whose maximum size is certain, this should be equal to max_size; that
|
||||
* ensures that no run-time out-of-shared-memory failures can occur.
|
||||
*
|
||||
* *infoP and hash_flags must specify at least the entry sizes and key
|
||||
* comparison semantics (see hash_create()). Flag bits and values specific
|
||||
* to shared-memory hash tables are added here, except that callers may
|
||||
* choose to specify HASH_PARTITION and/or HASH_FIXED_SIZE.
|
||||
*
|
||||
* Note: before Postgres 9.0, this function returned NULL for some failure
|
||||
* cases. Now, it always throws error instead, so callers need not check
|
||||
* for NULL.
|
||||
|
Reference in New Issue
Block a user