Skip to content

Commit 9815a58

Browse files
committed
Auto merge of #3446 - jtgeibel:daily-db-maintenance, r=Turbo87
Run daily database maintenance tasks Run `VACUUM version_downloads;` daily. We could alternatively tweak the auto-vacuum parameters on this table, but long term we should archive daily download stats outside of the database and it will be easier to pick the parameters then, once we have only 90 days of history rather than growing daily without bound. r? `@Turbo87`
2 parents 88e7a01 + c26c179 commit 9815a58

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/bin/enqueue-job.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn main() -> Result<()> {
3535
.unwrap_or_else(|| String::from("db-dump.tar.gz"));
3636
Ok(tasks::dump_db(database_url, target_name).enqueue(&conn)?)
3737
}
38+
"daily_db_maintenance" => Ok(tasks::daily_db_maintenance().enqueue(&conn)?),
3839
other => Err(anyhow!("Unrecognized job type `{}`", other)),
3940
}
4041
}

src/tasks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
mod daily_db_maintenance;
12
pub mod dump_db;
23
mod update_downloads;
34

5+
pub use daily_db_maintenance::daily_db_maintenance;
46
pub use dump_db::dump_db;
57
pub use update_downloads::update_downloads;

src/tasks/daily_db_maintenance.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// Run daily database maintenance tasks
2+
///
3+
/// By default PostgreSQL will run an auto-vacuum when 20% of the tuples in a table are dead.
4+
/// Because the `version_downloads` table includes years of historical data, we can accumulate
5+
/// a *lot* of garbage before an auto-vacuum is run.
6+
///
7+
/// We only need to keep 90 days of entries in `version_downloads`. Once we have a mechanism to
8+
/// archive daily download counts and drop historical data, we can drop this task and rely on
9+
/// auto-vacuum again.
10+
use diesel::{sql_query, RunQueryDsl};
11+
use swirl::PerformError;
12+
13+
#[swirl::background_job]
14+
pub fn daily_db_maintenance(conn: &PgConnection) -> Result<(), PerformError> {
15+
println!("Running VACUUM on version_downloads table");
16+
sql_query("VACUUM version_downloads;").execute(conn)?;
17+
println!("Finished running VACUUM on version_downloads table");
18+
Ok(())
19+
}

0 commit comments

Comments
 (0)