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>
This commit is contained in:
Souhail Hanfi 2024-03-26 02:25:01 +01:00 committed by GitHub
parent 903541f439
commit cbec43afa9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -235,6 +235,8 @@ class PGVector(VectorStore):
for querying. for querying.
It's provided here for backwards compatibility with older versions, It's provided here for backwards compatibility with older versions,
and will be removed in the future. 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: Example:
.. code-block:: python .. code-block:: python
@ -269,6 +271,7 @@ class PGVector(VectorStore):
connection: Optional[sqlalchemy.engine.Connection] = None, connection: Optional[sqlalchemy.engine.Connection] = None,
engine_args: Optional[dict[str, Any]] = None, engine_args: Optional[dict[str, Any]] = None,
use_jsonb: bool = False, use_jsonb: bool = False,
create_extension: bool = True,
) -> None: ) -> None:
"""Initialize the PGVector store.""" """Initialize the PGVector store."""
self.connection_string = connection_string self.connection_string = connection_string
@ -283,6 +286,7 @@ class PGVector(VectorStore):
self.engine_args = engine_args or {} self.engine_args = engine_args or {}
self._bind = connection if connection else self._create_engine() self._bind = connection if connection else self._create_engine()
self.use_jsonb = use_jsonb self.use_jsonb = use_jsonb
self.create_extension = create_extension
if not use_jsonb: if not use_jsonb:
# Replace with a deprecation warning. # Replace with a deprecation warning.
@ -311,7 +315,8 @@ class PGVector(VectorStore):
self, self,
) -> None: ) -> None:
"""Initialize the store.""" """Initialize the store."""
self.create_vector_extension() if self.create_extension:
self.create_vector_extension()
EmbeddingStore, CollectionStore = _get_embedding_collection_store( EmbeddingStore, CollectionStore = _get_embedding_collection_store(
self._embedding_length, use_jsonb=self.use_jsonb self._embedding_length, use_jsonb=self.use_jsonb