Skip to content

Approximate counters are not working on gitlab.com

Summary

While working on #208234 (closed) we noticed that approximate database counters are not working

Steps to reproduce

What is the current bug behavior?

Counters return -1

What is the expected correct behavior?

To return the actual value

Output of checks

start=Time.now; [Gitlab::UsageData.approximate_counts, Time.now - start]
unknown OID 28: failed to recognize type of 'relfrozenxid'. It will be treated as String.
unknown OID 1034: failed to recognize type of 'relacl'. It will be treated as String.
=> [{:labels=>-1, :merge_requests=>-1, :notes=>-1, :todos=>-1}, 22.436592918]

Usages of the approximate counters

https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/usage_data.rb#L257

APPROXIMATE_COUNT_MODELS = [Label, MergeRequest, Note, Todo]
      def approximate_counts
        approx_counts = Gitlab::Database::Count.approximate_counts(APPROXIMATE_COUNT_MODELS)

        APPROXIMATE_COUNT_MODELS.each_with_object({}) do |model, result|
          key = model.name.underscore.pluralize.to_sym

          result[key] = approx_counts[model] || -1
        end
      end

Possible fixes

Use batch counting, add indexes

Edited by Alina Mihaila