From 721fca6145c98478c52b02603ee84abc809cdd2b Mon Sep 17 00:00:00 2001 From: Valery Meleshkin Date: Wed, 14 Dec 2022 18:19:45 +0100 Subject: [PATCH] Fixing the query behind chunks_uncompressed metric by making it rely on a function used by the maintenance jobs. It should also fix for #1741 --- pkg/pgmodel/metrics/database/metrics.go | 30 ++++++++++++------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/pkg/pgmodel/metrics/database/metrics.go b/pkg/pgmodel/metrics/database/metrics.go index aea9d053b4..3a52403b49 100644 --- a/pkg/pgmodel/metrics/database/metrics.go +++ b/pkg/pgmodel/metrics/database/metrics.go @@ -149,6 +149,12 @@ var metrics = []metricQueryWrap{ Name: "chunks_metrics_uncompressed_count", Help: "The number of metrics chunks soon to be compressed by maintenance jobs.", }, + ), + customPollConfig: updateAtMostEvery(9 * time.Minute), + query: `SELECT coalesce(sum(jsonb_array_length(chunks_to_compress)), 0)::BIGINT AS uncompressed + FROM _prom_catalog.metric_chunks_that_need_to_be_compressed(INTERVAL '1 hour');`, + }, { + metrics: gauges( prometheus.GaugeOpts{ Namespace: util.PromNamespace, Subsystem: "sql_database", @@ -157,24 +163,16 @@ var metrics = []metricQueryWrap{ }, ), customPollConfig: updateAtMostEvery(9 * time.Minute), - query: `WITH chunk_candidates AS MATERIALIZED ( - SELECT chcons.dimension_slice_id, h.table_name, h.schema_name - FROM _timescaledb_catalog.chunk_constraint chcons - INNER JOIN _timescaledb_catalog.chunk c ON c.id = chcons.chunk_id - INNER JOIN _timescaledb_catalog.hypertable h ON h.id = c.hypertable_id - WHERE c.dropped IS FALSE - AND h.compression_state = 1 -- compression_enabled = TRUE - AND (c.status & 1) != 1 -- only check for uncompressed chunks - ) - SELECT - count(*) FILTER(WHERE m.delay_compression_until IS NULL OR m.delay_compression_until < now())::BIGINT AS uncompressed, - count(*) FILTER(WHERE m.delay_compression_until IS NOT NULL AND m.delay_compression_until >= now())::BIGINT AS delayed_compression - FROM chunk_candidates cc + query: `SELECT count(*)::BIGINT AS delayed_compression + FROM _prom_catalog.metric m + INNER JOIN _timescaledb_catalog.chunk c ON (c.schema_name = m.table_schema AND c.table_name = m.table_schema) + INNER JOIN _timescaledb_catalog.chunk_constraint cc ON (cc.chunk_id = c.id) INNER JOIN _timescaledb_catalog.dimension_slice ds ON ds.id = cc.dimension_slice_id - INNER JOIN _prom_catalog.metric m ON (m.table_name = cc.table_name AND m.table_schema = cc.schema_name) WHERE NOT m.is_view - AND ds.range_start <= _timescaledb_internal.time_to_internal(now() - interval '1 hour') - AND ds.range_end <= _timescaledb_internal.time_to_internal(now() - interval '1 hour')`, + AND m.delay_compression_until IS NOT NULL + AND m.delay_compression_until >= now() + AND ds.range_start <= _timescaledb_internal.time_to_internal(now() - interval '1 hour') + AND ds.range_end <= _timescaledb_internal.time_to_internal(now() - interval '1 hour')`, }, { metrics: gauges( prometheus.GaugeOpts{