Skip to content

Commit 773b7bd

Browse files
iAMSagar44tzolov
authored andcommitted
PgVectorStore: creating an index only if missing
- Resolve an issue where a new index keeps getting created during application start up. - Solution is is to create an index only if an index on the embedding column does not exist. - Add missing index name.
1 parent db6f7cd commit 773b7bd

File tree

1 file changed

+4
-2
lines changed
  • vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore

1 file changed

+4
-2
lines changed

vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/PgVectorStore.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class PgVectorStore implements VectorStore, InitializingBean {
6060

6161
public static final String VECTOR_TABLE_NAME = "vector_store";
6262

63+
public static final String VECTOR_INDEX_NAME = "spring_ai_vector_index";
64+
6365
public final FilterExpressionConverter filterExpressionConverter = new PgVectorFilterExpressionConverter();
6466

6567
private final JdbcTemplate jdbcTemplate;
@@ -352,8 +354,8 @@ embedding vector(%d)
352354

353355
if (this.createIndexMethod != PgIndexType.NONE) {
354356
this.jdbcTemplate.execute(String.format("""
355-
CREATE INDEX ON %s USING %s (embedding %s)
356-
""", VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index));
357+
CREATE INDEX IF NOT EXISTS %s ON %s USING %s (embedding %s)
358+
""", VECTOR_INDEX_NAME, VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index));
357359
}
358360
}
359361

0 commit comments

Comments
 (0)