From 85cf9e3555458b6ba77e7113317694e3d5aa151f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Fri, 27 Mar 2026 23:25:35 +0200
Subject: [PATCH v1 7/8] Remove HASH_DIRSIZE, always use the default algorithm
 to select it

It's not very useful to specify a non-standard directory size, and
always just used the default algorithm for it. hash_estimate_size()
assumed the default directory size anyway.
---
 src/backend/storage/ipc/shmem.c   |  7 ++--
 src/backend/utils/hash/dynahash.c | 54 +++++++------------------------
 src/include/utils/hsearch.h       |  6 +---
 3 files changed, 14 insertions(+), 53 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index c8171125871..2ee7d06e4d3 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -189,11 +189,9 @@ InitShmemAllocator(PGShmemHeader *seghdr)
 	 */
 	info.keysize = SHMEM_INDEX_KEYSIZE;
 	info.entrysize = sizeof(ShmemIndexEnt);
-	info.dsize = info.max_dsize = hash_select_dirsize(SHMEM_INDEX_SIZE);
 	info.alloc = ShmemHashAlloc;
 	info.alloc_arg = NULL;
-	hash_flags = HASH_ELEM | HASH_STRINGS
-		| HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE | HASH_FIXED_SIZE;
+	hash_flags = HASH_ELEM | HASH_STRINGS | HASH_SHARED_MEM | HASH_ALLOC | HASH_FIXED_SIZE;
 
 	if (!IsUnderPostmaster)
 	{
@@ -368,10 +366,9 @@ ShmemInitHash(const char *name,		/* table string name for shmem index */
 	 *
 	 * The shared memory allocator must be specified too.
 	 */
-	infoP->dsize = infoP->max_dsize = hash_select_dirsize(nelems);
 	infoP->alloc = ShmemHashAlloc;
 	infoP->alloc_arg = NULL;
-	hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE | HASH_FIXED_SIZE;
+	hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_FIXED_SIZE;
 
 	size = hash_estimate_size(nelems, infoP->entrysize);
 
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c
index 1173304ef0f..378213fda6c 100644
--- a/src/backend/utils/hash/dynahash.c
+++ b/src/backend/utils/hash/dynahash.c
@@ -115,10 +115,12 @@
  * a whole lot of records per bucket or performance goes down.
  *
  * In a hash table allocated in shared memory, the directory cannot be
- * expanded because it must stay at a fixed address.  The directory size
- * should be selected using hash_select_dirsize (and you'd better have
- * a good idea of the maximum number of entries!).  For non-shared hash
- * tables, the initial directory size can be left at the default.
+ * expanded because it must stay at a fixed address.  The directory size is
+ * chosen at creation based on the initial number of initial elements, so even
+ * though we support allocating more elements later, performance will suffer
+ * if the table grows much beyond the initial size.  (Currently, shared memory
+ * hash tables are only created by ShmemInitHash() though, which doesn't
+ * support growing at all.)
  */
 #define HASH_SEGSIZE			   256
 #define HASH_SEGSIZE_SHIFT	   8	/* must be log2(HASH_SEGSIZE) */
@@ -553,15 +555,6 @@ hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
 		hctl->num_partitions = info->num_partitions;
 	}
 
-	/*
-	 * SHM hash tables have fixed directory size passed by the caller.
-	 */
-	if (flags & HASH_DIRSIZE)
-	{
-		hctl->max_dsize = info->max_dsize;
-		hctl->dsize = info->dsize;
-	}
-
 	/* remember the entry sizes, too */
 	hctl->keysize = info->keysize;
 	hctl->entrysize = info->entrysize;
@@ -735,8 +728,11 @@ init_htab(HTAB *hashp, int64 nelem)
 	/*
 	 * Make sure directory is big enough.
 	 */
-	if (nsegs > hctl->dsize)
-		hctl->dsize = nsegs;
+	hctl->dsize = nsegs;
+
+	/* SHM hash tables have fixed directory. */
+	if (hashp->isshared)
+		hctl->max_dsize = hctl->dsize;
 
 	/* Allocate a directory */
 	hctl->dir = (HASHSEGMENT *)
@@ -804,34 +800,6 @@ hash_estimate_size(int64 num_entries, Size entrysize)
 	return size;
 }
 
-/*
- * Select an appropriate directory size for a hashtable with the given
- * maximum number of entries.
- * This is only needed for hashtables in shared memory, whose directories
- * cannot be expanded dynamically.
- * NB: assumes that all hash structure parameters have default values!
- *
- * XXX this had better agree with the behavior of init_htab()...
- */
-int64
-hash_select_dirsize(int64 num_entries)
-{
-	int64		nBuckets,
-				nSegments,
-				nDirEntries;
-
-	/* estimate number of buckets wanted */
-	nBuckets = next_pow2_int64(num_entries);
-	/* # of segments needed for nBuckets */
-	nSegments = next_pow2_int64((nBuckets - 1) / HASH_SEGSIZE + 1);
-	/* directory entries */
-	nDirEntries = DEF_DIRSIZE;
-	while (nDirEntries < nSegments)
-		nDirEntries <<= 1;		/* dir_alloc doubles dsize at each call */
-
-	return nDirEntries;
-}
-
 
 /********************** DESTROY ROUTINES ************************/
 
diff --git a/src/include/utils/hsearch.h b/src/include/utils/hsearch.h
index b60ae20acc7..115e97642f5 100644
--- a/src/include/utils/hsearch.h
+++ b/src/include/utils/hsearch.h
@@ -66,9 +66,6 @@ typedef struct HASHCTL
 {
 	/* Used if HASH_PARTITION flag is set: */
 	int64		num_partitions; /* # partitions (must be power of 2) */
-	/* Used if HASH_DIRSIZE flag is set: */
-	int64		dsize;			/* (initial) directory size */
-	int64		max_dsize;		/* limit to dsize if dir size is limited */
 	/* Used if HASH_ELEM flag is set (which is now required): */
 	Size		keysize;		/* hash key length in bytes */
 	Size		entrysize;		/* total user element size in bytes */
@@ -90,7 +87,7 @@ typedef struct HASHCTL
 /* Flag bits for hash_create; most indicate which parameters are supplied */
 #define HASH_PARTITION	0x0001	/* Hashtable is used w/partitioned locking */
 /* 0x0002 is unused */
-#define HASH_DIRSIZE	0x0004	/* Set directory size (initial and max) */
+/* 0x0004 is unused */
 #define HASH_ELEM		0x0008	/* Set keysize and entrysize (now required!) */
 #define HASH_STRINGS	0x0010	/* Select support functions for string keys */
 #define HASH_BLOBS		0x0020	/* Select support functions for binary keys */
@@ -149,7 +146,6 @@ extern void *hash_seq_search(HASH_SEQ_STATUS *status);
 extern void hash_seq_term(HASH_SEQ_STATUS *status);
 extern void hash_freeze(HTAB *hashp);
 extern Size hash_estimate_size(int64 num_entries, Size entrysize);
-extern int64 hash_select_dirsize(int64 num_entries);
 extern void AtEOXact_HashTables(bool isCommit);
 extern void AtEOSubXact_HashTables(bool isCommit, int nestDepth);
 
-- 
2.47.3

