diff --git a/backend/migrations/20260207000001_concurrent_indexes_v2_job.down.sql b/backend/migrations/20260207000001_concurrent_indexes_v2_job.down.sql new file mode 100644 index 0000000000..ce52b8b53b --- /dev/null +++ b/backend/migrations/20260207000001_concurrent_indexes_v2_job.down.sql @@ -0,0 +1 @@ +-- no-op: indexes are safe to leave in place diff --git a/backend/migrations/20260207000001_concurrent_indexes_v2_job.up.sql b/backend/migrations/20260207000001_concurrent_indexes_v2_job.up.sql new file mode 100644 index 0000000000..8ee642886d --- /dev/null +++ b/backend/migrations/20260207000001_concurrent_indexes_v2_job.up.sql @@ -0,0 +1,41 @@ +-- v2_job: drop obsolete indexes and create new ones CONCURRENTLY +DROP INDEX CONCURRENTLY IF EXISTS root_job_index; +DROP INDEX CONCURRENTLY IF EXISTS root_job_index_by_path_2; +DROP INDEX CONCURRENTLY IF EXISTS root_job_index_by_path; +DROP INDEX CONCURRENTLY IF EXISTS ix_job_workspace_id_created_at_new_6; +DROP INDEX CONCURRENTLY IF EXISTS ix_job_workspace_id_created_at_new_7; +DROP INDEX CONCURRENTLY IF EXISTS ix_job_created_at; +DROP INDEX CONCURRENTLY IF EXISTS ix_v2_job_root_by_path; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_workspace_id_created_at_new_3 + ON v2_job (workspace_id, created_at DESC); + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_workspace_id_created_at_new_8 + ON v2_job (workspace_id, created_at DESC) + WHERE kind IN ('deploymentcallback') AND parent_job IS NULL; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_workspace_id_created_at_new_9 + ON v2_job (workspace_id, created_at DESC) + WHERE kind IN ('dependencies', 'flowdependencies', 'appdependencies') AND parent_job IS NULL; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_workspace_id_created_at_new_5 + ON v2_job (workspace_id, created_at DESC) + WHERE kind IN ('preview', 'flowpreview') AND parent_job IS NULL; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_v2_job_labels + ON v2_job USING GIN (labels) + WHERE labels IS NOT NULL; + +ALTER TABLE v2_job ENABLE ROW LEVEL SECURITY; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_v2_job_workspace_id_created_at + ON v2_job (workspace_id, created_at DESC) + WHERE kind IN ('script', 'flow', 'singlestepflow') AND parent_job IS NULL; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_job_v2_job_root_by_path_2 + ON v2_job (workspace_id, runnable_path) + WHERE parent_job IS NULL; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_root_job_index_by_path_2 + ON v2_job (workspace_id, runnable_path, created_at DESC) + WHERE parent_job IS NULL; diff --git a/backend/migrations/20260207000002_concurrent_indexes_v2_job_completed.down.sql b/backend/migrations/20260207000002_concurrent_indexes_v2_job_completed.down.sql new file mode 100644 index 0000000000..ce52b8b53b --- /dev/null +++ b/backend/migrations/20260207000002_concurrent_indexes_v2_job_completed.down.sql @@ -0,0 +1 @@ +-- no-op: indexes are safe to leave in place diff --git a/backend/migrations/20260207000002_concurrent_indexes_v2_job_completed.up.sql b/backend/migrations/20260207000002_concurrent_indexes_v2_job_completed.up.sql new file mode 100644 index 0000000000..a52dbb02f8 --- /dev/null +++ b/backend/migrations/20260207000002_concurrent_indexes_v2_job_completed.up.sql @@ -0,0 +1,19 @@ +-- v2_job_completed: drop obsolete indexes and create new ones CONCURRENTLY +DROP INDEX CONCURRENTLY IF EXISTS ix_completed_job_workspace_id_created_at; +DROP INDEX CONCURRENTLY IF EXISTS ix_completed_job_workspace_id_created_at_new; +DROP INDEX CONCURRENTLY IF EXISTS index_completed_job_on_schedule_path; +DROP INDEX CONCURRENTLY IF EXISTS index_completed_on_created; +DROP INDEX CONCURRENTLY IF EXISTS ix_completed_job_workspace_id_created_at_new_2; +DROP INDEX CONCURRENTLY IF EXISTS ix_completed_job_workspace_id_started_at_new; +DROP INDEX CONCURRENTLY IF EXISTS ix_completed_job_workspace_id_started_at_new_2; +DROP INDEX CONCURRENTLY IF EXISTS labeled_jobs_on_jobs; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS labeled_jobs_on_jobs + ON v2_job_completed USING GIN ((result -> 'wm_labels')) + WHERE result ? 'wm_labels'; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_completed_completed_at + ON v2_job_completed (completed_at DESC); + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_job_workspace_id_completed_at_all + ON v2_job_completed (workspace_id, completed_at DESC); diff --git a/backend/migrations/20260207000003_concurrent_indexes_v2_job_queue.down.sql b/backend/migrations/20260207000003_concurrent_indexes_v2_job_queue.down.sql new file mode 100644 index 0000000000..ce52b8b53b --- /dev/null +++ b/backend/migrations/20260207000003_concurrent_indexes_v2_job_queue.down.sql @@ -0,0 +1 @@ +-- no-op: indexes are safe to leave in place diff --git a/backend/migrations/20260207000003_concurrent_indexes_v2_job_queue.up.sql b/backend/migrations/20260207000003_concurrent_indexes_v2_job_queue.up.sql new file mode 100644 index 0000000000..d543f69f1c --- /dev/null +++ b/backend/migrations/20260207000003_concurrent_indexes_v2_job_queue.up.sql @@ -0,0 +1,12 @@ +-- v2_job_queue: drop obsolete indexes and create new ones CONCURRENTLY +DROP INDEX CONCURRENTLY IF EXISTS concurrency_limit_stats_queue; +DROP INDEX CONCURRENTLY IF EXISTS queue_sort; +DROP INDEX CONCURRENTLY IF EXISTS queue_sort_2; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS queue_sort_v2 + ON v2_job_queue (priority DESC NULLS LAST, scheduled_for, tag) + WHERE running = false; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS v2_job_queue_suspend + ON v2_job_queue (workspace_id, suspend) + WHERE suspend > 0; diff --git a/backend/migrations/20260207000004_concurrent_indexes_other.down.sql b/backend/migrations/20260207000004_concurrent_indexes_other.down.sql new file mode 100644 index 0000000000..ce52b8b53b --- /dev/null +++ b/backend/migrations/20260207000004_concurrent_indexes_other.down.sql @@ -0,0 +1 @@ +-- no-op: indexes are safe to leave in place diff --git a/backend/migrations/20260207000004_concurrent_indexes_other.up.sql b/backend/migrations/20260207000004_concurrent_indexes_other.up.sql new file mode 100644 index 0000000000..eb3f7234e7 --- /dev/null +++ b/backend/migrations/20260207000004_concurrent_indexes_other.up.sql @@ -0,0 +1,16 @@ +-- audit, alerts, script, log_file: drop obsolete indexes and create new ones CONCURRENTLY +DROP INDEX CONCURRENTLY IF EXISTS log_file_hostname_log_ts_idx; + +CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_audit_timestamps + ON audit (timestamp DESC); + +CREATE INDEX CONCURRENTLY IF NOT EXISTS alerts_by_workspace + ON alerts (workspace_id); + +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_audit_recent_login_activities + ON audit (timestamp, username) + WHERE operation IN ('users.login', 'oauth.login', 'users.token.refresh'); + +CREATE INDEX CONCURRENTLY IF NOT EXISTS script_not_archived + ON script (workspace_id, path, created_at DESC) + WHERE archived = false; diff --git a/backend/windmill-api/src/db.rs b/backend/windmill-api/src/db.rs index 44088a8849..96d68d6252 100644 --- a/backend/windmill-api/src/db.rs +++ b/backend/windmill-api/src/db.rs @@ -64,8 +64,18 @@ lazy_static::lazy_static! { (20260126235947, include_str!( "../../custom_migrations/lowercase_emails_safe.sql" ).to_string()), - (20260206000000, include_str!( - "../../migrations/20260206000000_consolidate_live_index_migrations.up.sql" + (20260206000000, "".to_string()), + (20260207000001, include_str!( + "../../migrations/20260207000001_concurrent_indexes_v2_job.up.sql" + ).to_string()), + (20260207000002, include_str!( + "../../migrations/20260207000002_concurrent_indexes_v2_job_completed.up.sql" + ).to_string()), + (20260207000003, include_str!( + "../../migrations/20260207000003_concurrent_indexes_v2_job_queue.up.sql" + ).to_string()), + (20260207000004, include_str!( + "../../migrations/20260207000004_concurrent_indexes_other.up.sql" ).to_string()), ].into_iter().collect(); }