From a14107d28df8aa3ec356705e78c805afe0163e33 Mon Sep 17 00:00:00 2001 From: Moises Lopez Date: Tue, 10 Nov 2026 04:26:37 +0000 Subject: [PATCH] [PERF] queue_job: declare the indexes as models.Index and drop the one on channel The two indexes created in init() become models.Index attributes, the way 19.0 declares them, with the names they already have. models.Index compares the comment of the existing index with its definition and rebuilds it when they differ, so a pre-migration gives the existing indexes the comment the ORM expects and the first update does not rebuild them on a big queue_job table. queue.job.channel has index=True, which makes the ORM create queue_job__channel_index. The composite index on (channel, date_done, date_created) has channel as its leading column. New databases never create the single column index. Existing ones keep it, because the ORM only logs "Keep unexpected index" when a field stops asking for one. --- .../migrations/19.0.2.5.0/pre-migration.py | 25 ++++++++++++++ queue_job/models/queue_job.py | 34 ++++++------------- 2 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 queue_job/migrations/19.0.2.5.0/pre-migration.py diff --git a/queue_job/migrations/19.0.2.5.0/pre-migration.py b/queue_job/migrations/19.0.2.5.0/pre-migration.py new file mode 100644 index 0000000000..ac05290069 --- /dev/null +++ b/queue_job/migrations/19.0.2.5.0/pre-migration.py @@ -0,0 +1,25 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) +from openupgradelib import openupgrade + +# The indexes were created in init() with a free text comment. models.Index compares +# the comment with its own definition and rebuilds the index when they differ, so they +# get the comment the ORM expects and are not rebuilt on the first update. +INDEXES = { + "queue_job_identity_key_state_partial_index": ( + "INDEX (identity_key) " + "WHERE state in ('pending','enqueued','wait_dependencies') " + "AND identity_key IS NOT NULL" + ), + "queue_job_channel_date_done_date_created_index": ( + "INDEX (channel, date_done, date_created)" + ), +} + + +@openupgrade.migrate() +def migrate(env, version): + cr = env.cr + for name, definition in INDEXES.items(): + cr.execute("SELECT to_regclass(%s)", [name]) + if cr.fetchone()[0]: + cr.execute(f'COMMENT ON INDEX "{name}" IS %s', [definition]) diff --git a/queue_job/models/queue_job.py b/queue_job/models/queue_job.py index a2481adb75..d20001e019 100644 --- a/queue_job/models/queue_job.py +++ b/queue_job/models/queue_job.py @@ -8,7 +8,6 @@ from odoo import api, exceptions, fields, models from odoo.fields import Domain from odoo.tools import SQL, html_escape -from odoo.tools.sql import create_index from odoo.addons.base_sparse_field.models.fields import Serialized @@ -139,33 +138,20 @@ class QueueJob(models.Model): readonly=True, ) - channel = fields.Char(index=True) + channel = fields.Char() identity_key = fields.Char(readonly=True) worker_pid = fields.Integer(readonly=True) - def init(self): - cr = self.env.cr - # Used by Job.job_record_with_same_identity_key - create_index( - cr, - "queue_job_identity_key_state_partial_index", - "queue_job", - ["identity_key"], - where=( - "state in ('pending','enqueued','wait_dependencies') " - "AND identity_key IS NOT NULL" - ), - comment=("Queue Job: partial index for identity_key on active states"), - ) - # Used by ._gc_old_jobs - create_index( - cr, - "queue_job_channel_date_done_date_created_index", - "queue_job", - ["channel", "date_done", "date_created"], - comment="Queue Job: index to accelerate autovacuum", - ) + # Used by Job.job_record_with_same_identity_key + _identity_key_state_partial_index = models.Index( + "(identity_key) WHERE state in ('pending','enqueued','wait_dependencies') " + "AND identity_key IS NOT NULL" + ) + # Used by ._gc_old_jobs + _channel_date_done_date_created_index = models.Index( + "(channel, date_done, date_created)" + ) @api.depends("dependencies") def _compute_dependency_ids(self):