Repository navigation
Fix superfluous SELECT when scheduling a single future job - #807
Mohammadalkhassawneh wants to merge 2 commits into
Conversation
When a single job is enqueued, each Execution subclass fires a SELECT on solid_queue_jobs before INSERT. This happens because: 1. assumes_attributes_from_job sets a before_create callback that calls job.send(attribute) for each attribute to copy from the job record. 2. Since create_or_find_by!(job_id: id) only sets the job_id foreign key, the job association isn't loaded — so accessing it triggers a SELECT. The bulk path (enqueue_all) already avoids this by using insert_all, which bypasses callbacks entirely. Fix: pass job: self via the create_or_find_by! block so the association is already loaded in memory when both the before_create callback and the belongs_to validation run. No DB round-trip needed. Applied to all three single-job dispatch paths: schedule (ScheduledExecution), ready (ReadyExecution), and block (BlockedExecution).
6e2bffa to
cfad0ed
Compare
|
I tried this on PostgreSQL 18 with Rails 8.1.4, counting the SQL statements per |
Thanks for testing this, and for the statement counts. It's good to see the SELECT gone on PostgreSQL 18 as well. |
|
CI re-run: all green except six Ruby 3.2 jobs (Rails 7.1 / 8.0) that hit the job timeout mid-suite. The same Ruby 3.2 cancellations show up on main (e.g. build 33400944574), so they look unrelated to this change. Could someone re-run those jobs? |
|
Thanks so much for this, @Mohammadalkhassawneh, and for the rerun and the statement counts! #819 made the same change and has just been merged, with a test that also covers ready and blocked executions, so I'll close this one. Yours came first, and I really appreciate the clear write-up of where the extra |
Problem
When a single job is enqueued, Execution subclasses (
ScheduledExecution,ReadyExecution,BlockedExecution) each fire an extraSELECT solid_queue_jobs WHERE id = ?immediately before their INSERT.The root cause is
assume_attributes_from_job— abefore_createcallback that copiesqueue_name,priority, and any subclass-specific attributes from the job record:Because
create_or_find_by!(job_id: id)only sets thejob_idFK, thejobassociation is not loaded. Callingjob.send(attribute)lazy-loads it, triggering a SELECT. The bulk path (enqueue_all) already avoids this becauseinsert_allbypasses ActiveRecord callbacks entirely. The single-job paths do not.Fix
Pass
job: selfvia thecreate_or_find_by!block so the association is already loaded in memory whenassume_attributes_from_jobfires:Applied to all three single-job dispatch paths:
schedule,ready, andblock.