PGEngine#

class langchain_postgres.v2.engine.PGEngine(
key: object,
pool: AsyncEngine,
loop: AbstractEventLoop | None,
thread: Thread | None,
)[source]#

A class for managing connections to a Postgres database.

PGEngine constructor.

Parameters:
  • key (object) – Prevent direct constructor usage.

  • pool (AsyncEngine) – Async engine connection pool.

  • loop (Optional[asyncio.AbstractEventLoop]) – Async event loop used to create the engine.

  • thread (Optional[Thread]) – Thread used to create the engine async.

Raises:

Exception – If the constructor is called directly by the user.

Methods

__init__(key, pool, loop, thread)

PGEngine constructor.

adrop_table(table_name, *[, schema_name])

ainit_vectorstore_table(table_name, ...[, ...])

Create a table for saving of vectors to be used with PGVectorStore.

close()

Dispose of connection pool

drop_table(table_name, *[, schema_name])

from_connection_string(url, **kwargs)

Create an PGEngine instance from arguments

from_engine(engine[, loop])

Create an PGEngine instance from an AsyncEngine.

init_vectorstore_table(table_name, ...[, ...])

Create a table for saving of vectors to be used with PGVectorStore.

__init__(
key: object,
pool: AsyncEngine,
loop: AbstractEventLoop | None,
thread: Thread | None,
) None[source]#

PGEngine constructor.

Parameters:
  • key (object) – Prevent direct constructor usage.

  • pool (AsyncEngine) – Async engine connection pool.

  • loop (Optional[asyncio.AbstractEventLoop]) – Async event loop used to create the engine.

  • thread (Optional[Thread]) – Thread used to create the engine async.

Raises:

Exception – If the constructor is called directly by the user.

Return type:

None

async adrop_table(
table_name: str,
*,
schema_name: str = 'public',
) None[source]#
Parameters:
  • table_name (str)

  • schema_name (str)

Return type:

None

async ainit_vectorstore_table(
table_name: str,
vector_size: int,
*,
schema_name: str = 'public',
content_column: str = 'content',
embedding_column: str = 'embedding',
metadata_columns: list[Column | ColumnDict] | None = None,
metadata_json_column: str = 'langchain_metadata',
id_column: str | Column | ColumnDict = 'langchain_id',
overwrite_existing: bool = False,
store_metadata: bool = True,
) None[source]#

Create a table for saving of vectors to be used with PGVectorStore.

Parameters:
  • table_name (str) – The database table name.

  • vector_size (int) – Vector size for the embedding model to be used.

  • schema_name (str) – The schema name. Default: “public”.

  • content_column (str) – Name of the column to store document content. Default: “page_content”.

  • embedding_column (str) – Name of the column to store vector embeddings. Default: “embedding”.

  • metadata_columns (Optional[list[Union[Column, ColumnDict]]]) – A list of Columns to create for custom metadata. Default: None. Optional.

  • metadata_json_column (str) – The column to store extra metadata in JSON format. Default: “langchain_metadata”. Optional.

  • id_column (Union[str, Column, ColumnDict]) – Column to store ids. Default: “langchain_id” column name with data type UUID. Optional.

  • overwrite_existing (bool) – Whether to drop existing table. Default: False.

  • store_metadata (bool) – Whether to store metadata in the table. Default: True.

Return type:

None

async close() None[source]#

Dispose of connection pool

Return type:

None

async drop_table(
table_name: str,
*,
schema_name: str = 'public',
) None[source]#
Parameters:
  • table_name (str)

  • schema_name (str)

Return type:

None

classmethod from_connection_string(
url: str | URL,
**kwargs: Any,
) PGEngine[source]#

Create an PGEngine instance from arguments

Parameters:
  • url (Optional[str]) – the URL used to connect to a database. Use url or set other arguments.

  • kwargs (Any)

Raises:

ValueError – If not all database url arguments are specified

Returns:

PGEngine

Return type:

PGEngine

classmethod from_engine(
engine: AsyncEngine,
loop: AbstractEventLoop | None = None,
) PGEngine[source]#

Create an PGEngine instance from an AsyncEngine.

Parameters:
  • engine (AsyncEngine)

  • loop (AbstractEventLoop | None)

Return type:

PGEngine

init_vectorstore_table(
table_name: str,
vector_size: int,
*,
schema_name: str = 'public',
content_column: str = 'content',
embedding_column: str = 'embedding',
metadata_columns: list[Column | ColumnDict] | None = None,
metadata_json_column: str = 'langchain_metadata',
id_column: str | Column | ColumnDict = 'langchain_id',
overwrite_existing: bool = False,
store_metadata: bool = True,
) None[source]#

Create a table for saving of vectors to be used with PGVectorStore.

Parameters:
  • table_name (str) – The database table name.

  • vector_size (int) – Vector size for the embedding model to be used.

  • schema_name (str) – The schema name. Default: “public”.

  • content_column (str) – Name of the column to store document content. Default: “page_content”.

  • embedding_column (str) – Name of the column to store vector embeddings. Default: “embedding”.

  • metadata_columns (Optional[list[Union[Column, ColumnDict]]]) – A list of Columns to create for custom metadata. Default: None. Optional.

  • metadata_json_column (str) – The column to store extra metadata in JSON format. Default: “langchain_metadata”. Optional.

  • id_column (Union[str, Column, ColumnDict]) – Column to store ids. Default: “langchain_id” column name with data type UUID. Optional.

  • overwrite_existing (bool) – Whether to drop existing table. Default: False.

  • store_metadata (bool) – Whether to store metadata in the table. Default: True.

Return type:

None