Skip to content

World's first open-source enterprise world model.

Reuse the vector build connection for index status checks (#734)

mayakid0c08b86310e0
diff --git a/crates/utopia-store/src/vector_index.rs b/crates/utopia-store/src/vector_index.rs
index e114eb2a..8a3a7d15 100644
--- a/crates/utopia-store/src/vector_index.rs
+++ b/crates/utopia-store/src/vector_index.rs
@@ -103,13 +103,21 @@ pub const RESORT: &str = "distance + 0, id";
/// 索引现在的状态:`None` 没有;`Some(valid)` 有,`false` 是上次建到一半留下的
pub async fn status(pool: &PgPool, target: Target, dims: usize) -> AppResult<Option<bool>> {
+ status_on(pool, target, dims).await
+}
+
+async fn status_on<'a>(
+ executor: impl Executor<'a, Database = Postgres>,
+ target: Target,
+ dims: usize,
+) -> AppResult<Option<bool>> {
let row: Option<(bool,)> = sqlx::query_as(
"SELECT i.indisvalid FROM pg_class c
JOIN pg_index i ON i.indexrelid = c.oid
WHERE c.relname = $1 AND c.relkind = 'i'",
)
.bind(index_name(target, dims))
- .fetch_optional(pool)
+ .fetch_optional(executor)
.await?;
Ok(row.map(|(v,)| v))
}
@@ -186,7 +194,9 @@ pub async fn build(pool: &PgPool, target: Target, dims: usize) -> AppResult<Buil
// 默认 64 MB 的 maintenance_work_mem 到一万四千行 1024 维就装不下图,之后
// 每一行都要落盘再读,5 万行建了 4 分 20 秒;512 MB 只在建的这一会儿占着
conn.execute("SET maintenance_work_mem = '512MB'").await?;
- let before = status(pool, target, dims).await?;
+ // Reuse the held connection: acquiring another from a busy pool can
+ // make builds wait for connections they are themselves holding.
+ let before = status_on(&mut *conn, target, dims).await?;
if before == Some(false) {
conn.execute(format!("DROP INDEX CONCURRENTLY IF EXISTS {name}").as_str())
.await?;
diff --git a/crates/utopia-store/tests/a_vector_build_uses_one_connection.rs b/crates/utopia-store/tests/a_vector_build_uses_one_connection.rs
new file mode 100644
index 00000000..682d593e
--- /dev/null
+++ b/crates/utopia-store/tests/a_vector_build_uses_one_connection.rs
@@ -0,0 +1,38 @@
+//! A build already owns a connection; checking its index must not need another.
+use std::time::Duration;
+use utopia_store::vector_index::{self, Target};
+
+#[tokio::test]
+async fn a_build_finishes_with_only_one_connection_available() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ // Two is the application's supported minimum. The timeout only bounds a
+ // broken nested acquire; no assertion depends on index-building latency.
+ let pool = sqlx::postgres::PgPoolOptions::new()
+ .max_connections(2)
+ .acquire_timeout(Duration::from_secs(2))
+ .connect(&url)
+ .await?;
+ utopia_store::db::migrate(&pool).await?;
+ for (target, dims) in [(Target::Chunks, 17), (Target::EntityProfiles, 18)] {
+ vector_index::drop(&pool, target, dims).await?;
+ // Stand in for another request holding one connection. This makes the
+ // pool pressure deterministic, without racing two build tasks.
+ let busy = pool.acquire().await?;
+ let run = async {
+ let built = vector_index::build(&pool, target, dims).await?;
+ assert!(built.created);
+ assert_eq!(vector_index::status(&pool, target, dims).await?, Some(true));
+ let again = vector_index::build(&pool, target, dims).await?;
+ assert!(!again.created, "an existing valid index is reused");
+ Ok::<_, anyhow::Error>(())
+ }
+ .await;
+ drop(busy);
+ vector_index::drop(&pool, target, dims).await?;
+ run?;
+ }
+ pool.close().await;
+ Ok(())
+}