perf: index the legacy audit table by operation too

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-09-18 20:28:14 +02:00
co-authored by Claude Opus 5
parent 9ed483ae70
commit 3d88676bbe
3 changed files with 29 additions and 3 deletions
@@ -1 +1,2 @@
DROP INDEX IF EXISTS ix_audit_partitioned_workspace_operation;
DROP INDEX IF EXISTS ix_audit_workspace_operation;
@@ -1,6 +1,8 @@
-- The backend builds this per partition with CREATE INDEX CONCURRENTLY instead,
-- see create_audit_operation_index_concurrently in windmill-api/src/db.rs.
-- The backend builds these with CREATE INDEX CONCURRENTLY instead (per partition for
-- audit_partitioned), see create_audit_operation_index_concurrently in windmill-api/src/db.rs.
-- id matches list_audit's order and before_id cursor. timestamp trails it so a
-- time window is checked in the index rather than on every row it reads.
CREATE INDEX IF NOT EXISTS ix_audit_workspace_operation
ON audit (workspace_id, operation, id DESC, "timestamp");
CREATE INDEX IF NOT EXISTS ix_audit_partitioned_workspace_operation
ON audit_partitioned (workspace_id, operation, id DESC, "timestamp");
+24 -1
View File
@@ -40,6 +40,27 @@ const AUDIT_OPERATION_INDEX_KEY: &str = r#"(workspace_id, operation, id DESC, "t
async fn create_audit_operation_index_concurrently(
conn: &mut PgConnection,
) -> Result<(), MigrateError> {
// list_audit reads the legacy table too, which holds up to a retention period of rows
// written before partitioning.
let legacy_valid: Option<bool> = sqlx::query_scalar(
"SELECT indisvalid FROM pg_index
WHERE indexrelid = to_regclass('ix_audit_workspace_operation')",
)
.fetch_optional(&mut *conn)
.await?;
if legacy_valid != Some(true) {
conn.execute("DROP INDEX CONCURRENTLY IF EXISTS ix_audit_workspace_operation")
.await?;
conn.execute(
format!(
"CREATE INDEX CONCURRENTLY ix_audit_workspace_operation \
ON audit {AUDIT_OPERATION_INDEX_KEY}"
)
.as_str(),
)
.await?;
}
conn.execute(
format!(
"CREATE INDEX IF NOT EXISTS ix_audit_partitioned_workspace_operation \
@@ -61,7 +82,9 @@ async fn create_audit_operation_index_concurrently(
.await?;
let quote = |name: &str| format!("\"{}\"", name.replace('"', "\"\""));
for partition in partitions {
let index = quote(&format!("{partition}_workspace_id_operation_id_timestamp_idx"));
let index = quote(&format!(
"{partition}_workspace_id_operation_id_timestamp_idx"
));
tracing::info!("Building ix_audit_partitioned_workspace_operation on {partition}");
// An interrupted CONCURRENTLY build leaves an invalid index under this name.
conn.execute(format!("DROP INDEX CONCURRENTLY IF EXISTS {index}").as_str())