1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

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 9fe9ecd516 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 9fe9ecd516 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 <knizhnik@garret.ru>
Reviewed-by: Rahila Syed <rahilasyed90@gmail.com>
Discussion: https://www.postgresql.org/message-id/d59221f2-b3d2-41ad-8bf0-d581b42e4cba@garret.ru
Backpatch-through: 19
This commit is contained in:
Heikki Linnakangas
2026-08-25 14:11:35 +03:00
parent 77027b347c
commit 31ad154242

View File

@@ -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)