agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: PgVectorStore registers the vector type before creating the extension, so the first store on a fresh database can't write

Open littlebird69 opened this issue 1 month ago • 1 comments

Describe the bug

On a database where the vector extension is not installed yet, the first PgVectorStore built against it can't write anything. Every add fails with PSQLException: Unknown type vector.

The constructor does run CREATE EXTENSION IF NOT EXISTS vector (in ensureTable), so the extension does get created. But it registers the pgvector type on the connection before that:

// PgVectorStore constructor
tempConnection = DriverManager.getConnection(jdbcUrl, props);
PGvector.addVectorType(tempConnection);   // extension may not exist yet
...
ensureTable(tempConnection);              // runs CREATE EXTENSION IF NOT EXISTS vector

That connection is kept for the store's lifetime, so this store instance stays broken. A store built afterwards gets a new connection and works, because by then the extension exists.

To Reproduce

  1. Start a database with pgvector available but the extension not created, for example docker run --rm -e POSTGRES_PASSWORD=pw -p 5432:5432 pgvector/pgvector:pg16.
  2. Build a store and add a document:
    PgVectorStore store = PgVectorStore.builder()
            .jdbcUrl("jdbc:postgresql://127.0.0.1:5432/postgres")
            .username("postgres").password("pw")
            .tableName("t1").dimensions(3)
            .build();                       // succeeds; the extension now exists
    store.add(List.of(doc)).block();        // fails
    
  3. Build a second store against the same database and add again: it works.

Expected behavior

The first store works too, since the constructor already takes care of creating the extension.

Error messages

io.agentscope.core.rag.exception.VectorStoreException: Failed to add documents to PostgreSQL
	at io.agentscope.core.rag.store.PgVectorStore.lambda$add$0(PgVectorStore.java:301)
Caused by: org.postgresql.util.PSQLException: Unknown type vector.
	at org.postgresql.jdbc.PgPreparedStatement.setPGobject(PgPreparedStatement.java:527)
	at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1061)
	at io.agentscope.core.rag.store.PgVectorStore.addDocumentsToPostgres(PgVectorStore.java:587)

Environment (please complete the following information):

  • AgentScope-Java Version: 2.1.0-SNAPSHOT (main @ de2dc8f3c)
  • Java Version: 25
  • OS: Windows 11; PostgreSQL 16 with pgvector (pgvector/pgvector:pg16)

Additional context

Found while running a delete contract test against real backends for #3789 (PR #3793). It only shows up on a fresh database. The existing PgVectorStoreTest mocks the connection, so it can't catch it.

Suggested fix: call PGvector.addVectorType after ensureTable. Moving the CREATE EXTENSION statement ahead of addVectorType in the constructor would also work. I'm happy to open a PR.

littlebird69 avatar Oct 27 '26 06:10 littlebird69

I'd like to work on this. I'll open a PR shortly.

littlebird69 avatar Oct 27 '26 06:10 littlebird69