mirror of
https://github.com/postgres/postgres.git
synced 2025-12-21 05:21:08 +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:
@@ -30,7 +30,7 @@ static void ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname,
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char oid[OIDCHARS + 1];
|
||||
Oid reloid; /* hash key */
|
||||
} unlogged_relation_entry;
|
||||
|
||||
/*
|
||||
@@ -172,10 +172,11 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
|
||||
* need to be reset. Otherwise, this cleanup operation would be
|
||||
* O(n^2).
|
||||
*/
|
||||
memset(&ctl, 0, sizeof(ctl));
|
||||
ctl.keysize = sizeof(unlogged_relation_entry);
|
||||
ctl.keysize = sizeof(Oid);
|
||||
ctl.entrysize = sizeof(unlogged_relation_entry);
|
||||
hash = hash_create("unlogged hash", 32, &ctl, HASH_ELEM);
|
||||
ctl.hcxt = CurrentMemoryContext;
|
||||
hash = hash_create("unlogged relation OIDs", 32, &ctl,
|
||||
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
|
||||
|
||||
/* Scan the directory. */
|
||||
dbspace_dir = AllocateDir(dbspacedirname);
|
||||
@@ -198,9 +199,8 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
|
||||
* Put the OID portion of the name into the hash table, if it
|
||||
* isn't already.
|
||||
*/
|
||||
memset(ent.oid, 0, sizeof(ent.oid));
|
||||
memcpy(ent.oid, de->d_name, oidchars);
|
||||
hash_search(hash, &ent, HASH_ENTER, NULL);
|
||||
ent.reloid = atooid(de->d_name);
|
||||
(void) hash_search(hash, &ent, HASH_ENTER, NULL);
|
||||
}
|
||||
|
||||
/* Done with the first pass. */
|
||||
@@ -224,7 +224,6 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
|
||||
{
|
||||
ForkNumber forkNum;
|
||||
int oidchars;
|
||||
bool found;
|
||||
unlogged_relation_entry ent;
|
||||
|
||||
/* Skip anything that doesn't look like a relation data file. */
|
||||
@@ -238,14 +237,10 @@ ResetUnloggedRelationsInDbspaceDir(const char *dbspacedirname, int op)
|
||||
|
||||
/*
|
||||
* See whether the OID portion of the name shows up in the hash
|
||||
* table.
|
||||
* table. If so, nuke it!
|
||||
*/
|
||||
memset(ent.oid, 0, sizeof(ent.oid));
|
||||
memcpy(ent.oid, de->d_name, oidchars);
|
||||
hash_search(hash, &ent, HASH_FIND, &found);
|
||||
|
||||
/* If so, nuke it! */
|
||||
if (found)
|
||||
ent.reloid = atooid(de->d_name);
|
||||
if (hash_search(hash, &ent, HASH_FIND, NULL))
|
||||
{
|
||||
snprintf(rm_path, sizeof(rm_path), "%s/%s",
|
||||
dbspacedirname, de->d_name);
|
||||
|
||||
Reference in New Issue
Block a user