From a30a769ef69efe0e31e46798f4cb0e3264300453 Mon Sep 17 00:00:00 2001
From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com>
Date: Sat, 24 Oct 2026 06:18:14 -0400
Subject: [PATCH 1/2] Darling on plain PostgreSQL: the blocking and deadlock
baselines stop at the purge horizon (#6706)
---
.../DarlingEventBaselineCoveredDaysTests.cs | 89 +++++++++++++++++--
.../PgBaselineProvider.cs | 80 ++++++++++++++++-
2 files changed, 163 insertions(+), 6 deletions(-)
diff --git a/Darling/Darling.Tests/DarlingEventBaselineCoveredDaysTests.cs b/Darling/Darling.Tests/DarlingEventBaselineCoveredDaysTests.cs
index 0d1250f27d..12a8f674c9 100644
--- a/Darling/Darling.Tests/DarlingEventBaselineCoveredDaysTests.cs
+++ b/Darling/Darling.Tests/DarlingEventBaselineCoveredDaysTests.cs
@@ -270,6 +270,18 @@ public sealed class DarlingEventBaselineCoveredDaysLiveTests
private static readonly DateTime AnalysisTime = new(2026, 3, 4, 14, 0, 0, DateTimeKind.Unspecified);
+ ///
+ /// #6706: the provider measures the raw tables' purge horizon from its clock. The seeded data is March 2026, so the clock is
+ /// set at the window's end (Mar 4 00:00 UTC): at the default 30-day retention the horizon is then the window start (Feb 2)
+ /// and every assertion below stands as it did before the clamp existed.
+ ///
+ private static readonly TimeProvider Clock = new FixedClock(new DateTimeOffset(2026, 3, 4, 0, 0, 0, TimeSpan.Zero));
+
+ private sealed class FixedClock(DateTimeOffset now) : TimeProvider
+ {
+ public override DateTimeOffset GetUtcNow() => now;
+ }
+
private static readonly DateTime[] Tuesdays =
[
new(2026, 2, 3, 14, 0, 0, DateTimeKind.Unspecified), new(2026, 2, 10, 14, 0, 0, DateTimeKind.Unspecified),
@@ -316,7 +328,7 @@ public async Task Live_AQuietHour_OnAServerThatHasCapturedEvents_IsARowWithMeanZ
await SeedEventAsync(connection, family, serverId, ElsewhereEvent, ct);
await TimescaleSupport.EnsureBaselineFallbackViewsAsync(connection, null, ct);
- var map = await new PgBaselineProvider(postgres).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
+ var map = await new PgBaselineProvider(postgres, timeProvider: Clock).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
Assert.Equal(24 * 7, map.Buckets.Count);
var tuesday = map.Buckets[(Hour, Tuesday)];
@@ -364,7 +376,7 @@ public async Task Live_ACollectorThatLoggedSuccessEveryHour_ForAServerWhoseSourc
await SeedQuietMonthAsync(connection, serverId, collector, ct);
await TimescaleSupport.EnsureBaselineFallbackViewsAsync(connection, null, ct);
- var map = await new PgBaselineProvider(postgres).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
+ var map = await new PgBaselineProvider(postgres, timeProvider: Clock).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
Assert.Empty(map.Buckets);
@@ -412,7 +424,7 @@ public async Task Live_EventsOnTwoOfFiveCoveredDays_DivideByFive_NotByTwo(string
await TimescaleSupport.EnsureBaselineFallbackViewsAsync(connection, null, ct);
- var map = await new PgBaselineProvider(postgres).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
+ var map = await new PgBaselineProvider(postgres, timeProvider: Clock).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
var tuesday = map.Buckets[(Hour, Tuesday)];
Assert.Equal(6.0 / 5.0, tuesday.Mean, 9);
@@ -463,7 +475,7 @@ await SeedRunsAsync(connection, serverId, collector, Tuesdays[4], Tuesdays[4].Ad
await SeedEventAsync(connection, family, serverId, ElsewhereEvent, ct);
await TimescaleSupport.EnsureBaselineFallbackViewsAsync(connection, null, ct);
- var map = await new PgBaselineProvider(postgres).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
+ var map = await new PgBaselineProvider(postgres, timeProvider: Clock).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct);
var tuesday = map.Buckets[(Hour, Tuesday)];
Assert.Equal(4L, tuesday.SampleCount);
@@ -507,7 +519,7 @@ public async Task Live_TwoCallsHoursApartOnOneUtcDay_ShareOneCompute_NextDayReco
await SeedEventAsync(connection, family, serverId, ElsewhereEvent, ct);
await TimescaleSupport.EnsureBaselineFallbackViewsAsync(connection, null, ct);
- var provider = new PgBaselineProvider(postgres);
+ var provider = new PgBaselineProvider(postgres, timeProvider: Clock);
var day = AnalysisTime.Date;
var (morning, firstReads) = await CommandCapture.CountBaselineReadsAsync(
@@ -535,6 +547,73 @@ public async Task Live_TwoCallsHoursApartOnOneUtcDay_ShareOneCompute_NextDayReco
}
}
+ ///
+ /// #6706: on a plain-view store, raw events are purged at the collector's retention while collection_log is kept twice
+ /// as long. With retention 7 days and runs logged back 35 days, the days before the horizon (Feb 25 00:00 for a clock of
+ /// Mar 4 00:00) have logged runs and no events, and used to count as measured zeros. Events sit only on the last seven days
+ /// (three on Tuesday Mar 3 at 14:10, one on Sunday Mar 1 at 09:10). The Tuesday 14:00 bucket is now Mar 3 alone, so its mean
+ /// is the three events over one day, not over five. Then the same data under the default 30-day retention reads as it did
+ /// before: five Tuesdays, mean 3/5.
+ ///
+ [Theory]
+ [InlineData("blocking")]
+ [InlineData("deadlock")]
+ public async Task Live_RetentionBelowTheWindow_CountsOnlyTheDaysTheRawTableStillHolds(string family)
+ {
+ var connectionString = Environment.GetEnvironmentVariable("DARLING_TEST_PG");
+ Assert.SkipWhen(string.IsNullOrEmpty(connectionString),
+ "Set DARLING_TEST_PG to a Postgres connection string to run the live event-baseline coverage test.");
+
+ var ct = TestContext.Current.CancellationToken;
+ var (metric, collector, serverId) = Family(family);
+
+ using var connection = new NpgsqlConnection(connectionString);
+ await connection.OpenAsync(ct);
+ await PgMigrations.MigrateAsync(connection, ct);
+ await CleanupAsync(connection, serverId, ct);
+ await ExecAsync(connection, ct, "DELETE FROM config.config_collector_schedules WHERE server_id IS NULL AND collector_name = $1", collector);
+
+ await using var postgres = NpgsqlDataSource.Create(connectionString!);
+ var bodySucceeded = false;
+ try
+ {
+ await SeedQuietMonthAsync(connection, serverId, collector, ct);
+ for (var i = 0; i < 3; i++)
+ {
+ await SeedEventAsync(connection, family, serverId, Tuesdays[4].AddMinutes(10 + i), ct);
+ }
+
+ await SeedEventAsync(connection, family, serverId, new DateTime(2026, 3, 1, 9, 10, 0, DateTimeKind.Unspecified), ct);
+ await TimescaleSupport.EnsureBaselineFallbackViewsAsync(connection, null, ct);
+
+ await ExecAsync(connection, ct,
+ "INSERT INTO config.config_collector_schedules (server_id, collector_name, retention_days) VALUES (NULL, $1, 7)", collector);
+
+ var short7 = (await new PgBaselineProvider(postgres, timeProvider: Clock).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct))
+ .Buckets[(Hour, Tuesday)];
+ Assert.Equal(3.0, short7.Mean, 9);
+ Assert.Equal(1L, short7.SampleCount);
+ Assert.Equal(1L, short7.DistinctDays);
+
+ await ExecAsync(connection, ct, "DELETE FROM config.config_collector_schedules WHERE server_id IS NULL AND collector_name = $1", collector);
+
+ var thirty = (await new PgBaselineProvider(postgres, timeProvider: Clock).GetBucketMapAsync(serverId, metric, AnalysisTime, AnalysisTime.AddHours(1), ct))
+ .Buckets[(Hour, Tuesday)];
+ Assert.Equal(3.0 / 5.0, thirty.Mean, 9);
+ Assert.Equal(5L, thirty.SampleCount);
+
+ bodySucceeded = true;
+ }
+ finally
+ {
+ await LiveStoreCleanup.RunAsync(connectionString!, bodySucceeded, async (cleanup, cleanupCt) =>
+ {
+ await ExecAsync(cleanup, cleanupCt, "DELETE FROM config.config_collector_schedules WHERE server_id IS NULL AND collector_name = $1", collector);
+ await TearDownAsync(cleanup, serverId, cleanupCt);
+ });
+ }
+ }
+
/// SUCCESS runs every 15 minutes from to with no rows, carrying when given.
private static Task SeedRunsAsync(
NpgsqlConnection connection, int serverId, string collector, DateTime from, DateTime to, string? errorMessage, int batch, CancellationToken ct) =>
diff --git a/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs b/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs
index f0497c7661..18056445b8 100644
--- a/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs
+++ b/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs
@@ -130,6 +130,9 @@ public class PgBaselineProvider
private readonly ILogger? _logger;
private readonly BaselineLocalClock _localClock;
+ /* #6706: the clock the plain-view event arms measure the purge horizon from. Tests set it; the service never does. */
+ private readonly TimeProvider _time;
+
/// Cache TTL — baselines are recomputed after this interval.
public static TimeSpan CacheTtl { get; set; } = TimeSpan.FromHours(1);
@@ -147,11 +150,14 @@ public class PgBaselineProvider
/// #3941: the process's , consulted on a miss in this
/// provider's own cache and handed every successful compute. Null (every caller before #3941) keeps a private
/// cache and nothing else.
- public PgBaselineProvider(NpgsqlDataSource postgres, ILogger? logger = null, BaselineCache? sharedCache = null)
+ /// #6706: the clock the Blocking and Deadlock arms measure the raw tables' purge horizon
+ /// from. Null is .
+ public PgBaselineProvider(NpgsqlDataSource postgres, ILogger? logger = null, BaselineCache? sharedCache = null, TimeProvider? timeProvider = null)
{
_postgres = postgres ?? throw new ArgumentNullException(nameof(postgres));
_logger = logger;
_shared = sharedCache;
+ _time = timeProvider ?? TimeProvider.System;
/* Information, not Warning: "this host cannot resolve that zone id" is a statement about the host's
configuration, made once per zone per process by the resolver itself — see BaselineLocalClock. */
_localClock = new BaselineLocalClock(message => _logger?.LogInformation("{Message}", message));
@@ -851,6 +857,77 @@ private async Task ChooseSupplyAsync(
return prefersSuccessor ? query : legacyQuery;
}
+ /// The collector whose raw rows back each event arm, and the baseline relation the arm reads them through (#6706).
+ internal static (string Collector, string View)? EventSupplyFor(string metricName) => metricName switch
+ {
+ MetricNames.Blocking => ("blocked_process_report", TimescaleSupport.BlockedProcessBaselineView),
+ MetricNames.Deadlock => ("deadlocks", TimescaleSupport.DeadlockBaselineView),
+ _ => null,
+ };
+
+ private const string ContinuousAggregatesExistSql = "SELECT to_regclass('timescaledb_information.continuous_aggregates') IS NOT NULL";
+
+ ///
+ /// #6706: is this baseline relation an ordinary view over the raw table, rather than a continuous aggregate or absent?
+ /// A continuous aggregate is itself a relkind='v' view, so the catalog check alone is not enough; the
+ /// timescaledb_information half is asked only when that view exists (a store without the extension has none).
+ ///
+ internal static string IsPlainBaselineViewSql(string view)
+ => $"SELECT EXISTS (SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'collect' AND c.relname = '{view}' AND c.relkind = 'v')";
+
+ internal static string IsContinuousAggregateSql(string view)
+ => $"SELECT EXISTS (SELECT 1 FROM timescaledb_information.continuous_aggregates WHERE view_schema = 'collect' AND view_name = '{view}')";
+
+ ///
+ /// #6706: on a store where the Blocking or Deadlock arm's event relation is a plain view over the raw table (no
+ /// TimescaleDB, or the fallback view of a partly built one), the events reach back only as far as the raw table's purge
+ /// horizon, while the logged side reads collection_log, kept twice as long. With the collector's retention
+ /// below the 30-day window, every day between the horizon and the window start has logged runs and no events, and each
+ /// would count as a measured zero. The window starts no earlier than now minus the retention in force (the fleet override
+ /// or the default, , the rule the purge applies; the purge never
+ /// removes a row newer than that). A continuous aggregate keeps 35 days whatever raw retention is, so it is left alone, as is
+ /// every other metric. At the default 30 days the floor sits within a day of the window start (the window ends at the UTC
+ /// midnight), so a default store changes only in the hours of its oldest day that the purge may already have taken. The arm's
+ /// text is unchanged: it still reads $2.
+ ///
+ private async Task ClampToPlainViewPurgeHorizonAsync(
+ NpgsqlConnection connection, string metricName, DateTime windowStart, CancellationToken cancellationToken)
+ {
+ if (EventSupplyFor(metricName) is not var (collector, view))
+ {
+ return windowStart;
+ }
+
+ using (var plain = new NpgsqlCommand(IsPlainBaselineViewSql(view), connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds })
+ {
+ if (await plain.ExecuteScalarAsync(cancellationToken) is not true)
+ {
+ return windowStart;
+ }
+ }
+
+ using (var extension = new NpgsqlCommand(ContinuousAggregatesExistSql, connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds })
+ {
+ if (await extension.ExecuteScalarAsync(cancellationToken) is true)
+ {
+ using var aggregate = new NpgsqlCommand(IsContinuousAggregateSql(view), connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds };
+ if (await aggregate.ExecuteScalarAsync(cancellationToken) is true)
+ {
+ return windowStart;
+ }
+ }
+ }
+
+ long retentionDays;
+ using (var retention = new NpgsqlCommand("SELECT " + DarlingRetentionHorizons.FleetRetentionDaysSql(collector), connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds })
+ {
+ retentionDays = Convert.ToInt64(await retention.ExecuteScalarAsync(cancellationToken), System.Globalization.CultureInfo.InvariantCulture);
+ }
+
+ var horizon = DateTime.SpecifyKind(_time.GetUtcNow().UtcDateTime.AddDays(-retentionDays), DateTimeKind.Unspecified);
+ return horizon > windowStart ? horizon : windowStart;
+ }
+
private static async Task OldestBucketAsync(NpgsqlConnection connection, string relation, int serverId, CancellationToken cancellationToken)
{
using var probe = new NpgsqlCommand(SupplyOldestBucketSql(relation), connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds };
@@ -894,6 +971,7 @@ analyze_server and compare_analysis (BaselineCache) without changing anyone's an
if (keys is null)
{
query = await ChooseSupplyAsync(connection, serverId, metricName, query, windowStart, cancellationToken);
+ windowStart = await ClampToPlainViewPurgeHorizonAsync(connection, metricName, windowStart, cancellationToken);
}
/* #3653 Q6: the target's clock over this window, read on the same connection and INSIDE this try on
From f6b7a8d3bde74538167eba8a499b50aa0a3be115 Mon Sep 17 00:00:00 2001
From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com>
Date: Sat, 24 Oct 2026 08:00:35 -0400
Subject: [PATCH 2/2] Reuse TimescaleSupport.ResolveMaterializationAsync for
the plain-view clamp's aggregate check (#6706)
---
.../PgBaselineProvider.cs | 23 +++++--------------
1 file changed, 6 insertions(+), 17 deletions(-)
diff --git a/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs b/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs
index 588bef3ec1..924c0bf7a4 100644
--- a/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs
+++ b/Darling/PerformanceMonitor.Darling.Analysis/PgBaselineProvider.cs
@@ -865,19 +865,14 @@ private async Task ChooseSupplyAsync(
_ => null,
};
- private const string ContinuousAggregatesExistSql = "SELECT to_regclass('timescaledb_information.continuous_aggregates') IS NOT NULL";
-
///
/// #6706: is this baseline relation an ordinary view over the raw table, rather than a continuous aggregate or absent?
/// A continuous aggregate is itself a relkind='v' view, so the catalog check alone is not enough; the
- /// timescaledb_information half is asked only when that view exists (a store without the extension has none).
+ /// aggregate half is (#3684: one definition, not a copy).
///
internal static string IsPlainBaselineViewSql(string view)
=> $"SELECT EXISTS (SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'collect' AND c.relname = '{view}' AND c.relkind = 'v')";
- internal static string IsContinuousAggregateSql(string view)
- => $"SELECT EXISTS (SELECT 1 FROM timescaledb_information.continuous_aggregates WHERE view_schema = 'collect' AND view_name = '{view}')";
-
///
/// #6706: on a store where the Blocking or Deadlock arm's event relation is a plain view over the raw table (no
/// TimescaleDB, or the fallback view of a partly built one), the events reach back only as far as the raw table's purge
@@ -885,8 +880,9 @@ internal static string IsContinuousAggregateSql(string view)
/// below the 30-day window, every day between the horizon and the window start has logged runs and no events, and each
/// would count as a measured zero. The window starts no earlier than now minus the retention in force (the fleet override
/// or the default, , the rule the purge applies; the purge never
- /// removes a row newer than that). A continuous aggregate keeps 35 days whatever raw retention is, so it is left alone, as is
- /// every other metric. At the default 30 days the floor sits within a day of the window start (the window ends at the UTC
+ /// removes a row newer than that). A continuous aggregate keeps 35 days whatever raw retention is, so it is left alone (the
+ /// aggregate check is : null for a plain view or a store without
+ /// the extension), as is every other metric. At the default 30 days the floor sits within a day of the window start (the window ends at the UTC
/// midnight), so a default store changes only in the hours of its oldest day that the purge may already have taken. The arm's
/// text is unchanged: it still reads $2.
///
@@ -906,16 +902,9 @@ private async Task ClampToPlainViewPurgeHorizonAsync(
}
}
- using (var extension = new NpgsqlCommand(ContinuousAggregatesExistSql, connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds })
+ if (await TimescaleSupport.ResolveMaterializationAsync(connection, view, cancellationToken) is not null)
{
- if (await extension.ExecuteScalarAsync(cancellationToken) is true)
- {
- using var aggregate = new NpgsqlCommand(IsContinuousAggregateSql(view), connection) { CommandTimeout = DarlingAnalysisService.AnalysisCommandTimeoutSeconds };
- if (await aggregate.ExecuteScalarAsync(cancellationToken) is true)
- {
- return windowStart;
- }
- }
+ return windowStart;
}
long retentionDays;