Add automated PostgreSQL performance index management

- Add scripts and SQL for base and supplementary indexes (total 23)
- Integrate index checks and auto-creation into Jellyfin startup
- Update csproj to include SQL files in build/publish output
- Add diagnostics, publish/copy scripts, and troubleshooting docs
- Provide detailed guides for migration, verification, and merging
- Ensures robust, automated, and well-documented index setup
This commit is contained in:
2026-02-28 15:13:04 -05:00
parent 79110d2afd
commit 5565dc3e05
35 changed files with 5469 additions and 0 deletions
@@ -452,6 +452,12 @@ public sealed class PostgresDatabaseProvider : IJellyfinDatabaseProvider
}
logger.LogInformation("Database table verification complete");
// Check for supplementary performance indexes
await CheckSupplementaryIndexesAsync(connection, cancellationToken).ConfigureAwait(false);
// Apply performance indexes from SQL script if missing
await ApplyPerformanceIndexesFromScriptAsync(connection, cancellationToken).ConfigureAwait(false);
}
finally
{
@@ -833,4 +839,151 @@ public sealed class PostgresDatabaseProvider : IJellyfinDatabaseProvider
await dbContext.Database.ExecuteSqlRawAsync(deleteAllQuery).ConfigureAwait(false);
}
/// <summary>
/// Checks if supplementary performance indexes exist and logs warnings if missing.
/// These indexes complement the base schema and improve specific query patterns.
/// </summary>
/// <param name="connection">The database connection.</param>
/// <param name="cancellationToken">Cancellation token.</param>
private async Task CheckSupplementaryIndexesAsync(System.Data.Common.DbConnection connection, CancellationToken cancellationToken)
{
try
{
var checkQuery = @"
SELECT indexname
FROM pg_indexes
WHERE indexname IN (
'idx_baseitems_type_isvirtualitem_topparentid',
'idx_baseitems_topparentid_isfolder',
'idx_baseitems_datecreated_filtered',
'idx_itemvaluesmap_itemvalueid_itemid',
'idx_activitylogs_userid_datecreated'
)
ORDER BY indexname";
var foundIndexes = new List<string>();
await using var command = connection.CreateCommand();
command.CommandText = checkQuery;
await using var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
{
foundIndexes.Add(reader.GetString(0));
}
var expectedIndexes = new[]
{
"idx_baseitems_type_isvirtualitem_topparentid",
"idx_baseitems_topparentid_isfolder",
"idx_baseitems_datecreated_filtered",
"idx_itemvaluesmap_itemvalueid_itemid",
"idx_activitylogs_userid_datecreated"
};
var missingIndexes = expectedIndexes.Except(foundIndexes).ToList();
if (missingIndexes.Count > 0)
{
logger.LogWarning(
"Found {FoundCount}/5 supplementary performance indexes. Missing: {MissingIndexes}",
foundIndexes.Count,
string.Join(", ", missingIndexes));
logger.LogWarning(
"Performance indexes will be created automatically from sql/all_performance_indexes.sql");
}
else
{
logger.LogInformation("All 5 supplementary performance indexes are present.");
}
}
catch (Exception ex)
{
logger.LogWarning(ex, "Failed to check supplementary indexes. This is non-critical and startup will continue.");
}
}
/// <summary>
/// Applies performance indexes from the SQL script if they are missing.
/// This ensures optimal query performance after database initialization.
/// </summary>
/// <param name="connection">The database connection.</param>
/// <param name="cancellationToken">Cancellation token.</param>
private async Task ApplyPerformanceIndexesFromScriptAsync(System.Data.Common.DbConnection connection, CancellationToken cancellationToken)
{
try
{
// Check if base performance indexes exist
var checkQuery = @"
SELECT COUNT(*)
FROM pg_indexes
WHERE schemaname = 'library'
AND indexname IN (
'baseitems_communityrating_idx',
'baseitems_datecreated_idx',
'baseitems_datemodified_idx',
'baseitems_parentid_idx',
'baseitems_premieredate_idx',
'baseitems_productionyear_idx',
'baseitems_seriespresentationuniquekey_idx',
'baseitems_sortname_idx',
'baseitems_topparentid_idx'
)";
await using (var command = connection.CreateCommand())
{
command.CommandText = checkQuery;
var result = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
var existingIndexCount = result != null ? Convert.ToInt32(result) : 0;
// If we have all 9 base indexes, assume performance indexes are already applied
if (existingIndexCount >= 9)
{
logger.LogDebug("Base performance indexes already exist ({Count}/9). Skipping SQL script execution.", existingIndexCount);
return;
}
logger.LogInformation("Found {Count}/9 base performance indexes. Applying SQL script to create missing indexes...", existingIndexCount);
}
// Find the SQL script path
var sqlScriptPath = Path.Combine(AppContext.BaseDirectory, "sql", "all_performance_indexes.sql");
if (!File.Exists(sqlScriptPath))
{
logger.LogWarning(
"Performance indexes SQL script not found at: {ScriptPath}. " +
"Database will function but may have suboptimal performance. " +
"Run 'Add-All-Indexes.bat' or apply sql/all_performance_indexes.sql manually.",
sqlScriptPath);
return;
}
logger.LogInformation("Executing performance indexes script: {ScriptPath}", sqlScriptPath);
// Read and execute the SQL script
var sqlScript = await File.ReadAllTextAsync(sqlScriptPath, cancellationToken).ConfigureAwait(false);
// Remove psql-specific commands (\echo, etc.) since we're executing via code
sqlScript = System.Text.RegularExpressions.Regex.Replace(sqlScript, @"\\echo.*$", string.Empty, System.Text.RegularExpressions.RegexOptions.Multiline);
await using (var command = connection.CreateCommand())
{
command.CommandText = sqlScript;
command.CommandTimeout = 600; // 10 minutes for index creation
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
logger.LogInformation("Performance indexes created successfully. Database is now fully optimized.");
}
catch (Exception ex)
{
logger.LogError(
ex,
"Failed to apply performance indexes from SQL script. " +
"Database will function but performance may be suboptimal. " +
"Consider running 'Add-All-Indexes.bat' or applying sql/all_performance_indexes.sql manually.");
}
}
}