[19.1] Enable LFK cleanup LATERAL join by default

What does this MR do and why?

Backports the Loose Foreign Keys (LFK) cleanup query performance fix to the 19-1-stable-ee branch, for self-managed and GitLab Dedicated instances running 19.1.x.

The LATERAL join rewrite (!235721 (merged), e9d8803ca00e) is already present on 19-1-stable-ee but gated behind the default-off loose_foreign_keys_lateral_query feature flag. This MR cherry-picks only the flag removal so the fix is on by default:

  • !240768 (merged) — Remove the loose_foreign_keys_lateral_query feature flag (e7fe4677c591)

Why

On large instances LooseForeignKeys::CleanupWorker falls behind because the cleanup queries time out. The pre-19.2 query form is a correlated IN (SELECT ... WHERE fk_col IN ($1...$500) LIMIT N FOR UPDATE SKIP LOCKED) against high-fan-out parents, which on partitioned children (ci_builds, p_ci_pipelines) hits PG::QueryCanceled: canceling statement due to statement timeout. When these time out, the parent record is never marked processed, loose_foreign_keys_deleted_records grows unbounded, and old partitions never drop.

The LATERAL join form forces one index seek per parent id instead of a single large scan. PartitionCleanerService inherits it, so partitioned children benefit too.

This directly caused the upgrade incident in https://gitlab.com/gitlab-org/gitlab/-/issues/605940 (a packages_helm_metadata_cache_states sharding-key backfill hit a projects FK violation because a deleted project's cleanup record had been stuck pending for months).

Conflict resolution

Only spec/services/loose_foreign_keys/cleaner_service_spec.rb (FOSS) conflicted, because an intermediate rubocop commit (692fb3788073 — "Fix RSpec/BeEq Rubocop offenses") on master is not on the stable branch. Resolved by taking the end-state of that spec file from e7fe4677c591; the resulting cleaner_service.rb and specs are identical to the master post-flag-removal state.

References

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
  • This MR has been approved by a maintainer (only one approval is required).
  • Ensure the e2e:test-on-omnibus-ee job 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:

Edited by Krasimir Angelov

Merge request reports

Loading