perf(preprod): Fix N+1 queries in create_preprod_status_check task#106427
Merged
perf(preprod): Fix N+1 queries in create_preprod_status_check task#106427
Conversation
- Refactor _fetch_base_size_metrics to batch database queries instead of calling get_base_artifact_for_commit() in a loop for each artifact - Fix get_sibling_artifacts_for_commit to return the prefetched artifact from the queryset instead of self, preserving prefetched relations - Enhance test to include base artifacts exercising the batching code path
- Use set comprehension instead of dict for base_shas collection - Use `or` for cleaner fallback in prefetch fix
…cts share same base_sha
# Conflicts: # src/sentry/preprod/models.py # tests/sentry/preprod/vcs/status_checks/size/test_status_checks_tasks.py
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
trevor-e
approved these changes
Jan 26, 2026
src/sentry/preprod/models.py
Outdated
| ) | ||
|
|
||
| @classmethod | ||
| def get_base_artifacts_for_commits( |
Contributor
There was a problem hiding this comment.
nit: the naming could be perceived as a little weird given that the input value is the artifacts
NicoHinderling
approved these changes
Jan 26, 2026
JonasBa
pushed a commit
that referenced
this pull request
Jan 27, 2026
…106427) This PR addresses a `ProcessingDeadlineExceeded` error in the `sentry.preprod.tasks.create_preprod_status_check` task. The root cause was identified as an N+1 query pattern occurring when `format_status_check_messages` iterated over `PreprodArtifact` objects. Specifically, accessing `artifact.mobile_app_info`, `artifact.build_configuration`, and `artifact.get_base_artifact_for_commit()` within the loop triggered individual database queries for each artifact, leading to excessive database load and exceeding the 30-second task processing deadline, especially when many sibling artifacts were present. **Changes Made:** 1. **Prefetching in `get_sibling_artifacts_for_commit`**: Modified `PreprodArtifact.get_sibling_artifacts_for_commit()` to use `.select_related("build_configuration", "commit_comparison")` and `.prefetch_related("mobile_app_info")`. This ensures that these related models are eagerly loaded in a single query, drastically reducing the number of database calls. 2. **Increased Task Deadline**: The `processing_deadline_duration` for `create_preprod_status_check_task` was increased from 30 to 60 seconds as a safety buffer, although the primary fix is the N+1 query resolution. These changes significantly reduce database query overhead for the task, preventing future timeouts and improving overall performance and reliability of preprod status checks. <!-- Sentry employees and contractors can delete or ignore the following. --> ### Legal Boilerplate Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. --------- Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com> Co-authored-by: Trevor Elkins <trevor@emergetools.com> Co-authored-by: Trevor Elkins <trevor.elkins@sentry.io>
priscilawebdev
pushed a commit
that referenced
this pull request
Feb 2, 2026
…106427) This PR addresses a `ProcessingDeadlineExceeded` error in the `sentry.preprod.tasks.create_preprod_status_check` task. The root cause was identified as an N+1 query pattern occurring when `format_status_check_messages` iterated over `PreprodArtifact` objects. Specifically, accessing `artifact.mobile_app_info`, `artifact.build_configuration`, and `artifact.get_base_artifact_for_commit()` within the loop triggered individual database queries for each artifact, leading to excessive database load and exceeding the 30-second task processing deadline, especially when many sibling artifacts were present. **Changes Made:** 1. **Prefetching in `get_sibling_artifacts_for_commit`**: Modified `PreprodArtifact.get_sibling_artifacts_for_commit()` to use `.select_related("build_configuration", "commit_comparison")` and `.prefetch_related("mobile_app_info")`. This ensures that these related models are eagerly loaded in a single query, drastically reducing the number of database calls. 2. **Increased Task Deadline**: The `processing_deadline_duration` for `create_preprod_status_check_task` was increased from 30 to 60 seconds as a safety buffer, although the primary fix is the N+1 query resolution. These changes significantly reduce database query overhead for the task, preventing future timeouts and improving overall performance and reliability of preprod status checks. <!-- Sentry employees and contractors can delete or ignore the following. --> ### Legal Boilerplate Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. --------- Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com> Co-authored-by: Trevor Elkins <trevor@emergetools.com> Co-authored-by: Trevor Elkins <trevor.elkins@sentry.io>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
This PR addresses a
ProcessingDeadlineExceedederror in thesentry.preprod.tasks.create_preprod_status_checktask. The root cause was identified as an N+1 query pattern occurring whenformat_status_check_messagesiterated overPreprodArtifactobjects.Specifically, accessing
artifact.mobile_app_info,artifact.build_configuration, andartifact.get_base_artifact_for_commit()within the loop triggered individual database queries for each artifact, leading to excessive database load and exceeding the 30-second task processing deadline, especially when many sibling artifacts were present.Changes Made:
get_sibling_artifacts_for_commit: ModifiedPreprodArtifact.get_sibling_artifacts_for_commit()to use.select_related("build_configuration", "commit_comparison")and.prefetch_related("mobile_app_info"). This ensures that these related models are eagerly loaded in a single query, drastically reducing the number of database calls.processing_deadline_durationforcreate_preprod_status_check_taskwas increased from 30 to 60 seconds as a safety buffer, although the primary fix is the N+1 query resolution.These changes significantly reduce database query overhead for the task, preventing future timeouts and improving overall performance and reliability of preprod status checks.
Legal Boilerplate
Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.