register_vector leaks dumper classes on every call
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 55/100
Research direction
Start with register_vector_info and the dumper classes in pgvector/psycopg/vector.py, then compare their registration with psycopg’s hstore.py example linked in the issue. Use the supplied reproduction with psycopg’s C extension to check class counts before and after repeated calls; done means the dumper classes are no longer retained after contexts are deleted.
Written by the indexing model from the issue text.
Description
Hi!
Investigating a memory leak in our API, we found out that register_vector (and register_vector_async) creates 8 classes on every call that are never garbage collected if psycopg's C extension is in use, even after the connection is closed.
This is because these classes are stored by psycopg in a dict shared by the whole python process, and never cleared.
pgvector python 0.4.2 (but the code has not been updated on master) and psycopg 3.3.2
To reproduce:
import gc
from types import SimpleNamespace
import psycopg
from pgvector.psycopg.vector import (
VectorBinaryDumper,
VectorDumper,
register_vector_info,
)
from psycopg.adapt import AdaptersMap
from psycopg.types import TypeInfo
def count() -> int:
gc.collect()
return sum(
1
for base in (VectorDumper, VectorBinaryDumper)
for c in base.__subclasses__()
if c.__name__ == ""
)
print(psycopg.__version__, psycopg.pq.__impl__)
before = count()
for _ in range(1000):
ctx = SimpleNamespace(adapters=AdaptersMap(psycopg.adapters))
register_vector_info(ctx, TypeInfo("vector", 16385, 16386))
del ctx
print("leaked dumper classes:", count() - before) # expected 0, prints 2000
Caching these classes as done by psycopg could fix the issue : https://github.com/psycopg/psycopg/blob/99356800e90aa0e7b604f652d714ef0f33422b47/psycopg/psycopg/types/hstore.py#L225
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 100
- Avg merge
- 13m
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pgvector/pgvector-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pgvector/pgvector-python#169 ·
All issues in pgvector/pgvector-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 1 day
-
ChaosEngine: backup-remote doctor row passes while consumer mode hides bot memory from gitPossibly taken @MohabMohie claimed this today. Openagentic
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ShaftHQ/SHAFT_ENGINE#7263 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 87/100
huggingface/smolagents#3045 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day