Remove fallback code for database index creation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<NameOfLastUsedPublishProfile>D:\Projects\pgsql-jellyfin\Jellyfin.Server\Properties\PublishProfiles\SelfContained-Win-x64.pubxml</NameOfLastUsedPublishProfile>
|
||||
<NameOfLastUsedPublishProfile>D:\Projects\pgsql-jellyfin\Jellyfin.Server\Properties\PublishProfiles\SelfContained-Linux-x64.pubxml</NameOfLastUsedPublishProfile>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
-153
@@ -476,12 +476,6 @@ 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
|
||||
{
|
||||
@@ -874,151 +868,4 @@ 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user