diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index dce355e6683..7a9d5c6198b 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -125,11 +125,14 @@ void InitShmemAllocator(PGShmemHeader *seghdr) { Size offset; - int64 hash_size; HASHCTL info; int hash_flags; size_t size; +#ifndef EXEC_BACKEND + Assert(!IsUnderPostmaster); +#endif + Assert(seghdr != NULL); /* @@ -139,10 +142,6 @@ InitShmemAllocator(PGShmemHeader *seghdr) Assert(seghdr == (void *) MAXALIGN(seghdr)); Assert(seghdr->content_offset == MAXALIGN(seghdr->content_offset)); - ShmemSegHdr = seghdr; - ShmemBase = seghdr; - ShmemEnd = (char *) ShmemBase + seghdr->totalsize; - /* * Allocations after this point should go through ShmemAlloc, which * expects to allocate everything on cache line boundaries. Make sure the @@ -155,18 +154,25 @@ InitShmemAllocator(PGShmemHeader *seghdr) errmsg("out of shared memory (%zu bytes requested)", offset))); + /* + * In postmaster or stand-alone backend, initialize the shared memory + * allocator and the spinlock so that we can allocate shared memory for + * ShmemIndex using ShmemAlloc(). In a regular backend just set up the + * pointers required by ShmemAlloc(). + */ ShmemAllocator = (ShmemAllocatorData *) ((char *) seghdr + seghdr->content_offset); - ShmemLock = &ShmemAllocator->shmem_lock; -#ifndef EXEC_BACKEND - Assert(!IsUnderPostmaster); -#endif if (!IsUnderPostmaster) { SpinLockInit(&ShmemAllocator->shmem_lock); ShmemAllocator->free_offset = offset; } + ShmemLock = &ShmemAllocator->shmem_lock; + ShmemSegHdr = seghdr; + ShmemBase = seghdr; + ShmemEnd = (char *) ShmemBase + seghdr->totalsize; + /* * Create (or attach to) the shared memory index of shmem areas. * @@ -174,11 +180,9 @@ InitShmemAllocator(PGShmemHeader *seghdr) * use ShmemInitHash() here because it relies on ShmemIndex being already * initialized. */ - hash_size = SHMEM_INDEX_SIZE; - info.keysize = SHMEM_INDEX_KEYSIZE; info.entrysize = sizeof(ShmemIndexEnt); - info.dsize = info.max_dsize = hash_select_dirsize(hash_size); + info.dsize = info.max_dsize = hash_select_dirsize(SHMEM_INDEX_SIZE); info.alloc = ShmemAllocNoError; hash_flags = HASH_ELEM | HASH_STRINGS | HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE; if (!IsUnderPostmaster) @@ -189,7 +193,7 @@ InitShmemAllocator(PGShmemHeader *seghdr) else hash_flags |= HASH_ATTACH; info.hctl = ShmemAllocator->index; - ShmemIndex = hash_create("ShmemIndex", hash_size, &info, hash_flags); + ShmemIndex = hash_create("ShmemIndex", SHMEM_INDEX_SIZE, &info, hash_flags); Assert(ShmemIndex != NULL); }