From: MMeent (@MMeent) <noreply+MMeent@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #692: Memory context leak in vacuum?
Date: Tue, 08 Oct 2024 19:15:24 +0000
Message-ID: <gh-pgvector-pgvector-692@github.com> (raw)
As I looked through the recent changes, I noticed the following curiosity:
In the recent commit (unreleased) 57248ba1, a new memory context is allocated in every call to HnswUpdateNeighborsOnDisk. That function, in turn, is called ~unconditionally from RepairGraphElement in [hnswvacuum](https://github.com/pgvector/pgvector/blob/master/src/hnswvacuum.c#L184), which in turn is called repeatedly from various points during vacuum. It seems to me that this leaks one 128kB generation memory context for every call. While the context is reset at the end of the underlying function, it doesn't get destroyed at the end of HnswUpdateNeighborsOnDisk, and thus the first memory chunk of 128kB is never released; which may lead to OOMs in memory-constrained environments when vacuuming large graphs that have many deletions.
Note that this leak does not outlive its parent's lifetime, but if we assume that that context is the vacuum operation's context then that context may live for a long time, so it'd be preferred if the generation context was reused and not leaked.