Skip to content

Remove subtransactions in `Issue::Metrics`

In #338346 (comment 652623314), we identified the use of subtransactions can cause significant performance issues on PostgreSQL replicas at scale. If a long running transaction occurs, the subtransaction cache may not fit into the working set of memory, which can lead to a context switch storm when PostgreSQL needs to load subtransaction data from disk. While this arguably is a PostgreSQL bug, we should remove subtransactions entirely because they aren't performant at GitLab.com scale, nor are they strictly necessary.

From the Prometheus dashboard you can see that Issue::Metrics model creates a significant number of subtransactions.

issue_metrics_subtransactions

This seems to be connected with subtransactions coming from the Issue model too.

This might happen in

  # Ensure that the metrics association is safely created and respecting the unique constraint on issue_id
  override :ensure_metrics
  def ensure_metrics
    if !association(:metrics).loaded? || metrics.blank?
      metrics_record = Issue::Metrics.safe_find_or_create_by(issue: self)
      self.metrics = metrics_record
    end

    metrics.record!
  end

/cc @stanhu

Edited by Grzegorz Bizon