Path: redb/redb.Core/Providers/Base/MaintenanceProviderBase.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Providers/Base/MaintenanceProviderBase.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using redb.Core.Data;
using redb.Core.Query;
namespace redb.Core.Providers.Base;
///
/// The single implementation of : every provider-specific
/// difference lives in the SQL the dialect returns, normalized to ONE column-alias contract,
/// so the subclasses degenerate to wiring their dialect in (same shape as ValidationProvider).
///
public abstract class MaintenanceProviderBase : IMaintenanceProvider
{
protected IRedbContext Context { get; }
protected ISqlDialect Sql { get; }
protected ILogger? Logger { get; }
/// SQLite's bounded-ANALYZE limit; other engines ignore it. See configuration.
private readonly int _analysisLimit;
protected MaintenanceProviderBase(
IRedbContext context,
ISqlDialect sql,
int analysisLimit,
ILogger? logger = null)
{
Context = context ?? throw new System.ArgumentNullException(nameof(context));
Sql = sql ?? throw new System.ArgumentNullException(nameof(sql));
_analysisLimit = analysisLimit;
Logger = logger;
}
///
public virtual async Task AnalyzeAsync(CancellationToken cancellationToken = default)
{
var sql = Sql.Maintenance_Analyze(_analysisLimit);
Logger?.LogInformation("REDB maintenance: refreshing planner statistics ({Sql})", sql);
await Context.ExecuteAsync(sql, System.Array.Empty(), cancellationToken);
}
///
public virtual async Task AnalyzeTableAsync(string table, string? schema = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(table))
throw new ArgumentException("Table name is required.", nameof(table));
// An identifier cannot travel as a parameter, and this one comes from an operator's
// screen: it is checked against the catalogs first, and the dialect quotes what survives.
var exists = await Context.ExecuteScalarAsync(
Sql.Maintenance_SelectTableExists(),
new object[] { table, (object?)schema ?? DBNull.Value },
cancellationToken);
if (exists <= 0)
throw new InvalidOperationException(
$"Table '{(schema == null ? table : schema + "." + table)}' was not found in this database, " +
"so its planner statistics were not refreshed. Names come from GetTableStatsAsync().");
var sql = Sql.Maintenance_AnalyzeTable(schema, table, _analysisLimit);
Logger?.LogInformation("REDB maintenance: refreshing planner statistics of one table ({Sql})", sql);
await Context.ExecuteAsync(sql, System.Array.Empty(), cancellationToken);
}
///
public virtual async Task> GetIndexStatsAsync(CancellationToken cancellationToken = default)
{
// The dialect SQL normalizes each engine's catalogs to one alias contract, so this is
// the only mapping in existence. The single allowed branch: the dialect's fallback form,
// for SQLite without dbstat (no sizes) and SQL Server without VIEW SERVER STATE (no counters).
List rows;
try
{
rows = await Context.QueryAsync(
Sql.Maintenance_SelectIndexStats(), System.Array.Empty(), cancellationToken);
}
catch (System.Exception ex) when (Sql.Maintenance_SelectIndexStatsNoSize() is { } fallback
&& fallback != Sql.Maintenance_SelectIndexStats())
{
Logger?.LogDebug(ex,
"REDB maintenance: index-stats query failed (dbstat not compiled in, or no " +
"VIEW SERVER STATE right); falling back to the dialect's reduced form.");
rows = await Context.QueryAsync(
fallback, System.Array.Empty(), cancellationToken);
}
return rows.Select(ToStatistics).ToList();
}
///
public virtual async Task> GetTableStatsAsync(CancellationToken cancellationToken = default)
{
// The optional parts of a SQLite database decide how much of this question can be answered:
// dbstat may not be compiled in, and sqlite_stat1 does not exist until the first ANALYZE -
// a statement naming a missing table does not even prepare. The dialect lists the forms in
// decreasing capability, and the first one that runs wins; the last failure is rethrown.
var forms = Sql.Maintenance_SelectTableStatsForms();
List? rows = null;
for (var i = 0; i < forms.Count; i++)
{
try
{
rows = await Context.QueryAsync(
forms[i], System.Array.Empty(), cancellationToken);
break;
}
catch (System.Exception ex) when (i < forms.Count - 1)
{
Logger?.LogDebug(ex,
"REDB maintenance: table-stats form {Form} of {Count} did not run on this database " +
"(an optional catalog is missing); trying the next, less detailed one.",
i + 1, forms.Count);
}
}
foreach (var row in rows!)
row.IsRedbOwned = IsRedbTable(row.Table);
return rows;
}
///
public virtual async Task GetStatisticsWindowAsync(CancellationToken cancellationToken = default)
{
var rows = await Context.QueryAsync(
Sql.Maintenance_SelectStatisticsWindow(), System.Array.Empty(), cancellationToken);
return rows.FirstOrDefault() ?? new StatisticsWindow();
}
// === WHAT MUST SURVIVE ===
///
/// Tables of the redb type system. Their indexes are read on every scheme sync, every query
/// compilation and every materialization, and some of them are read so rarely that a usage
/// counter shows zero - which is exactly how a well-meant cleanup removes them.
///
private static readonly HashSet MetadataTables = new(StringComparer.OrdinalIgnoreCase)
{
"_types", "_schemes", "_structures", "_scheme_metadata_cache", "_dependencies",
"_functions", "_lists", "_links", "_migrations"
};
/// Tables redb checks permissions against.
private static readonly HashSet SecurityTables = new(StringComparer.OrdinalIgnoreCase)
{
"_users", "_roles", "_users_roles", "_permissions"
};
/// Tables holding redb data.
private static readonly HashSet DataTables = new(StringComparer.OrdinalIgnoreCase)
{
"_objects", "_values", "_list_items"
};
///
/// A table of the redb schema. By table name, never by index name: the schema carries both
/// hand-named indexes (IX__values__Object_not_null) and constraint-born ones the engine names
/// itself, and only the table is a reliable marker.
///
private static bool IsRedbTable(string table) => table.StartsWith("_", StringComparison.Ordinal);
private static IndexCriticality ReasonFor(IndexStatsRow row)
{
if (MetadataTables.Contains(row.Table)) return IndexCriticality.RedbMetadata;
if (SecurityTables.Contains(row.Table)) return IndexCriticality.RedbSecurity;
if (DataTables.Contains(row.Table)) return IndexCriticality.RedbData;
if (IsRedbTable(row.Table)) return IndexCriticality.RedbData;
if (row.IsPrimaryKey == true) return IndexCriticality.PrimaryKey;
if (row.IsUniqueConstraint == true || row.IsUnique == true) return IndexCriticality.Unique;
if (row.BacksForeignKey == true) return IndexCriticality.ForeignKey;
return IndexCriticality.None;
}
private static IndexStatistics ToStatistics(IndexStatsRow row)
{
var reason = ReasonFor(row);
return new IndexStatistics
{
Schema = row.Schema,
Table = row.Table,
Name = row.Name,
IsUnique = row.IsUnique,
Columns = Split(row.ColumnsCsv),
IncludedColumns = Split(row.IncludedColumnsCsv),
IsPrimaryKey = row.IsPrimaryKey == true,
IsUniqueConstraint = row.IsUniqueConstraint == true,
IsClustered = row.IsClustered,
BacksForeignKey = row.BacksForeignKey == true,
IsRedbOwned = IsRedbTable(row.Table),
IsSystemCritical = reason != IndexCriticality.None,
CriticalReason = reason,
SizeBytes = row.SizeBytes,
EstimatedRows = row.EstimatedRows,
Seeks = row.Seeks,
Scans = row.Scans,
Updates = row.Updates,
LastUsed = row.LastUsed
};
}
///
/// Column lists travel as one comma-separated value so the catalogs of three engines keep
/// fitting one row per index; the list the caller reads is built here, once.
///
private static IReadOnlyList Split(string? csv)
=> string.IsNullOrEmpty(csv)
? Array.Empty()
: csv.Split(',').Select(p => p.Trim()).Where(p => p.Length > 0).ToArray();
/// The row shape the dialect SQL produces; the public model is built from it.
private sealed class IndexStatsRow
{
public string? Schema { get; set; }
public string Table { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public bool? IsUnique { get; set; }
public string? ColumnsCsv { get; set; }
public string? IncludedColumnsCsv { get; set; }
public bool? IsPrimaryKey { get; set; }
public bool? IsUniqueConstraint { get; set; }
public bool? IsClustered { get; set; }
public bool? BacksForeignKey { get; set; }
public long? SizeBytes { get; set; }
public long? EstimatedRows { get; set; }
public long? Seeks { get; set; }
public long? Scans { get; set; }
public long? Updates { get; set; }
public DateTimeOffset? LastUsed { get; set; }
}
}