Path: redb/redb.Core/Providers/IMaintenanceProvider.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/IMaintenanceProvider.cs
----
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace redb.Core.Providers
{
///
/// Storage maintenance: planner statistics and index health. Born out of the 2026-09-10
/// incident where stale planner statistics after a bulk seed turned every plan into fiction
/// (a 30ms tree query ran for a second), and index health was visible only by hand-written
/// catalog queries per database.
///
/// Everything here is read-only except the two AnalyzeAsync overloads, and both are
/// explicit calls. Reorganising the storage (VACUUM, REINDEX, resetting counters) is not
/// offered on purpose (owner decision 2026-09-21): the facade reports what the database is
/// doing so an operator can hand the findings to a DBA, and a button that locks a production
/// table must not be one click away in a dashboard.
///
///
public interface IMaintenanceProvider
{
///
/// Refreshes the planner statistics of the database (PostgreSQL ANALYZE,
/// MSSQL sp_updatestats, SQLite PRAGMA analysis_limit; ANALYZE).
/// An EXPLICIT call by design: on a large database it can run for minutes, so it must
/// never be a silent side effect of a save. Call it after bulk writes - a seed, an
/// import, a migration - or every estimate the planner reads will be stale.
///
Task AnalyzeAsync(CancellationToken cancellationToken = default);
///
/// Refreshes the planner statistics of ONE table (PostgreSQL ANALYZE t, MSSQL
/// UPDATE STATISTICS t, SQLite ANALYZE t - every engine supports it), for
/// the usual case where the operator knows the table that brought them here and a
/// whole-database pass would run for minutes.
///
/// Table name, unquoted, as the catalogs report it.
/// Schema of the table; null means the engine's default
/// (public, dbo) and is the only option on SQLite.
/// The name is empty.
/// No such table in the database. The name is
/// checked against the catalogs and quoted by the dialect before it reaches SQL: it comes
/// from a dashboard, and an identifier cannot be a parameter.
Task AnalyzeTableAsync(string table, string? schema = null, CancellationToken cancellationToken = default);
///
/// Index statistics of the user tables, read from the database catalogs. Fields the
/// engine cannot report are null - see each property; nothing is guessed.
///
Task> GetIndexStatsAsync(CancellationToken cancellationToken = default);
///
/// Table statistics of the user tables: size, rows, the size of their indexes, and when
/// the engine last refreshed statistics or reclaimed dead rows. The other half of the
/// picture an operator needs - says what the indexes are,
/// this says what they sit on.
///
Task> GetTableStatsAsync(CancellationToken cancellationToken = default);
///
/// Since when the usage counters of have been counting,
/// and whether this node is a replica. Without it "this index served no read" says
/// nothing: the counters may have been reset or the server restarted a minute ago, and on
/// a PostgreSQL replica they count only this node's own reads.
///
Task GetStatisticsWindowAsync(CancellationToken cancellationToken = default);
}
///
/// One index as the database catalog sees it. Honestly nullable: each provider fills what
/// its engine can report (PostgreSQL and MSSQL carry usage counters, SQLite does not;
/// MSSQL usage counters reset on server restart; needs PostgreSQL 16+).
///
public class IndexStatistics
{
/// Schema of the table; null on engines without schemas (SQLite).
public string? Schema { get; set; }
/// Table the index belongs to.
public string Table { get; set; } = string.Empty;
/// Index name.
public string Name { get; set; } = string.Empty;
/// Whether the index is unique; null when the catalog does not say.
public bool? IsUnique { get; set; }
/// Key columns in index order. Empty only when the engine could not report them.
public IReadOnlyList Columns { get; set; } = Array.Empty();
///
/// Columns carried but not keyed (MSSQL INCLUDE); empty elsewhere. Without this a
/// covering index is indistinguishable from a composite one.
///
public IReadOnlyList IncludedColumns { get; set; } = Array.Empty();
/// The index backs a primary key.
public bool IsPrimaryKey { get; set; }
/// The index backs a unique constraint (as opposed to a plain unique index).
public bool IsUniqueConstraint { get; set; }
/// MSSQL clustered index - the table itself; null on engines without the notion.
public bool? IsClustered { get; set; }
/// The index serves a foreign key, so dropping it turns key checks into scans.
public bool BacksForeignKey { get; set; }
/// The index belongs to a table of the redb schema (a name starting with '_').
public bool IsRedbOwned { get; set; }
///
/// The index must not be dropped, whatever its usage counters say (owner decision
/// 2026-09-21). True for every index of the redb schema and for any index that backs a
/// primary key, a unique constraint or a foreign key. says which.
///
public bool IsSystemCritical { get; set; }
/// Why is set; when it is not.
public IndexCriticality CriticalReason { get; set; }
/// On-disk size in bytes; null when unavailable (SQLite without dbstat).
public long? SizeBytes { get; set; }
/// Planner's row estimate for the index; null when unavailable.
public long? EstimatedRows { get; set; }
/// Point-lookup uses. PostgreSQL reports one combined scan counter here.
public long? Seeks { get; set; }
/// Range/full scans (MSSQL); null where the engine does not split the counter.
public long? Scans { get; set; }
/// Maintenance writes into the index (MSSQL); null elsewhere.
public long? Updates { get; set; }
/// Last time the index served a read; PostgreSQL 16+ and MSSQL only.
public DateTimeOffset? LastUsed { get; set; }
}
/// Why an index must survive regardless of how often it is read.
public enum IndexCriticality
{
/// Nothing structural: the usage counters are the whole story.
None = 0,
/// A table of the redb type system: schemes, structures, types, their cache, migrations.
RedbMetadata = 1,
/// A table redb checks permissions against: users, roles, permissions.
RedbSecurity = 2,
/// A table holding redb data: objects, values, list items.
RedbData = 3,
/// Backs a primary key.
PrimaryKey = 4,
/// Enforces uniqueness.
Unique = 5,
/// Serves a foreign key.
ForeignKey = 6
}
///
/// One table as the database catalog sees it. Nullable in the same spirit as
/// : what the engine does not report stays null.
///
public class TableStatistics
{
/// Schema of the table; null on engines without schemas (SQLite).
public string? Schema { get; set; }
/// Table name.
public string Table { get; set; } = string.Empty;
/// The table belongs to the redb schema (a name starting with '_').
public bool IsRedbOwned { get; set; }
/// Planner's row estimate; null when unavailable.
public long? EstimatedRows { get; set; }
/// Size of the table's own data in bytes; null when unavailable.
public long? DataSizeBytes { get; set; }
/// Total size of the table's indexes in bytes; null when unavailable.
public long? IndexesSizeBytes { get; set; }
/// Rows dead but not yet reclaimed (PostgreSQL); null elsewhere.
public long? DeadRows { get; set; }
/// When statistics of this table were last refreshed by an explicit request; null when the engine does not record it.
public DateTimeOffset? LastAnalyze { get; set; }
/// When the engine refreshed them on its own (PostgreSQL autoanalyze); null elsewhere.
public DateTimeOffset? LastAutoAnalyze { get; set; }
/// When dead rows were last reclaimed (PostgreSQL vacuum); null elsewhere.
public DateTimeOffset? LastVacuum { get; set; }
///
/// Whether the engine holds any statistics for this table at all. SQLite records no
/// dates, so this is all it can say: ANALYZE has run at least once, or never.
///
public bool? HasStatistics { get; set; }
}
///
/// The window the usage counters of
/// cover, and whether this node is a replica.
///
public class StatisticsWindow
{
///
/// Since when the counters have been counting: the last statistics reset (PostgreSQL) or
/// the start of the instance (MSSQL). Null where the engine keeps no counters (SQLite) or
/// has never reset them.
///
public DateTimeOffset? CountersSince { get; set; }
///
/// The node is a read-only replica (PostgreSQL in recovery). Its counters describe this
/// node alone, so an index unused here may be busy on the primary. Null where the notion
/// does not apply.
///
public bool? IsReplica { get; set; }
}
}