It should be possible to specify a custom name for the table used by PgVectorStore, which is currently hardcoded to vector_store. The associated index name could default to being generated based on the name while also allowing custom specification.

PgVectorStore is effectively a database singleton. This is not Spring-like.

Concrete reasons this would be desirable:

  • It should be possible to have multiple vector stores in the same database.
  • It should be possible to have a vector store within a schema. It's good practice for an app's tables to be within a schema.

Comment From: akash4chandran

Hi, I faced a similar challenge while working on a project using the Spring AI framework with the PgVectorStore. By default, PgVectorStore uses the vector_store table, but I needed to use a custom table name.

Here's how I managed to use a custom table within my custom schema:

CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE SEQUENCE custom_schema.custom_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

DROP TABLE IF EXISTS custom_schema.custom_table;
CREATE TABLE IF NOT EXISTS custom_schema.custom_table (
    id int8 NOT NULL UNIQUE default NEXTVAL('custom_schema.custom_seq'),
    unique_id UUID NOT null UNIQUE,
    content text,
    metadata json,
    embedding vector(1536),
    created_on TIMESTAMPTZ,
    modified_on TIMESTAMPTZ,
    CONSTRAINT custom_pkey PRIMARY KEY (id)
);

Using Hibernate for Custom Entity

To work with this custom table using Hibernate, you can use the hibernate-vector dependency to map your custom entity. Here’s how you can configure it:

<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-vector</artifactId>
    <version>6.4.0.Final</version>
</dependency>

This setup allows you to use your custom table with PgVectorStore and Hibernate. For more details on using and configuring PgVectorStore, refer to

Feel free to reach out if you have any questions or need further assistance!

Comment From: muthuishere

I have been closely following this issue regarding enabling custom table names for the PGvector Store, and I’ve developed a solution that I believe addresses the requirements discussed here.

The solution includes: - Adding properties to configure custom table and index names. - Modifying the PgVectorStore class to use these configurable properties. - Ensuring backward compatibility with setups using default settings.

I have thoroughly tested these changes to ensure they work seamlessly with both new configurations and existing deployments. I will be submitting the PR for the same

Comment From: tzolov

Hi @johnsonr ,

Regarding the

It should be possible to have multiple vector stores in the same database. It should be possible to have a vector store within a schema. It's good practice for an app's tables to be within a schema.

The reason for not providing parametrisable table name/schema/index is that , for convenience, we pre-create the table when not available. And we cannot afford dynamic DDLs as no support for prepared DDL is provided.

We could either drop the pre-creation all together or run it only when no custom table/schema/index were provided. For the later the user would be responsible to create/provide the tables. Which is expected for prod environments anyway.

Would this make sense?

Comment From: muthuishere

@tzolov Without this modification, a user will be able to create only one table in any given database. If we want to accommodate multiple embeddings of different dimensions, the user would need to have separate databases. This is because the table name remains constant in any given database.

I could see two options:

  1. Add a table name validation using matches("^[a-zA-Z0-9_]+$"). to ensure no SQL injection is there

  2. Upon receiving the property spring.ai.vectorstore.pgvector.vectorTableName, ensure the table exists and the fields are available as the same name, or we can throw an exception. if its not specified use the existing flow

We could either drop the pre-creation all together or run it only when no custom table/schema/index were provided. For the later the user would be responsible to create/provide the tables. Which is expected for prod environments anyway.

I could see you have already mentioned , if this is okay , I will update my PR accordingly

Comment From: muthuishere

@tzolov

PR https://github.com/spring-projects/spring-ai/pull/872 has been updated with commits to introduce the ability to specify custom table name for PgVectorStore, and stop pre create tables when custom table names are provided. also added tablename validations, field validations to ensure things are okay

Key changes include: - Adding properties to configure custom table names. - Modifying PgVectorStore to conditionally pre-create tables only when custom table names are not provided. - Implementing table & field validation for custom configurations. - Extensive tests to ensure compatibility with both new configurations and existing deployments.

With these changes, automatic table pre-creation is removed, and users are expected to manage table setup in custom scenarios, consistent with production environment practices.