The PgVectorStore implementation of the Vector Store keeps creating a new index on the embedding column even if an index on that column already exists.

I believe the index needs to be created only if it does not exist.

Running the application multiple times keeps creating new indexes. This will result in redundant indexes and increased storage.

See behaviour below -

The first time I run the application with PGVector dependencies.

vector_store=# \d vector_store
                     Table "public.vector_store"
  Column   |     Type     | Collation | Nullable |      Default
-----------+--------------+-----------+----------+--------------------
 id        | uuid         |           | not null | uuid_generate_v4()
 content   | text         |           |          |
 metadata  | json         |           |          |
 embedding | vector(1536) |           |          |
Indexes:
    "vector_store_pkey" PRIMARY KEY, btree (id)
    "vector_store_embedding_idx" hnsw (embedding vector_cosine_ops)

The second time I run the application.

vector_store-# \d vector_store
                     Table "public.vector_store"
  Column   |     Type     | Collation | Nullable |      Default
-----------+--------------+-----------+----------+--------------------
 id        | uuid         |           | not null | uuid_generate_v4()
 content   | text         |           |          |
 metadata  | json         |           |          |
 embedding | vector(1536) |           |          |
Indexes:
    "vector_store_pkey" PRIMARY KEY, btree (id)
    "vector_store_embedding_idx" hnsw (embedding vector_cosine_ops)
    "vector_store_embedding_idx1" hnsw (embedding vector_cosine_ops)
    "vector_store_embedding_idx2" hnsw (embedding vector_cosine_ops)

Comment From: iamroberthanson

This is the code causing the issue.

if (this.createIndexMethod != PgIndexType.NONE) {
    this.jdbcTemplate.execute(String.format("""
            CREATE INDEX ON %s USING %s (embedding %s)
            """, VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index));
}

I noticed it because I have a large index and it started hanging after a reboot. I think a workaround is to set spring.ai.vectorstore.pgvector.index-type=NONE after the index has been created, but I haven't fully tested this.

Comment From: iAMSagar44

@iamroberthanson - this is fixed and will be part of an upcoming Release.

Please check - https://github.com/spring-projects/spring-ai/commit/773b7bd3cacb3c644b2fcb67bf6355a463c47bf7