pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
12+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-27 10:22 "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
  0 siblings, 0 replies; 12+ messages in thread

From: zthhhhh (@zthhhhh) @ 2026-07-27 10:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

# 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));
^ permalink raw [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-27 17:17 ` "ankane (@ankane)" <noreply+ankane@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: ankane (@ankane) @ 2026-07-27 17:17 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @zthhhhh, thanks for the PR. Can you share a query where this occurs?

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 02:57 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: zthhhhh (@zthhhhh) @ 2026-07-28 02:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane , thanks for your reply.

> Hi @zthhhhh, thanks for the PR. Can you share a query where this occurs?

Sure, below is an example.
```sql
--  prepare
CREATE TABLE ivf_rescan_items
(
    id        integer PRIMARY KEY,
    embedding vector(2000) NOT NULL
);

INSERT INTO ivf_rescan_items
SELECT i,
       ARRAY(
           SELECT (random() + i * 0)::real
           FROM generate_series(1, 2000)
       )::vector
FROM generate_series(1, 5000) AS i;

CREATE INDEX ivf_rescan_items_embedding_idx
ON ivf_rescan_items
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);

-- query for reproduction. function generate_series can also be a heap relation
SELECT count(*)
FROM generate_series(1, 20000) AS q(id)
CROSS JOIN LATERAL
(
    SELECT i.id
    FROM ivf_rescan_items AS i
    ORDER BY i.embedding <=> (
        ARRAY[(q.id % 100 + 1)::real] ||
        array_fill(1::real, ARRAY[1999])
    )::vector
    LIMIT 1
);
```

The query plan is
```
 Aggregate
   ->  Nested Loop
         ->  Function Scan on generate_series q
         ->  Limit
               ->  Index Scan using ivf_rescan_items_embedding_idx on ivf_rescan_items i
                     Order By: (embedding <=> ((ARRAY[(((q.id % 100) + 1))::real] || '{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}'::real[]))::vector)
```
The inner ivfflat index scan is parameterized by q.id, so Postgresql calls `ivfflatrescan` for every outer row.


# Confirm
To observe the meory usage, I temporarily added the function `MemoryContextStats(so->tmpCtx)`  in function `ivfflatendscan`, the output is shown below.

Before the fix:
```
Ivfflat scan temporary context: 167780352 total in 30 blocks; 3777896 free (221 chunks); 164002456 used
  TupleSort main: 32816 total in 2 blocks; 6848 free (0 chunks); 25968 used
    TupleSort sort: 8192 total in 1 blocks; 7952 free (0 chunks); 240 used
      Caller tuples: 32656 total in 3 blocks; 16176 free; 16480 used
Grand total: 167854016 bytes in 36 blocks; 3808872 free (221 chunks); 164045144 used
```

After applying the fix:
```
Ivfflat scan temporary context: 24576 total in 2 blocks; 15040 free (5 chunks); 9536 used
  TupleSort main: 32816 total in 2 blocks; 6848 free (0 chunks); 25968 used
    TupleSort sort: 8192 total in 1 blocks; 7952 free (0 chunks); 240 used
      Caller tuples: 32656 total in 3 blocks; 16176 free; 16480 used
Grand total: 98240 bytes in 8 blocks; 46016 free (5 chunks); 52224 used
```

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 04:05 ` "ankane (@ankane)" <noreply+ankane@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: ankane (@ankane) @ 2026-07-28 04:05 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks @zthhhhh. Confirmed that fixes it. Rather than casting, I think it'd be better to use `PointerGetDatum(NULL)` and `DatumGetPointer(so->value) != NULL`.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 04:19 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: zthhhhh (@zthhhhh) @ 2026-07-28 04:19 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

OK, I have made the changes, and squashed into a single commit.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 04:24 ` "ankane (@ankane)" <noreply+ankane@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: ankane (@ankane) @ 2026-07-28 04:24 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 04:24 ` "ankane (@ankane)" <noreply+ankane@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: ankane (@ankane) @ 2026-07-28 04:24 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thank you, merged in 93a5b4a16d699eaf890970777c19f19b9d545782.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 05:33 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: zthhhhh (@zthhhhh) @ 2026-07-28 05:33 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane , I noticed that the author email in commit contains a typo: `gamil` should be `gmail`. 

Since the commit was merged recently, would it be possible to correct the author email before more commits are based on it? I understand that this would rewrite the commit hash, so please feel free to decline if rewriteing the branch history is not appropriate.

Sorry for the inconvenience.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 06:04 ` "ankane (@ankane)" <noreply+ankane@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: ankane (@ankane) @ 2026-07-28 06:04 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> please feel free to decline if rewriteing the branch history is not appropriate

I think rewriting the history and force pushing could break things for some users, so it's probably not worth the risk.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 06:11 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: zthhhhh (@zthhhhh) @ 2026-07-28 06:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Understand. Thank for your considering my request.

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 07:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: ankane (@ankane) @ 2026-07-28 07:48 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @zthhhhh, I added some tests in the commit above based on your repro script (with correct email).

^ permalink  raw  [nested|flat] 12+ messages in thread

* Re: [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan
@ 2026-07-28 09:07 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
  10 siblings, 0 replies; 12+ messages in thread

From: zthhhhh (@zthhhhh) @ 2026-07-28 09:07 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Ok, appreciate your work!

^ permalink  raw  [nested|flat] 12+ messages in thread


end of thread, other threads:[~2026-07-28 09:07 UTC | newest]

Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-07-27 10:22 [pgvector/pgvector] PR #1002: Fix memory leak in ivfrescan "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
2026-07-27 17:17 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-07-28 02:57 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
2026-07-28 04:05 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-07-28 04:19 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
2026-07-28 04:24 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-07-28 04:24 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-07-28 05:33 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
2026-07-28 06:04 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-07-28 06:11 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@github.com>
2026-07-28 07:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-07-28 09:07 ` "zthhhhh (@zthhhhh)" <noreply+zthhhhh@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