Loading
Skip zeroing approval rules that already require no approvals
Approval policy syncs set approvals_required to 0 on every passed rule on every run, including rules already at 0. That is the steady state for a merge request whose rules keep passing. Behind the approval_policy_sync_skip_unchanged_writes flag (off by default), ApprovalMergeRequestRule.remove_required_approved only matches rules with approvals_required > 0, so rows already at 0 aren't rewritten by any of its three callers. The filter runs against the stored row, so every rule ends in the same state as today.
Detailed context for AI agents
Why
ApprovalMergeRequestRule.remove_required_approvedis an unconditionalUPDATE approval_merge_request_rules SET approvals_required = 0 WHERE id IN (...). Its callers arePolicyRuleEvaluationService#save(every passed rule, fromSyncMergeRequestApprovalsWorkerand other workers),SyncAnyMergeRequestRulesService#update_required_approvalsandUnblockFailOpenApprovalRulesService#execute.- Postgres writes a new row version for every matched row, even when the value doesn't change. So each run takes row locks, writes WAL and leaves dead tuples for every passed rule.
approvals_requiredisn't indexed, so these are HOT-eligible updates. The gain is less row churn and lock time onapproval_merge_request_rules, not a large latency win per job.
Change
remove_required_approvedtakes a new requiredproject:keyword, used as the flag actor. With the flag on, it chains the existingwith_approvals_requiredscope (approvals_required > 0), so the statement becomesUPDATE ... WHERE id IN (...) AND approvals_required > 0.- All three callers pass
merge_request.project. No other callers exist. - An earlier version filtered only in
PolicyRuleEvaluationServicethrough a private wrapper. Review pointed out the other two callers do the same no-op write, so the filter moved into the model method and the wrapper was removed. - With the flag off, every caller runs master's UPDATE plus one
Feature.enabled?check.
Edge cases
- The filter reads the stored row, not the in-memory rule. A rule that both failed and passed in the same evaluation is first reset by
reset_required_approvals, so its row is above 0 and it is still zeroed. A spec covers this. update_allsets no timestamps and runs no callbacks, so skipping a row that is already 0 changes nothing observable. No caller uses the returned row count.- In
SyncAnyMergeRequestRulesServicethe violated and zeroed rule sets are disjoint, and inUnblockFailOpenApprovalRulesServiceonly fail-open rules are zeroed, so the stored-row filter gives the same end state there too. - Concurrent jobs: if another job raises the rule above 0 before this UPDATE, the filter still matches and zeroes it, as master does. If another job already set it to 0, both versions end at 0.
- The UPDATE statement still runs when every row is already 0, so load balancing stickiness is the same as today.
Scope
- An earlier version of this MR also skipped the violations transaction when no row could change, and skipped the zeroing from the in-memory
approvals_required. Both were dropped:- The violations part had to copy
delete_violations' matching and add a primary read, for about 8% of the worker's transaction time. It may come back in a separateUpdateViolationsServiceMR. - The in-memory check could act on a value read from a lagging replica. The SQL filter decides on the primary.
- The violations part had to copy
- The larger per-job cost is the per-rule re-sync transaction in
MergeRequest#reset_required_approvals. That is tracked separately in #637926.
Verification
Locally on 6816f649, in an isolated rspec slot:
ee/spec/models/approval_merge_request_rule_spec.rb(.with_approvals_required+ new.remove_required_approved, flag on and off): 3 examples, 0 failures.ee/spec/services/security/security_orchestration_policies/policy_rule_evaluation_service_spec.rb: 77 examples, 0 failures. The row-count specs moved to the model spec; the fail-then-pass edge case stays here.ee/spec/services/security/scan_result_policies/sync_any_merge_request_rules_service_spec.rb: 122 examples, 0 failures.ee/spec/services/security/scan_result_policies/unblock_fail_open_approval_rules_service_spec.rb: 8 examples, 0 failures.
Feature flag and rollout
approval_policy_sync_skip_unchanged_writes,gitlab_com_derisk, EE, default off, actor is the merge request's project. Rollout issue: #642935.
Edited by Marc Shaw