From: pashkinelfe (@pashkinelfe) <noreply+pashkinelfe@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #602: Memory allocation failure at CREATE EXTENSION.
Date: Fri, 21 Jun 2024 07:50:57 +0000
Message-ID: <gh-pgvector-pgvector-602@github.com> (raw)
Hi!
I found the following error at trying create extension on one of the customers machine:
`not enough shared memory for data structure "hnsw LWLock ids" (4 bytes requested)`
called from the code block
```
/*
* Assign a tranche ID for our LWLocks. This only needs to be done by one
* backend, as the tranche ID is remembered in shared memory.
*
* This shared memory area is very small, so we just allocate it from the
* "slop" that PostgreSQL reserves for small allocations like this. If
* this grows bigger, we should use a shmem_request_hook and
* RequestAddinShmemSpace() to pre-reserve space for this.
*/
void
HnswInitLockTranche(void)
{
int *tranche_ids;
bool found;
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
tranche_ids = ShmemInitStruct("hnsw LWLock ids",
sizeof(int) * 1,
&found);
if (!found)
tranche_ids[0] = LWLockNewTrancheId();
hnsw_lock_tranche_id = tranche_ids[0];
LWLockRelease(AddinShmemInitLock);
/* Per-backend registration of the tranche ID */
LWLockRegisterTranche(hnsw_lock_tranche_id, "HnswBuild");
}
```
It's a rare error that I met only once. Could we not rely that the 'slop' has enough free space to do allocation? As I think PG user has little power to check or free this memory.