ann_router.backends.pgvector_backend module
pgvector backend — vector search inside an existing PostgreSQL.
pgvector’s niche is persistence + metadata filters when a Postgres is already
in place: you keep vectors next to your relational data and filter with plain
SQL WHERE clauses, backed by an HNSW index on the vector column. The router
picks it (over Qdrant) when the criteria say “a database is already there”,
because reusing it beats standing up a second datastore. Unlike every other
backend this one needs a live server, so it is unavailable unless a DSN is
supplied — absence is a clean skip, never a crash.
Consumes: pgvector + psycopg (optional, pip install 'ann-router[pgvector]')
and a reachable PostgreSQL with the vector extension.
Produces: PgVectorIndex.
Author: Warith Harchaoui <warith.harchaoui@deraison.ai>
- class ann_router.backends.pgvector_backend.PgVectorIndex(dim, metric='cosine', **kwargs)[source]
Bases:
ANNIndexA single vectors table in PostgreSQL, indexed with pgvector HNSW.
Requires a DSN (
dsn=kwarg or theANN_ROUTER_PG_DSNenv var). Points live in a(id bigint, embedding vector(dim), payload jsonb)table so the SQLWHEREpath can filter onpayload.- Parameters:
Examples
>>> PgVectorIndex.capabilities().persistent True
- add(vectors)[source]
Append vectors with ids continuing past the current max.
- Parameters:
vectors (numpy.ndarray) – Shape
(m, dim).- Return type:
None
- add_with_ids(vectors, ids)[source]
Append vectors with explicit ids.
- Parameters:
vectors (numpy.ndarray) – Shape
(m, dim).ids (numpy.ndarray) – Shape
(m,)integer ids.
- Return type:
None
- build(vectors, ids=None, payloads=None)[source]
(Re)create the table, insert the corpus, and build the HNSW index.
- Parameters:
- Returns:
self.- Return type:
- classmethod capabilities()[source]
Return the pgvector capability descriptor (persistent + filterable).
- Return type:
- classmethod is_available()[source]
Return
Trueif psycopg + pgvector import (a live DSN is still needed).Examples
>>> isinstance(PgVectorIndex.is_available(), bool) True
- Return type:
- load(path)[source]
Reconnect to the existing table (
pathmay override the DSN).- Parameters:
path (str) – A DSN to reconnect with, or falsy to reuse the constructor’s DSN.
- Returns:
self, reconnected.- Return type:
- remove(ids)[source]
Delete rows by id.
- Parameters:
ids (numpy.ndarray) – Shape
(m,)integer ids to drop.- Return type:
None
- save(path)[source]
No-op — the table lives in PostgreSQL and is already durable.
- Parameters:
path (str) – Unused — accepted only to satisfy the shared interface.
- Return type:
None
Notes
Persistence is the database’s job; there is nothing to serialise. Use the same DSN + table to
load()the index in another process.
- search_filter(queries, k, where=None)[source]
Return top-
kneighbours, optionally filtered by apayloadclause.- Parameters:
- Returns:
ids (numpy.ndarray) – Shape
(q, k)(-1pads short rows).distances (numpy.ndarray) – Shape
(q, k)distances under the metric operator.
- Return type:
tuple[ndarray, ndarray]