Skip to content

Commit 322cd97

Browse files
JelteFCommitfest Bot
authored andcommitted
Use hash_make macros throughout the codebase
This shows how our code base looks when using the new APIs. This has some typesafety, readability and maintanability benefits, but it also introduces some backpatching problems. These backpatching problems cannot be resolved by backporting the new hash_make macros, because some of them require C11 (which we only require on master for now). I think it's unlikely that we'll need to backpatch things in code that creates hashtables though, so it could still be worth it to do this complete refactor. At the very least we should choose a few places where we use the new macros to make sure they have coverage.
1 parent ed67a9a commit 322cd97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+371
-835
lines changed

contrib/dblink/dblink.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,13 +2540,9 @@ getConnectionByName(const char *name)
25402540
static HTAB *
25412541
createConnHash(void)
25422542
{
2543-
HASHCTL ctl;
2544-
2545-
ctl.keysize = NAMEDATALEN;
2546-
ctl.entrysize = sizeof(remoteConnHashEnt);
2547-
2548-
return hash_create("Remote Con hash", NUMCONN, &ctl,
2549-
HASH_ELEM | HASH_STRINGS);
2543+
return hash_make_cxt(remoteConnHashEnt, name,
2544+
"Remote Con hash", NUMCONN,
2545+
TopMemoryContext);
25502546
}
25512547

25522548
static remoteConn *

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ static void
517517
pgss_shmem_startup(void)
518518
{
519519
bool found;
520-
HASHCTL info;
521520
FILE *file = NULL;
522521
FILE *qfile = NULL;
523522
uint32 header;
@@ -557,12 +556,9 @@ pgss_shmem_startup(void)
557556
pgss->stats.stats_reset = GetCurrentTimestamp();
558557
}
559558

560-
info.keysize = sizeof(pgssHashKey);
561-
info.entrysize = sizeof(pgssEntry);
562-
pgss_hash = ShmemInitHash("pg_stat_statements hash",
563-
pgss_max, pgss_max,
564-
&info,
565-
HASH_ELEM | HASH_BLOBS);
559+
pgss_hash = shmem_hash_make(pgssEntry, key,
560+
"pg_stat_statements hash",
561+
pgss_max, pgss_max);
566562

567563
LWLockRelease(AddinShmemInitLock);
568564

contrib/pg_trgm/trgm_regexp.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,6 @@ convertPgWchar(pg_wchar c, trgm_mb_char *result)
893893
static void
894894
transformGraph(TrgmNFA *trgmNFA)
895895
{
896-
HASHCTL hashCtl;
897896
TrgmStateKey initkey;
898897
TrgmState *initstate;
899898
ListCell *lc;
@@ -905,13 +904,7 @@ transformGraph(TrgmNFA *trgmNFA)
905904
trgmNFA->overflowed = false;
906905

907906
/* Create hashtable for states */
908-
hashCtl.keysize = sizeof(TrgmStateKey);
909-
hashCtl.entrysize = sizeof(TrgmState);
910-
hashCtl.hcxt = CurrentMemoryContext;
911-
trgmNFA->states = hash_create("Trigram NFA",
912-
1024,
913-
&hashCtl,
914-
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
907+
trgmNFA->states = hash_make(TrgmState, stateKey, "Trigram NFA", 1024);
915908
trgmNFA->nstates = 0;
916909

917910
/* Create initial state: ambiguous prefix, NFA's initial state */

contrib/postgres_fdw/connection.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,13 @@ GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
214214
/* First time through, initialize connection cache hashtable */
215215
if (ConnectionHash == NULL)
216216
{
217-
HASHCTL ctl;
218-
219217
if (pgfdw_we_get_result == 0)
220218
pgfdw_we_get_result =
221219
WaitEventExtensionNew("PostgresFdwGetResult");
222220

223-
ctl.keysize = sizeof(ConnCacheKey);
224-
ctl.entrysize = sizeof(ConnCacheEntry);
225-
ConnectionHash = hash_create("postgres_fdw connections", 8,
226-
&ctl,
227-
HASH_ELEM | HASH_BLOBS);
221+
ConnectionHash = hash_make_cxt(ConnCacheEntry, key,
222+
"postgres_fdw connections", 8,
223+
TopMemoryContext);
228224

229225
/*
230226
* Register some callback functions that manage connection cleanup.

contrib/postgres_fdw/shippable.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "postgres_fdw.h"
2929
#include "utils/hsearch.h"
3030
#include "utils/inval.h"
31+
#include "utils/memutils.h"
3132
#include "utils/syscache.h"
3233

3334
/* Hash table for caching the results of shippability lookups */
@@ -90,13 +91,10 @@ InvalidateShippableCacheCallback(Datum arg, int cacheid, uint32 hashvalue)
9091
static void
9192
InitializeShippableCache(void)
9293
{
93-
HASHCTL ctl;
94-
9594
/* Create the hash table. */
96-
ctl.keysize = sizeof(ShippableCacheKey);
97-
ctl.entrysize = sizeof(ShippableCacheEntry);
98-
ShippableCacheHash =
99-
hash_create("Shippability cache", 256, &ctl, HASH_ELEM | HASH_BLOBS);
95+
ShippableCacheHash = hash_make_cxt(ShippableCacheEntry, key,
96+
"Shippability cache", 256,
97+
TopMemoryContext);
10098

10199
/* Set up invalidation callback on pg_foreign_server. */
102100
CacheRegisterSyscacheCallback(FOREIGNSERVEROID,

contrib/tablefunc/tablefunc.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -705,24 +705,17 @@ static HTAB *
705705
load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
706706
{
707707
HTAB *crosstab_hash;
708-
HASHCTL ctl;
709708
int ret;
710709
uint64 proc;
711710
MemoryContext SPIcontext;
712711

713-
/* initialize the category hash table */
714-
ctl.keysize = MAX_CATNAME_LEN;
715-
ctl.entrysize = sizeof(crosstab_HashEnt);
716-
ctl.hcxt = per_query_ctx;
717-
718712
/*
719-
* use INIT_CATS, defined above as a guess of how many hash table entries
720-
* to create, initially
713+
* Initialize the category hash table. Use INIT_CATS, defined above as a
714+
* guess of how many hash table entries to create, initially.
721715
*/
722-
crosstab_hash = hash_create("crosstab hash",
723-
INIT_CATS,
724-
&ctl,
725-
HASH_ELEM | HASH_STRINGS | HASH_CONTEXT);
716+
crosstab_hash = hash_make_cxt(crosstab_HashEnt, internal_catname,
717+
"crosstab hash", INIT_CATS,
718+
per_query_ctx);
726719

727720
/* Connect to SPI manager */
728721
SPI_connect();

src/backend/access/common/heaptuple.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,9 @@ missing_match(const void *key1, const void *key2, Size keysize)
125125
static void
126126
init_missing_cache(void)
127127
{
128-
HASHCTL hash_ctl;
129-
130-
hash_ctl.keysize = sizeof(missing_cache_key);
131-
hash_ctl.entrysize = sizeof(missing_cache_key);
132-
hash_ctl.hcxt = TopMemoryContext;
133-
hash_ctl.hash = missing_hash;
134-
hash_ctl.match = missing_match;
135128
missing_cache =
136-
hash_create("Missing Values Cache",
137-
32,
138-
&hash_ctl,
139-
HASH_ELEM | HASH_CONTEXT | HASH_FUNCTION | HASH_COMPARE);
129+
hashset_make_fn_cxt(missing_cache_key, "Missing Values Cache", 32,
130+
missing_hash, missing_match, TopMemoryContext);
140131
}
141132

142133
/* ----------------------------------------------------------------

src/backend/access/gist/gistbuild.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,15 +1515,8 @@ typedef struct
15151515
static void
15161516
gistInitParentMap(GISTBuildState *buildstate)
15171517
{
1518-
HASHCTL hashCtl;
1519-
1520-
hashCtl.keysize = sizeof(BlockNumber);
1521-
hashCtl.entrysize = sizeof(ParentMapEntry);
1522-
hashCtl.hcxt = CurrentMemoryContext;
1523-
buildstate->parentMap = hash_create("gistbuild parent map",
1524-
1024,
1525-
&hashCtl,
1526-
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
1518+
buildstate->parentMap = hash_make(ParentMapEntry, childblkno,
1519+
"gistbuild parent map", 1024);
15271520
}
15281521

15291522
static void

src/backend/access/gist/gistbuildbuffers.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ GISTBuildBuffers *
4444
gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
4545
{
4646
GISTBuildBuffers *gfbb;
47-
HASHCTL hashCtl;
4847

4948
gfbb = palloc(sizeof(GISTBuildBuffers));
5049
gfbb->pagesPerBuffer = pagesPerBuffer;
@@ -72,13 +71,8 @@ gistInitBuildBuffers(int pagesPerBuffer, int levelStep, int maxLevel)
7271
* nodeBuffersTab hash is association between index blocks and it's
7372
* buffers.
7473
*/
75-
hashCtl.keysize = sizeof(BlockNumber);
76-
hashCtl.entrysize = sizeof(GISTNodeBuffer);
77-
hashCtl.hcxt = CurrentMemoryContext;
78-
gfbb->nodeBuffersTab = hash_create("gistbuildbuffers",
79-
1024,
80-
&hashCtl,
81-
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
74+
gfbb->nodeBuffersTab = hash_make(GISTNodeBuffer, nodeBlocknum,
75+
"gistbuildbuffers", 1024);
8276

8377
gfbb->bufferEmptyingQueue = NIL;
8478

src/backend/access/hash/hashpage.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,6 @@ void
13561356
_hash_finish_split(Relation rel, Buffer metabuf, Buffer obuf, Bucket obucket,
13571357
uint32 maxbucket, uint32 highmask, uint32 lowmask)
13581358
{
1359-
HASHCTL hash_ctl;
13601359
HTAB *tidhtab;
13611360
Buffer bucket_nbuf = InvalidBuffer;
13621361
Buffer nbuf;
@@ -1367,16 +1366,8 @@ _hash_finish_split(Relation rel, Buffer metabuf, Buffer obuf, Bucket obucket,
13671366
Bucket nbucket;
13681367
bool found;
13691368

1370-
/* Initialize hash tables used to track TIDs */
1371-
hash_ctl.keysize = sizeof(ItemPointerData);
1372-
hash_ctl.entrysize = sizeof(ItemPointerData);
1373-
hash_ctl.hcxt = CurrentMemoryContext;
1374-
1375-
tidhtab =
1376-
hash_create("bucket ctids",
1377-
256, /* arbitrary initial size */
1378-
&hash_ctl,
1379-
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
1369+
/* Initialize hash tables used to track TIDs (with arbitrary initial size) */
1370+
tidhtab = hashset_make(ItemPointerData, "bucket ctids", 256);
13801371

13811372
bucket_nblkno = nblkno = _hash_get_newblock_from_oldbucket(rel, obucket);
13821373

0 commit comments

Comments
 (0)