From cbec43afa9015eaa836f5e8277ff43ca4e831519 Mon Sep 17 00:00:00 2001 From: Souhail Hanfi Date: Tue, 26 Mar 2024 02:25:01 +0100 Subject: [PATCH] community[patch]: avoid creating extension PGvector while using readOnly Databases (#19268) - **Description:** PgVector class always runs "create extension" on init and this statement crashes on ReadOnly databases (read only replicas). but wierdly the next create collection etc work even in readOnly databases - **Dependencies:** no new dependencies - **Twitter handle:** @VenOmaX666 Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> --- .../community/langchain_community/vectorstores/pgvector.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/vectorstores/pgvector.py b/libs/community/langchain_community/vectorstores/pgvector.py index fb4bb6bb6af..b7b4cae22b6 100644 --- a/libs/community/langchain_community/vectorstores/pgvector.py +++ b/libs/community/langchain_community/vectorstores/pgvector.py @@ -235,6 +235,8 @@ class PGVector(VectorStore): for querying. It's provided here for backwards compatibility with older versions, and will be removed in the future. + create_extension: If True, will create the vector extension if it doesn't exist. + disabling creation is useful when using ReadOnly Databases. Example: .. code-block:: python @@ -269,6 +271,7 @@ class PGVector(VectorStore): connection: Optional[sqlalchemy.engine.Connection] = None, engine_args: Optional[dict[str, Any]] = None, use_jsonb: bool = False, + create_extension: bool = True, ) -> None: """Initialize the PGVector store.""" self.connection_string = connection_string @@ -283,6 +286,7 @@ class PGVector(VectorStore): self.engine_args = engine_args or {} self._bind = connection if connection else self._create_engine() self.use_jsonb = use_jsonb + self.create_extension = create_extension if not use_jsonb: # Replace with a deprecation warning. @@ -311,7 +315,8 @@ class PGVector(VectorStore): self, ) -> None: """Initialize the store.""" - self.create_vector_extension() + if self.create_extension: + self.create_vector_extension() EmbeddingStore, CollectionStore = _get_embedding_collection_store( self._embedding_length, use_jsonb=self.use_jsonb