Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(pgvector): address Copilot review feedback
Sameer6305b473285dcb71
diff --git a/pyproject.toml b/pyproject.tomlindex 4491c28e..bc1f4d6a 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -128,7 +128,7 @@ vectorstore-qdrant = ["qdrant-client>=1.0.0"]vectorstore-weaviate = ["weaviate-client>=4.0.0"]vectorstore-pinecone = ["pinecone-client>=3.0.0"]vectorstore-milvus = ["pymilvus>=2.0.0"]-vectorstore-pgvector = ["psycopg[binary,pool]>=3.0.0", "psycopg2-binary>=2.9.0", "pgvector>=0.2.0"]+vectorstore-pgvector = ["psycopg[binary,pool]>=3.0.0", "pgvector>=0.2.0"]vectorstore-all = ["semantica[vectorstore-qdrant,vectorstore-weaviate,vectorstore-pinecone,vectorstore-milvus,vectorstore-pgvector]"diff --git a/semantica/vector_store/pgvector_store.py b/semantica/vector_store/pgvector_store.pyindex aea3642d..6a646cd8 100644--- a/semantica/vector_store/pgvector_store.py+++ b/semantica/vector_store/pgvector_store.py@@ -313,33 +313,26 @@ class PgVectorStore:f"Metadata length ({len(metadata)}) must match vectors length ({num_vectors})")- # Batch insert using safe SQL composition- if PSYCOPG3_AVAILABLE:- insert_sql = psycopg_sql.SQL("""- INSERT INTO {} (id, vector, metadata)- VALUES (%s, %s, %s)- ON CONFLICT (id) DO UPDATE SET- vector = EXCLUDED.vector,- metadata = EXCLUDED.metadata- """).format(psycopg_sql.Identifier(self.table_name))- else:- insert_sql = """- INSERT INTO {} (id, vector, metadata)- VALUES (%s, %s, %s)- ON CONFLICT (id) DO UPDATE SET- vector = EXCLUDED.vector,- metadata = EXCLUDED.metadata- """.format(psycopg_sql.Identifier(self.table_name))+ # Batch insert using safe SQL composition with executemany() for efficiency+ # Build data tuples for batch insert+ data_tuples = [+ (vec_id, vec.tolist() if isinstance(vec, np.ndarray) else list(vec), json.dumps(meta))+ for vec_id, vec, meta in zip(ids, vectors, metadata)+ ]++ insert_sql = psycopg_sql.SQL("""+ INSERT INTO {} (id, vector, metadata)+ VALUES (%s, %s, %s)+ ON CONFLICT (id) DO UPDATE SET+ vector = EXCLUDED.vector,+ metadata = EXCLUDED.metadata+ """).format(psycopg_sql.Identifier(self.table_name))with self._get_connection() as conn:try:cur = conn.cursor()- for vec_id, vec, meta in zip(ids, vectors, metadata):- vec_list = vec.tolist() if isinstance(vec, np.ndarray) else list(vec)- if PSYCOPG3_AVAILABLE:- cur.execute(insert_sql, (vec_id, vec_list, json.dumps(meta)))- else:- cur.execute(insert_sql, (vec_id, vec_list, json.dumps(meta)))+ # Use executemany for batch insert optimization+ cur.executemany(insert_sql, data_tuples)conn.commit()cur.close()self.logger.info(f"Added {num_vectors} vectors")@@ -406,62 +399,34 @@ class PgVectorStore:where_clause = psycopg_sql.SQL(" AND ").join(filter_conditions)- if PSYCOPG3_AVAILABLE:- search_sql = psycopg_sql.SQL("""- SELECT id, vector {op} %s::vector AS score, metadata- FROM {table}- WHERE {where}- ORDER BY vector {op} %s::vector- LIMIT %s- """).format(- op=psycopg_sql.SQL(op),- table=psycopg_sql.Identifier(self.table_name),- where=where_clause- )- else:- # psycopg2: format table name, embed operator directly- search_sql = psycopg_sql.SQL("""- SELECT id, vector {op} %s::vector AS score, metadata- FROM {table}- WHERE {where}- ORDER BY vector {op} %s::vector- LIMIT %s- """).format(- op=psycopg_sql.SQL(op),- table=psycopg_sql.Identifier(self.table_name),- where=where_clause- )+ search_sql = psycopg_sql.SQL("""+ SELECT id, vector {op} %s::vector AS score, metadata+ FROM {table}+ WHERE {where}+ ORDER BY vector {op} %s::vector+ LIMIT %s+ """).format(+ op=psycopg_sql.SQL(op),+ table=psycopg_sql.Identifier(self.table_name),+ where=where_clause+ )params = [query_list] + filter_values + [query_list, top_k]else:- if PSYCOPG3_AVAILABLE:- search_sql = psycopg_sql.SQL("""- SELECT id, vector {op} %s::vector AS score, metadata- FROM {table}- ORDER BY vector {op} %s::vector- LIMIT %s- """).format(- op=psycopg_sql.SQL(op),- table=psycopg_sql.Identifier(self.table_name)- )- else:- search_sql = psycopg_sql.SQL("""- SELECT id, vector {op} %s::vector AS score, metadata- FROM {table}- ORDER BY vector {op} %s::vector- LIMIT %s- """).format(- op=psycopg_sql.SQL(op),- table=psycopg_sql.Identifier(self.table_name)- )+ search_sql = psycopg_sql.SQL("""+ SELECT id, vector {op} %s::vector AS score, metadata+ FROM {table}+ ORDER BY vector {op} %s::vector+ LIMIT %s+ """).format(+ op=psycopg_sql.SQL(op),+ table=psycopg_sql.Identifier(self.table_name)+ )params = [query_list, query_list, top_k]with self._get_connection() as conn:try:cur = conn.cursor()- if PSYCOPG3_AVAILABLE:- cur.execute(search_sql, params)- else:- cur.execute(search_sql, params)+ cur.execute(search_sql, params)rows = cur.fetchall()cur.close()@@ -520,22 +485,14 @@ class PgVectorStore:return True# Use safe SQL composition for table name- if PSYCOPG3_AVAILABLE:- delete_sql = psycopg_sql.SQL("DELETE FROM {} WHERE id = ANY(%s)").format(- psycopg_sql.Identifier(self.table_name)- )- else:- delete_sql = psycopg_sql.SQL("DELETE FROM {} WHERE id = ANY(%s)").format(- psycopg_sql.Identifier(self.table_name)- )+ delete_sql = psycopg_sql.SQL("DELETE FROM {} WHERE id = ANY(%s)").format(+ psycopg_sql.Identifier(self.table_name)+ )with self._get_connection() as conn:try:cur = conn.cursor()- if PSYCOPG3_AVAILABLE:- cur.execute(delete_sql, (ids,))- else:- cur.execute(delete_sql, (ids,))+ cur.execute(delete_sql, (ids,))conn.commit()deleted_count = cur.rowcountcur.close()@@ -616,10 +573,7 @@ class PgVectorStore:psycopg_sql.SQL(", ").join(updates))params.append(vec_id)- if PSYCOPG3_AVAILABLE:- cur.execute(update_sql, params)- else:- cur.execute(update_sql, params)+ cur.execute(update_sql, params)conn.commit()cur.close()@@ -649,26 +603,16 @@ class PgVectorStore:return []# Use safe SQL composition for table name- if PSYCOPG3_AVAILABLE:- get_sql = psycopg_sql.SQL("""- SELECT id, vector, metadata- FROM {}- WHERE id = ANY(%s)- """).format(psycopg_sql.Identifier(self.table_name))- else:- get_sql = psycopg_sql.SQL("""- SELECT id, vector, metadata- FROM {}- WHERE id = ANY(%s)- """).format(psycopg_sql.Identifier(self.table_name))+ get_sql = psycopg_sql.SQL("""+ SELECT id, vector, metadata+ FROM {}+ WHERE id = ANY(%s)+ """).format(psycopg_sql.Identifier(self.table_name))with self._get_connection() as conn:try:cur = conn.cursor()- if PSYCOPG3_AVAILABLE:- cur.execute(get_sql, (ids,))- else:- cur.execute(get_sql, (ids,))+ cur.execute(get_sql, (ids,))rows = cur.fetchall()cur.close()@@ -818,10 +762,7 @@ class PgVectorStore:drop_sql = psycopg_sql.SQL("DROP INDEX IF EXISTS {}").format(psycopg_sql.Identifier(index_name))- if PSYCOPG3_AVAILABLE:- cur.execute(drop_sql)- else:- cur.execute(drop_sql)+ cur.execute(drop_sql)conn.commit()cur.close()self.logger.info(f"Dropped index: {index_name}")@@ -848,10 +789,7 @@ class PgVectorStore:count_sql = psycopg_sql.SQL("SELECT COUNT(*) FROM {}").format(psycopg_sql.Identifier(self.table_name))- if PSYCOPG3_AVAILABLE:- cur.execute(count_sql)- else:- cur.execute(count_sql)+ cur.execute(count_sql)count = cur.fetchone()[0]# Get index infodiff --git a/tests/vector_store/test_pgvector_store.py b/tests/vector_store/test_pgvector_store.pyindex 3f134e43..fdcb5ace 100644--- a/tests/vector_store/test_pgvector_store.py+++ b/tests/vector_store/test_pgvector_store.py@@ -100,7 +100,7 @@ def store(pg_available, unique_table_name) -> Generator:if not pg_available:pytest.skip("PostgreSQL with pgvector not available")- from semantica.vector_store.pgvector_store import PgVectorStore+ from semantica.vector_store.pgvector_store import PgVectorStore, psycopg_sqlstore = PgVectorStore(connection_string=TEST_CONNECTION_STRING,@@ -112,15 +112,22 @@ def store(pg_available, unique_table_name) -> Generator:yield store- # Cleanup+ # Cleanup: Drop test table after test completes+ # Uses best-effort cleanup - failures are silently ignored since+ # this is teardown of optional test resources, not core functionalitytry:with store._get_connection() as conn:cur = conn.cursor()- cur.execute(f"DROP TABLE IF EXISTS {unique_table_name}")+ drop_sql = psycopg_sql.SQL("DROP TABLE IF EXISTS {}").format(+ psycopg_sql.Identifier(unique_table_name)+ )+ cur.execute(drop_sql)conn.commit()cur.close()store.close()except Exception:+ # Best-effort cleanup: PostgreSQL may be unavailable during teardown+ # This is expected when tests are skipped or connection is lostpass@@ -297,15 +304,23 @@ class TestPgVectorStoreSearch:assert len(results) == 0- # Cleanup+ # Cleanup: Drop test table after test completes+ # Uses best-effort cleanup - failures are silently ignored since+ # this is teardown of optional test resourcestry:with empty_store._get_connection() as conn:cur = conn.cursor()- cur.execute(f"DROP TABLE IF EXISTS {unique_table_name}")+ from semantica.vector_store.pgvector_store import psycopg_sql+ drop_sql = psycopg_sql.SQL("DROP TABLE IF EXISTS {}").format(+ psycopg_sql.Identifier(unique_table_name)+ )+ cur.execute(drop_sql)conn.commit()cur.close()empty_store.close()except Exception:+ # Best-effort cleanup: PostgreSQL may be unavailable during teardown+ # This is expected when tests are skipped or connection is lostpass@@ -557,7 +572,7 @@ class TestPgVectorStoreDistanceMetrics:if not pg_available:pytest.skip("PostgreSQL not available")- from semantica.vector_store.pgvector_store import PgVectorStore+ from semantica.vector_store.pgvector_store import PgVectorStore, psycopg_sqlstore = PgVectorStore(connection_string=TEST_CONNECTION_STRING,@@ -569,15 +584,22 @@ class TestPgVectorStoreDistanceMetrics:yield store- # Cleanup+ # Cleanup: Drop test table after test completes+ # Uses best-effort cleanup - failures are silently ignored since+ # this is teardown of optional test resources, not core functionalitytry:with store._get_connection() as conn:cur = conn.cursor()- cur.execute(f"DROP TABLE IF EXISTS {store.table_name}")+ drop_sql = psycopg_sql.SQL("DROP TABLE IF EXISTS {}").format(+ psycopg_sql.Identifier(store.table_name)+ )+ cur.execute(drop_sql)conn.commit()cur.close()store.close()except Exception:+ # Best-effort cleanup: PostgreSQL may be unavailable during teardown+ # This is expected when tests are skipped or connection is lostpassdef test_l2_distance_search(self, l2_store):