From f2ddde174735716729affc3353d2089dbe3ccbe4 Mon Sep 17 00:00:00 2001 From: Wendell Jones Date: Sat, 7 Mar 2026 11:52:55 -0500 Subject: [PATCH] Remove fallback code for database index creation --- Jellyfin.Server/Jellyfin.Server.csproj.user | 2 +- .../PostgresDatabaseProvider.cs | 153 ------------------ 2 files changed, 1 insertion(+), 154 deletions(-) diff --git a/Jellyfin.Server/Jellyfin.Server.csproj.user b/Jellyfin.Server/Jellyfin.Server.csproj.user index bd1700e6..55f2b8df 100644 --- a/Jellyfin.Server/Jellyfin.Server.csproj.user +++ b/Jellyfin.Server/Jellyfin.Server.csproj.user @@ -1,6 +1,6 @@  - D:\Projects\pgsql-jellyfin\Jellyfin.Server\Properties\PublishProfiles\SelfContained-Win-x64.pubxml + D:\Projects\pgsql-jellyfin\Jellyfin.Server\Properties\PublishProfiles\SelfContained-Linux-x64.pubxml \ No newline at end of file diff --git a/src/Jellyfin.Database/Jellyfin.Database.Providers.Postgres/PostgresDatabaseProvider.cs b/src/Jellyfin.Database/Jellyfin.Database.Providers.Postgres/PostgresDatabaseProvider.cs index 78b7213c..73aa792e 100644 --- a/src/Jellyfin.Database/Jellyfin.Database.Providers.Postgres/PostgresDatabaseProvider.cs +++ b/src/Jellyfin.Database/Jellyfin.Database.Providers.Postgres/PostgresDatabaseProvider.cs @@ -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); } - - /// - /// Checks if supplementary performance indexes exist and logs warnings if missing. - /// These indexes complement the base schema and improve specific query patterns. - /// - /// The database connection. - /// Cancellation token. - 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(); - - 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."); - } - } - - /// - /// Applies performance indexes from the SQL script if they are missing. - /// This ensures optimal query performance after database initialization. - /// - /// The database connection. - /// Cancellation token. - 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."); - } - } }