Skip to content

perf(preprod): Fix N+1 queries in create_preprod_status_check task#106427

Merged
trevor-e merged 13 commits intomasterfrom
seer/perf/preprod-status-check-n1
Jan 26, 2026
Merged

perf(preprod): Fix N+1 queries in create_preprod_status_check task#106427
trevor-e merged 13 commits intomasterfrom
seer/perf/preprod-status-check-n1

Conversation

@seer-by-sentry
Copy link
Contributor

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.

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.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 16, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 16, 2026
@trevor-e trevor-e marked this pull request as ready for review January 16, 2026 17:05
@trevor-e trevor-e requested a review from a team as a code owner January 16, 2026 17:05
- 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
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 16, 2026
- Use set comprehension instead of dict for base_shas collection
- Use `or` for cleaner fallback in prefetch fix
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 16, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 17, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 17, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 17, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 17, 2026
# Conflicts:
#	src/sentry/preprod/models.py
#	tests/sentry/preprod/vcs/status_checks/size/test_status_checks_tasks.py
@linear
Copy link

linear bot commented Jan 23, 2026

@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 23, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 23, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 23, 2026
Copy link
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 23, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 24, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 24, 2026
@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 26, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 26, 2026
)

@classmethod
def get_base_artifacts_for_commits(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the naming could be perceived as a little weird given that the input value is the artifacts

@github-actions github-actions bot removed the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 26, 2026
@trevor-e trevor-e added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Jan 26, 2026
@trevor-e trevor-e merged commit c7991f2 into master Jan 26, 2026
71 of 72 checks passed
@trevor-e trevor-e deleted the seer/perf/preprod-status-check-n1 branch January 26, 2026 22:05
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>
@github-actions github-actions bot locked and limited conversation to collaborators Feb 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants