From 1077bae82afd76c2c415ac74cf1d208bfdca5081 Mon Sep 17 00:00:00 2001 From: Philipp Thun Date: Thu, 22 Feb 2024 13:17:05 +0100 Subject: [PATCH] Change delayed_jobs_reserve index Add WHERE condition. Jobs that failed are only kept for debugging purposes, they are not taken into account when reserving a job. The corresponding SELECT statement [1] includes 'WHERE failed_at IS NULL', so the corresponding index can omit failed jobs. Partial indexes are not supported for MySQL, thus this change is only for PostgreSQL. [1] https://github.com/TalentBox/delayed_job_sequel/ blob/938ff68e98b44bbc391805c672a74c5ee3aef08b/ lib/delayed/backend/sequel.rb#L26 --- ...2131500_change_delayed_jobs_reserve_index.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 db/migrations/20240222131500_change_delayed_jobs_reserve_index.rb diff --git a/db/migrations/20240222131500_change_delayed_jobs_reserve_index.rb b/db/migrations/20240222131500_change_delayed_jobs_reserve_index.rb new file mode 100644 index 00000000000..f95a7406ce0 --- /dev/null +++ b/db/migrations/20240222131500_change_delayed_jobs_reserve_index.rb @@ -0,0 +1,17 @@ +Sequel.migration do + up do + if database_type == :postgres + drop_index :delayed_jobs, nil, name: :delayed_jobs_reserve, options: %i[if_exists concurrently] + add_index :delayed_jobs, %i[queue locked_at locked_by failed_at run_at priority], + where: { failed_at: nil }, name: :delayed_jobs_reserve, options: %i[if_not_exists concurrently] + end + end + + down do + if database_type == :postgres + drop_index :delayed_jobs, nil, name: :delayed_jobs_reserve, options: %i[if_exists concurrently] + add_index :delayed_jobs, %i[queue locked_at locked_by failed_at run_at priority], + name: :delayed_jobs_reserve, options: %i[if_not_exists concurrently] + end + end +end