diff --git a/backend/migrations/20260918062854_audit_partitioned_workspace_operation_index.up.sql b/backend/migrations/20260918062854_audit_partitioned_workspace_operation_index.up.sql deleted file mode 100644 index c7de75a93c..0000000000 --- a/backend/migrations/20260918062854_audit_partitioned_workspace_operation_index.up.sql +++ /dev/null @@ -1,4 +0,0 @@ --- The backend builds this per partition with CREATE INDEX CONCURRENTLY instead, --- see create_audit_operation_index_concurrently in windmill-api/src/db.rs -CREATE INDEX IF NOT EXISTS ix_audit_partitioned_workspace_operation - ON audit_partitioned (workspace_id, operation, "timestamp" DESC); diff --git a/backend/migrations/20260918062854_audit_partitioned_workspace_operation_index.down.sql b/backend/migrations/20260918173412_audit_partitioned_workspace_operation_index.down.sql similarity index 100% rename from backend/migrations/20260918062854_audit_partitioned_workspace_operation_index.down.sql rename to backend/migrations/20260918173412_audit_partitioned_workspace_operation_index.down.sql diff --git a/backend/migrations/20260918173412_audit_partitioned_workspace_operation_index.up.sql b/backend/migrations/20260918173412_audit_partitioned_workspace_operation_index.up.sql new file mode 100644 index 0000000000..4d07e66f5a --- /dev/null +++ b/backend/migrations/20260918173412_audit_partitioned_workspace_operation_index.up.sql @@ -0,0 +1,6 @@ +-- The backend builds this per partition with CREATE INDEX CONCURRENTLY instead, +-- 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_partitioned_workspace_operation + ON audit_partitioned (workspace_id, operation, id DESC, "timestamp"); diff --git a/backend/windmill-api/src/db.rs b/backend/windmill-api/src/db.rs index 7e627652e3..fd6ee24e56 100644 --- a/backend/windmill-api/src/db.rs +++ b/backend/windmill-api/src/db.rs @@ -30,7 +30,8 @@ async fn current_database(conn: &mut PgConnection) -> Result Result<(), MigrateError> { conn.execute( - r#"CREATE INDEX IF NOT EXISTS ix_audit_partitioned_workspace_operation - ON ONLY audit_partitioned (workspace_id, operation, "timestamp" DESC)"#, + format!( + "CREATE INDEX IF NOT EXISTS ix_audit_partitioned_workspace_operation \ + ON ONLY audit_partitioned {AUDIT_OPERATION_INDEX_KEY}" + ) + .as_str(), ) .await?; let partitions: Vec = sqlx::query_scalar( @@ -57,14 +61,14 @@ 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_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()) .await?; conn.execute( format!( - r#"CREATE INDEX CONCURRENTLY {index} ON {} (workspace_id, operation, "timestamp" DESC)"#, + "CREATE INDEX CONCURRENTLY {index} ON {} {AUDIT_OPERATION_INDEX_KEY}", quote(&partition) ) .as_str(),