diff --git a/libs/langchain-community/src/vectorstores/pgvector.ts b/libs/langchain-community/src/vectorstores/pgvector.ts index 6a58c4ba5cd0..8f4e564f2caf 100644 --- a/libs/langchain-community/src/vectorstores/pgvector.ts +++ b/libs/langchain-community/src/vectorstores/pgvector.ts @@ -685,6 +685,7 @@ export class PGVectorStore extends VectorStore { * @param m - The max number of connections per layer (16 by default). Index build time improves with smaller values, while higher values can speed up search queries. * @param efConstruction - The size of the dynamic candidate list for constructing the graph (64 by default). A higher value can potentially improve the index quality at the cost of index build time. * @param distanceFunction - The distance function name you want to use, is automatically selected based on the distanceStrategy. + * @param namespace - The namespace is used to create the index with a specific name. This is useful when you want to create multiple indexes on the same database schema (within the same schema in PostgreSQL, the index name must be unique across all tables). * @returns Promise that resolves with the query response of creating the index. */ async createHnswIndex(config: { @@ -692,8 +693,10 @@ export class PGVectorStore extends VectorStore { m?: number; efConstruction?: number; distanceFunction?: string; + namespace?: string; }): Promise { let idxDistanceFunction = config?.distanceFunction || "vector_cosine_ops"; + const prefix = config?.namespace ? `${config.namespace}_` : ""; switch (this.distanceStrategy) { case "cosine": @@ -709,7 +712,7 @@ export class PGVectorStore extends VectorStore { throw new Error(`Unknown distance strategy: ${this.distanceStrategy}`); } - const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${ + const createIndexQuery = `CREATE INDEX IF NOT EXISTS ${prefix}${ this.vectorColumnName }_embedding_hnsw_idx ON ${this.computedTableName} USING hnsw ((${