Class PgVectorStore

java.lang.Object
io.agentscope.core.rag.store.PgVectorStore
All Implemented Interfaces:
VDBStoreBase, AutoCloseable

public class PgVectorStore extends Object implements VDBStoreBase, AutoCloseable
PostgreSQL pgvector database store implementation.

This class provides an interface for storing and searching vectors using PostgreSQL with the pgvector extension. It implements the VDBStoreBase interface to provide a unified API for vector storage operations.

In PostgreSQL with pgvector, we use a table to store the vectors and metadata, including the document ID, chunk ID, content, and custom payload as JSONB.

This implementation uses the pgvector JDBC driver (com.pgvector:pgvector). To use this class, add the following dependencies:


 <dependency>
     <groupId>org.postgresql</groupId>
     <artifactId>postgresql</artifactId>
     <version>42.7.4</version>
 </dependency>
 <dependency>
     <groupId>com.pgvector</groupId>
     <artifactId>pgvector</artifactId>
     <version>0.1.6</version>
 </dependency>
 

Prerequisites:

  • PostgreSQL 11+ with pgvector extension installed
  • Run: CREATE EXTENSION IF NOT EXISTS vector;

Example usage:


 // Using builder with full configuration (recommended)
 try (PgVectorStore store = PgVectorStore.builder()
         .jdbcUrl("jdbc:postgresql://localhost:5432/mydb")
         .username("postgres")
         .password("password")
         .schema("my_schema")  // optional, defaults to "public"
         .tableName("embeddings")
         .dimensions(1024)
         .distanceType(PgVectorStore.DistanceType.COSINE)
         .build()) {
     store.add(documents).block();
     List<Document> results = store.search(searchDto).block();
 }

 // Using static factory method (simpler, uses defaults)
 try (PgVectorStore store = PgVectorStore.create(
         "jdbc:postgresql://localhost:5432/mydb",
         "postgres", "password", "embeddings", 1024)) {
     store.add(documents).block();
     List<Document> results = store.search(searchDto).block();
 }
 

Note: The JDBC URL should follow PostgreSQL format:

  • jdbc:postgresql://host:port/database
  • jdbc:postgresql://localhost:5432/vectordb
  • Method Details

    • create

      public static PgVectorStore create(String jdbcUrl, String username, String password, String tableName, int dimensions) throws VectorStoreException
      Creates a new PgVectorStore with minimal configuration.
      Parameters:
      jdbcUrl - the PostgreSQL JDBC URL (e.g., "jdbc:postgresql://localhost:5432/mydb")
      username - the database username
      password - the database password
      tableName - the name of the table to use
      dimensions - the dimension of vectors that will be stored
      Returns:
      a new PgVectorStore instance
      Throws:
      VectorStoreException - if initialization fails
    • add

      public reactor.core.publisher.Mono<Void> add(List<io.agentscope.core.rag.model.Document> documents)
      Description copied from interface: VDBStoreBase
      Adds multiple documents to the store.

      All documents must have embedding vectors set. The store will persist both the vectors and the document metadata/payloads.

      This method supports batch operations for efficiency. Implementations should handle batch insertion optimally.

      Specified by:
      add in interface VDBStoreBase
      Parameters:
      documents - the list of documents to store (all must have embeddings set)
      Returns:
      a Mono that completes when all documents are stored
    • search

      public reactor.core.publisher.Mono<List<io.agentscope.core.rag.model.Document>> search(SearchDocumentDto searchDocumentDto)
      Description copied from interface: VDBStoreBase
      Searches for similar documents.

      This method performs vector similarity search and returns the most similar documents. Results include the document metadata and similarity scores. Optionally, embeddings may also be included in the results (implementation-dependent).

      Specified by:
      search in interface VDBStoreBase
      Parameters:
      searchDocumentDto - the search document DTO
      Returns:
      a Mono that emits a list of Document objects with scores set, sorted by similarity (descending)
    • delete

      public reactor.core.publisher.Mono<Boolean> delete(String docId)
      Description copied from interface: VDBStoreBase
      Deletes a document from the store (optional).

      Default implementation throws UnsupportedOperationException. Vector stores that support deletion should override this method.

      Specified by:
      delete in interface VDBStoreBase
      Parameters:
      docId - the document ID (UUID string) to delete
      Returns:
      a Mono that emits true if the deletion was successful, false otherwise
    • getConnection

      public Connection getConnection() throws VectorStoreException
      Gets the underlying JDBC connection for advanced operations.

      This method provides access to the connection for users who need to perform operations beyond the standard VDBStoreBase interface, such as:

      • Custom index configuration
      • Advanced filtering
      • Table management
      • Batch operations
      Returns:
      the Connection instance
      Throws:
      VectorStoreException - if the store has been closed
    • getJdbcUrl

      public String getJdbcUrl()
      Gets the JDBC URL.
      Returns:
      the JDBC URL
    • getSchema

      public String getSchema()
      Gets the schema name.
      Returns:
      the schema name
    • getTableName

      public String getTableName()
      Gets the table name.
      Returns:
      the table name
    • getDimensions

      public int getDimensions()
      Gets the vector dimensions.
      Returns:
      the dimensions
    • getDistanceType

      public PgVectorStore.DistanceType getDistanceType()
      Gets the distance type used for similarity search.
      Returns:
      the distance type
    • close

      public void close()
      Closes the PostgreSQL connection and releases all resources.

      This method closes the JDBC connection, releasing all database resources. This method is idempotent and can be called multiple times safely.

      After closing, all operations on this store will fail with a VectorStoreException. It's recommended to use try-with-resources for automatic resource management:

      
       try (PgVectorStore store = PgVectorStore.builder()
               .jdbcUrl("jdbc:postgresql://localhost:5432/mydb")
               .tableName("embeddings")
               .dimensions(1024)
               .build()) {
           store.add(documents).block();
           // Store is automatically closed here
       }
       
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Checks if this store has been closed.
      Returns:
      true if the store has been closed, false otherwise
    • builder

      public static PgVectorStore.Builder builder()
      Creates a new builder for constructing PgVectorStore instances.
      Returns:
      a new builder instance