diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2021-11-17 19:33:54 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2021-11-18 12:50:46 +0300 |
commit | 4fe8fd912146315da97855172cfff4ad1106af77 (patch) | |
tree | 79cf2f8bcaacd56b2f5698f35d3881f73159f864 | |
parent | ab692bf6a4b35d0e9d536d33cb4e248d50449847 (diff) |
Materialize valid_primaries view in RepositoryStoreCollector
RepositoryStoreCollector gathers metrics on repositories which don't
have a valid primary candidates available. This indicates the repository
is unavailable as the current primary is not valid and ther are no valid
candidates to failover to. The query is currently extremely inefficient
on some versions of Postgres as it ends up computing the full valid_primaries
view for each of the rows it checks. This doesn't seem to occur on all versions
of Postgres, namely 12.6 at least manages to push down the search criteria
inside the view. This commit fixes the situation by materializing the
valid_primaries view prior to querying it. This ensures the full view isn't
computed for all of the rows but rather Postgres just uses the pre-computed
result.
Changelog: performance
-rw-r--r-- | internal/praefect/datastore/collector.go | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/internal/praefect/datastore/collector.go b/internal/praefect/datastore/collector.go index be3b6f646..0877757a0 100644 --- a/internal/praefect/datastore/collector.go +++ b/internal/praefect/datastore/collector.go @@ -74,6 +74,11 @@ func (c *RepositoryStoreCollector) Collect(ch chan<- prometheus.Metric) { // they are either unhealthy or out of date. func (c *RepositoryStoreCollector) queryMetrics(ctx context.Context) (map[string]int, error) { rows, err := c.db.QueryContext(ctx, ` +WITH valid_primaries AS MATERIALIZED ( + SELECT repository_id + FROM valid_primaries +) + SELECT virtual_storage, COUNT(*) FROM repositories WHERE NOT EXISTS ( |