Improve hash_create()'s API for some added robustness.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 15 Dec 2020 16:38:53 +0000 (11:38 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 15 Dec 2020 16:38:53 +0000 (11:38 -0500)
commitb3817f5f774663d55931dd4fab9c5a94a15ae7ab
tree2a74193a670d8409ff4c8ff86d58cbbfa1e6a4ce
parenta58db3aa10e62e4228aa409ba006014fa07a8ca2
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
63 files changed:
contrib/dblink/dblink.c
contrib/pg_stat_statements/pg_stat_statements.c
contrib/postgres_fdw/connection.c
contrib/postgres_fdw/shippable.c
contrib/tablefunc/tablefunc.c
src/backend/access/gist/gistbuildbuffers.c
src/backend/access/hash/hashpage.c
src/backend/access/heap/rewriteheap.c
src/backend/access/transam/xlogutils.c
src/backend/catalog/pg_enum.c
src/backend/catalog/pg_inherits.c
src/backend/commands/async.c
src/backend/commands/prepare.c
src/backend/commands/sequence.c
src/backend/executor/execPartition.c
src/backend/nodes/extensible.c
src/backend/optimizer/util/predtest.c
src/backend/optimizer/util/relnode.c
src/backend/parser/parse_oper.c
src/backend/partitioning/partdesc.c
src/backend/postmaster/autovacuum.c
src/backend/postmaster/checkpointer.c
src/backend/postmaster/pgstat.c
src/backend/replication/logical/relation.c
src/backend/replication/logical/reorderbuffer.c
src/backend/replication/logical/tablesync.c
src/backend/replication/pgoutput/pgoutput.c
src/backend/storage/buffer/bufmgr.c
src/backend/storage/buffer/localbuf.c
src/backend/storage/file/reinit.c
src/backend/storage/ipc/shmem.c
src/backend/storage/ipc/standby.c
src/backend/storage/lmgr/lock.c
src/backend/storage/lmgr/lwlock.c
src/backend/storage/lmgr/predicate.c
src/backend/storage/smgr/smgr.c
src/backend/storage/sync/sync.c
src/backend/tsearch/ts_typanalyze.c
src/backend/utils/adt/array_typanalyze.c
src/backend/utils/adt/jsonfuncs.c
src/backend/utils/adt/pg_locale.c
src/backend/utils/adt/ri_triggers.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/attoptcache.c
src/backend/utils/cache/evtcache.c
src/backend/utils/cache/relcache.c
src/backend/utils/cache/relfilenodemap.c
src/backend/utils/cache/spccache.c
src/backend/utils/cache/ts_cache.c
src/backend/utils/cache/typcache.c
src/backend/utils/fmgr/dfmgr.c
src/backend/utils/fmgr/fmgr.c
src/backend/utils/hash/dynahash.c
src/backend/utils/mmgr/portalmem.c
src/backend/utils/time/combocid.c
src/include/utils/hsearch.h
src/pl/plperl/plperl.c
src/pl/plpgsql/src/pl_comp.c
src/pl/plpgsql/src/pl_exec.c
src/pl/plpython/plpy_plpymodule.c
src/pl/plpython/plpy_procedure.c
src/pl/tcl/pltcl.c
src/timezone/pgtz.c