diff --git a/procrastinate/contrib/django/migrations/0031_add_indexes_for_fetch_job.py b/procrastinate/contrib/django/migrations/0031_add_indexes_for_fetch_job.py new file mode 100644 index 000000000..794363039 --- /dev/null +++ b/procrastinate/contrib/django/migrations/0031_add_indexes_for_fetch_job.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from django.db import migrations + +from .. import migrations_utils + + +class Migration(migrations.Migration): + operations = [ + migrations_utils.RunProcrastinateSQL( + name="02.14.01_01_add_indexes_for_fetch_job.sql" + ) + ] + name = "0031_add_indexes_for_fetch_job" + dependencies = [("procrastinate", "0030_alter_procrastinateevent_options")] diff --git a/procrastinate/sql/migrations/02.14.01_01_add_indexes_for_fetch_job.sql b/procrastinate/sql/migrations/02.14.01_01_add_indexes_for_fetch_job.sql new file mode 100644 index 000000000..71974ee6f --- /dev/null +++ b/procrastinate/sql/migrations/02.14.01_01_add_indexes_for_fetch_job.sql @@ -0,0 +1,7 @@ +-- recreate procrastinate_jobs_id_lock_idx index by adding aborting status to the filter so that it can be used by the fetch job function +CREATE INDEX procrastinate_jobs_id_lock_idx_temp ON procrastinate_jobs (id, lock) WHERE status = ANY (ARRAY['todo'::procrastinate_job_status, 'doing'::procrastinate_job_status, 'aborting'::procrastinate_job_status]); +DROP INDEX procrastinate_jobs_id_lock_idx; +ALTER INDEX procrastinate_jobs_id_lock_idx_temp RENAME TO procrastinate_jobs_id_lock_idx; + +-- add index to avoid seq scan of outer loop in the fetch job function +CREATE INDEX procrastinate_jobs_priority_idx ON procrastinate_jobs(priority desc, id asc) WHERE (status = 'todo'::procrastinate_job_status); diff --git a/procrastinate/sql/schema.sql b/procrastinate/sql/schema.sql index a2fe4d950..4dd7bd59a 100644 --- a/procrastinate/sql/schema.sql +++ b/procrastinate/sql/schema.sql @@ -65,7 +65,9 @@ CREATE UNIQUE INDEX procrastinate_jobs_queueing_lock_idx ON procrastinate_jobs ( CREATE UNIQUE INDEX procrastinate_jobs_lock_idx ON procrastinate_jobs (lock) WHERE status = 'doing'; CREATE INDEX procrastinate_jobs_queue_name_idx ON procrastinate_jobs(queue_name); -CREATE INDEX procrastinate_jobs_id_lock_idx ON procrastinate_jobs (id, lock) WHERE status = ANY (ARRAY['todo'::procrastinate_job_status, 'doing'::procrastinate_job_status]); +CREATE INDEX procrastinate_jobs_id_lock_idx ON procrastinate_jobs (id, lock) WHERE status = ANY (ARRAY['todo'::procrastinate_job_status, 'doing'::procrastinate_job_status, 'aborting'::procrastinate_job_status]); +CREATE INDEX procrastinate_jobs_priority_idx ON procrastinate_jobs(priority desc, id asc) WHERE (status = 'todo'::procrastinate_job_status); + CREATE INDEX procrastinate_events_job_id_fkey ON procrastinate_events(job_id);