diff --git a/crates/ipx/src/db.rs b/crates/ipx/src/db.rs index b6055e8..955b03e 100644 --- a/crates/ipx/src/db.rs +++ b/crates/ipx/src/db.rs @@ -117,6 +117,15 @@ async fn create_missing(orm: &sea_orm::DatabaseConnection) -> Result<()> { orm.execute_unprepared(sql).await.with_context(|| sql.to_owned())?; } } + // An expression index has no statistics until its table is analysed, and without them + // Postgres guessed a third of entries were recent and read them all, entries_recent unused + // (#274). Analysed once when they are missing; autovacuum keeps them after. + if orm.get_database_backend() == sea_orm::DbBackend::Postgres { + let has = Statement::from_string(sea_orm::DbBackend::Postgres, "SELECT 1 FROM pg_stats WHERE tablename = 'entries_recent'"); + if orm.query_one_raw(has).await?.is_none() { + orm.execute_unprepared("ANALYZE entries").await.context("analysing entries")?; + } + } if orm.execute_unprepared("SELECT recheck FROM signal_verdicts LIMIT 0").await.is_err() { let sql = "ALTER TABLE signal_verdicts ADD COLUMN recheck BOOLEAN NOT NULL DEFAULT false"; orm.execute_unprepared(sql).await.with_context(|| sql.to_owned())?;