pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: zthhhhh (@zthhhhh) <noreply+zthhhhh@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
Date: Mon, 27 Jul 2026 10:22:34 +0000
Message-ID: <gh-pgvector-pgvector-1002@github.com> (raw)

# Problem
For IVFFlat operator classes with a normalization support function, such as `vector_cosine_ops`, `GetScanValue` allocates a normalized query vector in `so->tmpCtx`.

A parameterized index scan can reuse the same `IndexScanDesc` many times. For example, a nested loop can rescan the IVFFlat index once per outer row. Each rescan then allocates another normalized vector, while the previous allocation remains in `so->tmpCtx` until `ivfflatendscan`.

# Fix

Initialize `so->value` when beginning the scan and release an owned normalized value during rescan, if `normprocinfo` is not null.

inline patch 1: src/ivfscan.c (+7/-0 lines) download inline-1.patchdiff --git a/src/ivfscan.c b/src/ivfscan.c index 35b10f34f..ec1f5b24c 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -282,6 +282,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) so->probes = probes; so->maxProbes = maxProbes; so->dimensions = dimensions; + so->value = PointerGetDatum(NULL); /* Set support functions */ so->procinfo = index_getprocinfo(index, 1, IVFFLAT_DISTANCE_PROC); @@ -340,6 +341,12 @@ ivfflatrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int pairingheap_reset(so->listQueue); so->listIndex = 0; + if (so->normprocinfo != NULL && DatumGetPointer(so->value) != NULL) + { + pfree(DatumGetPointer(so->value)); + so->value = PointerGetDatum(NULL); + } + if (keys && scan->numberOfKeys > 0) memmove(scan->keyData, keys, scan->numberOfKeys * sizeof(ScanKeyData));

view thread (12+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-1002@github.com>
Permalink:  ../gh-pgvector-pgvector-1002@github.com/

 ·  · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox