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.

References: #637924, #631813

Detailed context for AI agents

Why

  • ApprovalMergeRequestRule.remove_required_approved is an unconditional UPDATE approval_merge_request_rules SET approvals_required = 0 WHERE id IN (...). Its callers are PolicyRuleEvaluationService#save (every passed rule, from SyncMergeRequestApprovalsWorker and other workers), SyncAnyMergeRequestRulesService#update_required_approvals and UnblockFailOpenApprovalRulesService#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_required isn't indexed, so these are HOT-eligible updates. The gain is less row churn and lock time on approval_merge_request_rules, not a large latency win per job.

Change

  • remove_required_approved takes a new required project: keyword, used as the flag actor. With the flag on, it chains the existing with_approvals_required scope (approvals_required > 0), so the statement becomes UPDATE ... WHERE id IN (...) AND approvals_required > 0.
  • All three callers pass merge_request.project. No other callers exist.
  • An earlier version filtered only in PolicyRuleEvaluationService through 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_all sets 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 SyncAnyMergeRequestRulesService the violated and zeroed rule sets are disjoint, and in UnblockFailOpenApprovalRulesService only 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 separate UpdateViolationsService MR.
    • The in-memory check could act on a value read from a lagging replica. The SQL filter decides on the primary.
  • 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

Merge request reports

Loading
Loading