From 31ad154242e2dc9fa9bbc4ebbe31f559e2282d9c Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 25 Aug 2026 14:11:35 +0300 Subject: [PATCH] Fix crash on trying to expand a shared memory hash table When a hash table grows large enough we try to expand the directory, but we shouldn't try to do that if the hash table is marked as HASH_FIXED_SIZE. Before commit 9fe9ecd516b that was harmless, although it was a little weird to expand the directory when allocating the new element was doomed to fail later anyway. But the new allocator function added in that commit for shared memory hash tables did not expect to be called after hash table creation at all and would just crash. Fix by ensuring that the allocator is not called after hash table creation for HASH_FIXED_SIZE tables. The crash started with commit 9fe9ecd516b in v19, so backpatch to v19. To trigger the crash, you needed a shared memory hash table of just the right size so that we would attempt to resize it: power-of-2 and at least HASH_SEGSIZE (256) elements. With any other size, you would run out of allocated elements first. Author: Konstantin Knizhnik Reviewed-by: Rahila Syed Discussion: https://www.postgresql.org/message-id/d59221f2-b3d2-41ad-8bf0-d581b42e4cba@garret.ru Backpatch-through: 19 --- src/backend/utils/hash/dynahash.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c index dc7ae64a5a9..f6fc6271a2e 100644 --- a/src/backend/utils/hash/dynahash.c +++ b/src/backend/utils/hash/dynahash.c @@ -931,7 +931,7 @@ hash_search_with_hash_value(HTAB *hashp, * table is the subject of any active hash_seq_search scans. */ if (hctl->freeList[0].nentries > (int64) hctl->max_bucket && - !IS_PARTITIONED(hctl) && !hashp->frozen && + !hctl->isfixed && !IS_PARTITIONED(hctl) && !hashp->frozen && !has_seq_scans(hashp)) (void) expand_table(hashp); } @@ -1494,6 +1494,7 @@ expand_table(HTAB *hashp) HASHBUCKET currElement, nextElement; + Assert(!hctl->isfixed); Assert(!IS_PARTITIONED(hctl)); #ifdef HASH_STATISTICS @@ -1583,6 +1584,7 @@ dir_realloc(HTAB *hashp) int64 old_dirsize; int64 new_dirsize; + Assert(!hashp->hctl->isfixed); if (hashp->hctl->max_dsize != NO_MAX_DSIZE) return false; @@ -1618,6 +1620,8 @@ seg_alloc(HTAB *hashp) { HASHSEGMENT segp; + Assert(!hashp->hctl->isfixed); + segp = (HASHSEGMENT) hashp->alloc(sizeof(HASHBUCKET) * HASH_SEGSIZE, hashp->alloc_arg); if (!segp)