Fix: do not query the run status of every scheduled task in the schedule loop - #3072
Open
TheBeast85 wants to merge 1 commit into
Open
Fix: do not query the run status of every scheduled task in the schedule loop#3072TheBeast85 wants to merge 1 commit into
TheBeast85 wants to merge 1 commit into
Conversation
…ule loop
init_task_schedule_iterator selects everything the due checks need except the
task run status, so task_schedule_iterator_start_due, _stop_due and
_timed_out each ask the database for it separately, once per row and once per
pass of the schedule loop. task_running_report asks for the run status as
well, which makes three identical queries per scheduled task and pass.
They all run inside the transaction that init_task_schedule_iterator opens
with sql_begin_immediate_giveup, so the loop holds that transaction while it
issues them.
Select tasks.run_status with the rest of the row and read it from there. It
is appended to the select list, so the existing column indices are untouched,
and the accessor is static because nothing outside this file needs it.
In _stop_due, check the run status before looking for a running report: a
task that is not requested, queued or running has none, so the report lookup
(and the run status query inside it) is only needed for tasks that are on
their way.
idle, 7 scheduled tasks, 60 s
"SELECT run_status FROM tasks" statements
before: 108 after: 0
The count is three per scheduled task per pass, so it grows with the number
of scheduled tasks: on an installation with 4503 of them it was 40527
statements per 30 s, and the loop held its transaction for ~850 ms every
10 seconds.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
greenbonebot
enabled auto-merge (rebase)
July 30, 2026 10:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
tasks.run_statusininit_task_schedule_iteratorand read it from the rowtask_schedule_iterator_stop_due, check the run status before looking for a running reportWhy
The three due checks each asked the database for the run status separately, once per row and once per pass of the schedule loop, and
task_running_reportasks for it again - three identical queries per scheduled task and pass. They all run inside the transaction that the iterator opens withsql_begin_immediate_giveup, so the loop holds that transaction while issuing them.Only a task that is requested, queued or running can have a running report, so that lookup is not needed for any of the others.
Idle with 7 scheduled tasks the
SELECT run_status FROM tasksstatements go from 108 to 0 per minute. The count is three per scheduled task and pass, so it grows with the number of scheduled tasks: with 4503 of them it was 40527 statements per 30 s and the loop held its transaction for about 850 ms every 10 seconds.The run status column is appended to the select list, so the existing column indices are untouched, and the accessor is static because nothing outside the file needs it.
Checklist