Backport of 'Avoid unbounded preload in any_merge_request rules sync'
What does this MR do and why?
Backports !241870 (merged) to 19-1-stable-ee.
SyncAnyMergeRequestRulesService preloaded approval_merge_request_rules through approval_policy_rule. That association is keyed by approval_policy_rule_id, so for a policy linked to thousands of projects the preload eagerly loaded every approval rule across the whole instance and caused PG::QueryCanceled statement timeouts in SyncAnyMergeRequestApprovalRulesWorker.
The preloaded collection was only used in policy_affected_by_target_branch? to check whether the policy already had approval rules. That check only needs this merge request's rules, which are already loaded via any_merge_request_rules, so we reuse them through the existing approval_rules_for_policies matcher and drop the unbounded preloads.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
- This MR is backporting a bug fix, documentation update, or spec fix, previously merged in the default branch.
- The MR that fixed the bug on the default branch has been deployed to GitLab.com (not applicable for documentation or spec changes).
- The MR title is descriptive (e.g. "Backport of 'title of default branch MR'"). This is important, since the title will be copied to the patch blog post.
- Required labels have been applied to this merge request
- severity label and bug subtype labels (if applicable)
- If this MR fixes a bug that affects customers, the customer label has been applied.
- This MR has been approved by a maintainer (only one approval is required).
- Ensure the
e2e:test-on-omnibus-eejob has succeeded, or if it has failed, investigate the failures. If you determine the failures are unrelated, you may proceed. If you need assistance investigating, request help in the #s_developer_experience Slack channel to confirm the failures are unrelated to the merge request.
Note to the merge request author and maintainer
If you have questions about the patch release process, please:
- Refer to the patch release runbook for engineers and maintainers for guidance.
- Ask questions on the
#releasesSlack channel (internal only). - Once the backport has been merged, the commit changes will be automatically deployed to a release environment that can be used for manual validation. See after merging runbook for details.