Skip to content

Commit c884d7d

Browse files
committed
Merge branch 'postgres_vector_on_demand'
2 parents a2c2505 + 6c00af4 commit c884d7d

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ POSTGRES_MAX_CONNECTIONS=12
405405
### POSTGRES_WORKSPACE=forced_workspace_name
406406

407407
### PostgreSQL Vector Storage Configuration
408+
### Enable/disable vector features (default: true for backward compatibility)
409+
### Set to false to disable pgvector extension and vector operations when using PostgreSQL
410+
### only for KV/Graph/DocStatus storage with a different vector backend (e.g., Milvus, Qdrant)
411+
POSTGRES_ENABLE_VECTOR=true
408412
### Vector storage type: HNSW, IVFFlat, VCHORDRQ
409413
POSTGRES_VECTOR_INDEX_TYPE=HNSW
410414
POSTGRES_HNSW_M=16

lightrag/kg/postgres_impl.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ def __init__(self, config: dict[str, Any], **kwargs: Any):
145145
self.ssl_crl = config.get("ssl_crl")
146146

147147
# Vector configuration
148+
_ev = config.get("enable_vector", True)
149+
self.enable_vector = (
150+
_ev
151+
if isinstance(_ev, bool)
152+
else str(_ev).lower() in ("true", "1", "yes", "on")
153+
) # True for backward compatibility, can be set to False to disable vector features
148154
self.vector_index_type = config.get("vector_index_type")
149155
self.hnsw_m = config.get("hnsw_m")
150156
self.hnsw_ef = config.get("hnsw_ef")
@@ -332,32 +338,35 @@ async def _init_connection(connection: asyncpg.Connection) -> None:
332338
encode/decode vector columns, eliminating non-deterministic behavior
333339
where some connections have the codec and others don't.
334340
"""
335-
await register_vector(connection)
341+
if self.enable_vector:
342+
await register_vector(connection)
336343

337344
async def _create_pool_once() -> None:
338345
# STEP 1: Bootstrap - ensure vector extension exists BEFORE pool creation.
339346
# On a fresh database, register_vector() in _init_connection will fail
340347
# if the vector extension doesn't exist yet, because the 'vector' type
341348
# won't be found in pg_catalog. We must create the extension first
342349
# using a standalone bootstrap connection.
343-
bootstrap_conn = await asyncpg.connect(
344-
user=self.user,
345-
password=self.password,
346-
database=self.database,
347-
host=self.host,
348-
port=self.port,
349-
ssl=connection_params.get("ssl"),
350-
)
351-
try:
352-
await self.configure_vector_extension(bootstrap_conn)
353-
finally:
354-
await bootstrap_conn.close()
350+
# Skip this step if vector support is not enabled.
351+
if self.enable_vector:
352+
bootstrap_conn = await asyncpg.connect(
353+
user=self.user,
354+
password=self.password,
355+
database=self.database,
356+
host=self.host,
357+
port=self.port,
358+
ssl=connection_params.get("ssl"),
359+
)
360+
try:
361+
await self.configure_vector_extension(bootstrap_conn)
362+
finally:
363+
await bootstrap_conn.close()
355364

356365
# STEP 2: Now safe to create pool with register_vector callback.
357-
# The vector extension is guaranteed to exist at this point.
366+
# The vector extension is guaranteed to exist at this point (if enabled).
358367
pool = await asyncpg.create_pool(
359368
**connection_params,
360-
init=_init_connection, # Register pgvector codec on every connection
369+
init=_init_connection, # Register pgvector codec on every connection (if enabled)
361370
) # type: ignore
362371
self.pool = pool
363372

@@ -464,7 +473,7 @@ async def _run_with_retry(
464473
await self.configure_age(connection, graph_name)
465474
elif with_age and not graph_name:
466475
raise ValueError("Graph name is required when with_age is True")
467-
if self.vector_index_type == "VCHORDRQ":
476+
if self.enable_vector and self.vector_index_type == "VCHORDRQ":
468477
await self.configure_vchordrq(connection)
469478
return await operation(connection)
470479

@@ -1750,6 +1759,12 @@ def get_config() -> dict[str, Any]:
17501759
"POSTGRES_SSL_CRL",
17511760
config.get("postgres", "ssl_crl", fallback=None),
17521761
),
1762+
# Vector configuration
1763+
"enable_vector": os.environ.get(
1764+
"POSTGRES_ENABLE_VECTOR",
1765+
config.get("postgres", "enable_vector", fallback="true"),
1766+
).lower()
1767+
in ("true", "1", "yes", "on"),
17531768
"vector_index_type": os.environ.get(
17541769
"POSTGRES_VECTOR_INDEX_TYPE",
17551770
config.get("postgres", "vector_index_type", fallback="HNSW"),
@@ -2828,6 +2843,11 @@ async def initialize(self):
28282843
# Use "default" for compatibility (lowest priority)
28292844
self.workspace = "default"
28302845

2846+
if not self.db.enable_vector:
2847+
raise ValueError(
2848+
"Cannot use PGVectorStorage when POSTGRES_ENABLE_VECTOR=false. Configure an alternative vector backend."
2849+
)
2850+
28312851
# Setup table (create if not exists and handle migration)
28322852
await PGVectorStorage.setup_table(
28332853
self.db,

0 commit comments

Comments
 (0)