Postgres perf, path migration, and shutdown race fixes
- Major query performance boost: replaced correlated subqueries with DistinctBy() in BaseItemRepository, added episode deduplication index, and increased default command timeout to 120s. - Fixed LibraryMonitor shutdown race: added disposal checks and exception handling in FileRefresher to prevent ObjectDisposedException. - Added PowerShell and SQL utilities for fixing Linux paths in config files and database; new scripts for WebDir and path migration. - Auto-fix for WebDir in startup.json on load; new helper scripts and docs. - Added automated weekly DB performance monitoring scripts and reporting. - Expanded documentation and README for all fixes, scripts, and migration steps. - Updated SQL scripts for index creation and diagnostics; improved output handling. - Updated db-config defaults and added new diagnostic/report files.
This commit is contained in:
@@ -0,0 +1,396 @@
|
||||
#Requires -Version 5.1
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Automated weekly performance monitoring for Jellyfin PostgreSQL database
|
||||
|
||||
.DESCRIPTION
|
||||
This script:
|
||||
1. Runs comprehensive diagnostics
|
||||
2. Analyzes slow queries
|
||||
3. Tracks index usage over time
|
||||
4. Generates weekly performance reports
|
||||
5. Can be scheduled via Task Scheduler
|
||||
|
||||
.PARAMETER OutputDirectory
|
||||
Directory to save reports (default: reports/weekly)
|
||||
|
||||
.PARAMETER EmailReport
|
||||
If specified, emails the report (requires email configuration)
|
||||
|
||||
.PARAMETER CompareWithPrevious
|
||||
Compare current stats with previous week
|
||||
|
||||
.EXAMPLE
|
||||
.\Weekly-Performance-Monitor.ps1
|
||||
|
||||
.EXAMPLE
|
||||
.\Weekly-Performance-Monitor.ps1 -OutputDirectory "C:\Reports\Jellyfin" -CompareWithPrevious
|
||||
|
||||
.EXAMPLE
|
||||
# Schedule weekly via Task Scheduler
|
||||
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Path\To\Weekly-Performance-Monitor.ps1"
|
||||
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 6am
|
||||
Register-ScheduledTask -TaskName "Jellyfin DB Monitor" -Action $action -Trigger $trigger
|
||||
|
||||
.NOTES
|
||||
Author: Generated for Jellyfin PostgreSQL Project
|
||||
Date: 2026-03-05
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter()]
|
||||
[string]$OutputDirectory = "reports\weekly",
|
||||
|
||||
[Parameter()]
|
||||
[switch]$EmailReport,
|
||||
|
||||
[Parameter()]
|
||||
[switch]$CompareWithPrevious
|
||||
)
|
||||
|
||||
# ============================================================================
|
||||
# Configuration
|
||||
# ============================================================================
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$ScriptRoot = Split-Path -Parent $PSCommandPath
|
||||
$ProjectRoot = Split-Path -Parent $ScriptRoot
|
||||
|
||||
# Load database configuration
|
||||
. "$ProjectRoot\scripts\db-config.ps1"
|
||||
|
||||
# ============================================================================
|
||||
# Functions
|
||||
# ============================================================================
|
||||
|
||||
function Write-ColorOutput {
|
||||
param(
|
||||
[string]$Message,
|
||||
[ConsoleColor]$Color = 'White'
|
||||
)
|
||||
Write-Host $Message -ForegroundColor $Color
|
||||
}
|
||||
|
||||
function Get-ReportTimestamp {
|
||||
return Get-Date -Format "yyyy-MM-dd_HHmmss"
|
||||
}
|
||||
|
||||
function Get-WeekNumber {
|
||||
$date = Get-Date
|
||||
$culture = [System.Globalization.CultureInfo]::CurrentCulture
|
||||
return $culture.Calendar.GetWeekOfYear($date,
|
||||
[System.Globalization.CalendarWeekRule]::FirstDay,
|
||||
[DayOfWeek]::Monday)
|
||||
}
|
||||
|
||||
function Test-DatabaseConnection {
|
||||
Write-ColorOutput "`nTesting database connection..." -Color Cyan
|
||||
|
||||
try {
|
||||
$result = & $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c "SELECT version();" 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-ColorOutput "[OK] Database connection successful" -Color Green
|
||||
return $true
|
||||
} else {
|
||||
Write-ColorOutput "[ERROR] Database connection failed: $result" -Color Red
|
||||
return $false
|
||||
}
|
||||
} catch {
|
||||
Write-ColorOutput "[ERROR] Database connection error: $_" -Color Red
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-DiagnosticReport {
|
||||
param([string]$OutputFile)
|
||||
|
||||
Write-ColorOutput "`nRunning comprehensive diagnostics..." -Color Cyan
|
||||
|
||||
try {
|
||||
& $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f "$ProjectRoot\sql\diagnostics.sql" > $OutputFile 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-ColorOutput "[OK] Diagnostics completed: $OutputFile" -Color Green
|
||||
return $true
|
||||
} else {
|
||||
Write-ColorOutput "[WARN] Diagnostics completed with warnings" -Color Yellow
|
||||
return $true
|
||||
}
|
||||
} catch {
|
||||
Write-ColorOutput "[ERROR] Diagnostics failed: $_" -Color Red
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-QueryAnalysis {
|
||||
param([string]$OutputFile)
|
||||
|
||||
Write-ColorOutput "`nAnalyzing slow queries..." -Color Cyan
|
||||
|
||||
try {
|
||||
& $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f "$ProjectRoot\sql\query-analysis.sql" > $OutputFile 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-ColorOutput "[OK] Query analysis completed: $OutputFile" -Color Green
|
||||
return $true
|
||||
} else {
|
||||
Write-ColorOutput "[WARN] Query analysis completed with warnings" -Color Yellow
|
||||
return $true
|
||||
}
|
||||
} catch {
|
||||
Write-ColorOutput "[ERROR] Query analysis failed: $_" -Color Red
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Get-IndexUsageStats {
|
||||
param([string]$OutputFile)
|
||||
|
||||
Write-ColorOutput "`nGathering index usage statistics..." -Color Cyan
|
||||
|
||||
$query = @"
|
||||
SELECT
|
||||
schemaname,
|
||||
tablename,
|
||||
indexname,
|
||||
idx_scan,
|
||||
idx_tup_read,
|
||||
idx_tup_fetch,
|
||||
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
|
||||
FROM pg_stat_user_indexes
|
||||
WHERE schemaname IN ('library', 'users', 'authentication', 'displaypreferences', 'activitylog')
|
||||
ORDER BY schemaname, tablename, idx_scan DESC;
|
||||
"@
|
||||
|
||||
try {
|
||||
& $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c $query > $OutputFile 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-ColorOutput "[OK] Index stats gathered: $OutputFile" -Color Green
|
||||
return $true
|
||||
} else {
|
||||
Write-ColorOutput "[WARN] Index stats completed with warnings" -Color Yellow
|
||||
return $true
|
||||
}
|
||||
} catch {
|
||||
Write-ColorOutput "[ERROR] Index stats failed: $_" -Color Red
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Get-TableSizeStats {
|
||||
param([string]$OutputFile)
|
||||
|
||||
Write-ColorOutput "`nGathering table size statistics..." -Color Cyan
|
||||
|
||||
$query = @"
|
||||
SELECT
|
||||
schemaname,
|
||||
tablename,
|
||||
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS total_size,
|
||||
pg_size_pretty(pg_relation_size(schemaname||'.'||tablename)) AS table_size,
|
||||
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename) - pg_relation_size(schemaname||'.'||tablename)) AS index_size,
|
||||
n_live_tup AS row_count,
|
||||
n_dead_tup AS dead_rows,
|
||||
ROUND(100.0 * n_dead_tup / NULLIF(n_live_tup, 0), 2) AS dead_pct
|
||||
FROM pg_stat_user_tables
|
||||
WHERE schemaname IN ('library', 'users', 'authentication', 'displaypreferences', 'activitylog')
|
||||
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
|
||||
"@
|
||||
|
||||
try {
|
||||
& $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -c $query > $OutputFile 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-ColorOutput "[OK] Table size stats gathered: $OutputFile" -Color Green
|
||||
return $true
|
||||
} else {
|
||||
Write-ColorOutput "[WARN] Table size stats completed with warnings" -Color Yellow
|
||||
return $true
|
||||
}
|
||||
} catch {
|
||||
Write-ColorOutput "[ERROR] Table size stats failed: $_" -Color Red
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Compare-WithPrevious {
|
||||
param(
|
||||
[string]$CurrentReport,
|
||||
[string]$OutputDirectory
|
||||
)
|
||||
|
||||
Write-ColorOutput "`nComparing with previous week's report..." -Color Cyan
|
||||
|
||||
# Find most recent previous report
|
||||
$previousReports = Get-ChildItem "$OutputDirectory\diagnostics_*.txt" -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.Name -ne (Split-Path $CurrentReport -Leaf) } |
|
||||
Sort-Object LastWriteTime -Descending
|
||||
|
||||
if ($previousReports.Count -eq 0) {
|
||||
Write-ColorOutput "[WARN] No previous reports found for comparison" -Color Yellow
|
||||
return
|
||||
}
|
||||
|
||||
$previousReport = $previousReports[0].FullName
|
||||
Write-ColorOutput "Comparing with: $($previousReports[0].Name)" -Color Cyan
|
||||
|
||||
# TODO: Add detailed comparison logic
|
||||
# For now, just note that both reports exist
|
||||
Write-ColorOutput "[OK] Comparison data available" -Color Green
|
||||
Write-ColorOutput " Current: $CurrentReport" -Color Gray
|
||||
Write-ColorOutput " Previous: $previousReport" -Color Gray
|
||||
}
|
||||
|
||||
function New-SummaryReport {
|
||||
param(
|
||||
[string]$OutputDirectory,
|
||||
[string]$Timestamp
|
||||
)
|
||||
|
||||
Write-ColorOutput "`nGenerating summary report..." -Color Cyan
|
||||
|
||||
$summaryFile = Join-Path $OutputDirectory "summary_$Timestamp.txt"
|
||||
|
||||
$summary = @"
|
||||
================================================================================
|
||||
JELLYFIN DATABASE WEEKLY PERFORMANCE SUMMARY
|
||||
================================================================================
|
||||
Report Date: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
|
||||
Database: $DB_NAME at $DB_HOST
|
||||
Week Number: $(Get-WeekNumber)
|
||||
================================================================================
|
||||
|
||||
REPORTS GENERATED:
|
||||
* diagnostics_$Timestamp.txt : Full diagnostic report
|
||||
* query_analysis_$Timestamp.txt : Slow query analysis
|
||||
* index_usage_$Timestamp.txt : Index usage statistics
|
||||
* table_sizes_$Timestamp.txt : Table size and bloat info
|
||||
* summary_$Timestamp.txt : This summary
|
||||
|
||||
================================================================================
|
||||
QUICK HEALTH CHECK:
|
||||
================================================================================
|
||||
|
||||
To view critical issues check diagnostics file for:
|
||||
* Section 5: TABLES WITH HIGH SEQUENTIAL SCANS
|
||||
* Section 8: UNUSED OR RARELY USED INDEXES
|
||||
* Section 10: SLOWEST QUERIES
|
||||
|
||||
To identify slow queries check query_analysis file for:
|
||||
* Section 2: TOP 20 SLOWEST QUERIES (by max execution time)
|
||||
* Section 5: BASEITEMS TABLE QUERIES ANALYSIS
|
||||
|
||||
================================================================================
|
||||
RECOMMENDED ACTIONS:
|
||||
================================================================================
|
||||
|
||||
* Review slow queries taking longer than 10 seconds
|
||||
* Check if new indexes are being utilized
|
||||
* Look for tables with many sequential scans
|
||||
* Monitor cache hit ratio (should be above 95 percent)
|
||||
* Check for tables with high dead tuples percentage (need VACUUM)
|
||||
|
||||
================================================================================
|
||||
AUTOMATION SETUP:
|
||||
================================================================================
|
||||
|
||||
To schedule this script weekly via Task Scheduler run Setup-Weekly-Monitor.ps1
|
||||
as Administrator. This will create a scheduled task to run every Monday at 6am.
|
||||
|
||||
To disable the scheduled task run Setup-Weekly-Monitor.ps1 with -Uninstall flag.
|
||||
|
||||
================================================================================
|
||||
NEXT REPORT: $(Get-Date).AddDays(7).ToString("yyyy-MM-dd")
|
||||
================================================================================
|
||||
"@
|
||||
|
||||
$summary | Out-File -FilePath $summaryFile -Encoding UTF8
|
||||
Write-ColorOutput "[OK] Summary generated: $summaryFile" -Color Green
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Main Execution
|
||||
# ============================================================================
|
||||
|
||||
Write-ColorOutput "`n========================================" -Color Cyan
|
||||
Write-ColorOutput "Jellyfin Database Weekly Monitor" -Color Cyan
|
||||
Write-ColorOutput "========================================" -Color Cyan
|
||||
Write-ColorOutput "Database: $DB_NAME" -Color Yellow
|
||||
Write-ColorOutput "Host: $DB_HOST" -Color Yellow
|
||||
Write-ColorOutput "Week: $(Get-WeekNumber)" -Color Yellow
|
||||
Write-ColorOutput "Date: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -Color Yellow
|
||||
Write-ColorOutput "========================================`n" -Color Cyan
|
||||
|
||||
# Create output directory
|
||||
$reportDir = Join-Path $ProjectRoot $OutputDirectory
|
||||
if (-not (Test-Path $reportDir)) {
|
||||
New-Item -ItemType Directory -Path $reportDir -Force | Out-Null
|
||||
Write-ColorOutput "Created report directory: $reportDir" -Color Green
|
||||
}
|
||||
|
||||
# Generate timestamp for this run
|
||||
$timestamp = Get-ReportTimestamp
|
||||
|
||||
# Test connection
|
||||
if (-not (Test-DatabaseConnection)) {
|
||||
Write-ColorOutput "`n[ERROR] Cannot connect to database. Exiting." -Color Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Run diagnostics
|
||||
$diagnosticsFile = Join-Path $reportDir "diagnostics_$timestamp.txt"
|
||||
$success = Invoke-DiagnosticReport -OutputFile $diagnosticsFile
|
||||
|
||||
if (-not $success) {
|
||||
Write-ColorOutput "`n[WARN] Some reports failed. Check output directory." -Color Yellow
|
||||
}
|
||||
|
||||
# Run query analysis
|
||||
$queryAnalysisFile = Join-Path $reportDir "query_analysis_$timestamp.txt"
|
||||
Invoke-QueryAnalysis -OutputFile $queryAnalysisFile | Out-Null
|
||||
|
||||
# Gather index usage stats
|
||||
$indexUsageFile = Join-Path $reportDir "index_usage_$timestamp.txt"
|
||||
Get-IndexUsageStats -OutputFile $indexUsageFile | Out-Null
|
||||
|
||||
# Gather table size stats
|
||||
$tableSizesFile = Join-Path $reportDir "table_sizes_$timestamp.txt"
|
||||
Get-TableSizeStats -OutputFile $tableSizesFile | Out-Null
|
||||
|
||||
# Compare with previous week if requested
|
||||
if ($CompareWithPrevious) {
|
||||
Compare-WithPrevious -CurrentReport $diagnosticsFile -OutputDirectory $reportDir
|
||||
}
|
||||
|
||||
# Generate summary
|
||||
New-SummaryReport -OutputDirectory $reportDir -Timestamp $timestamp
|
||||
|
||||
# Cleanup old reports (keep last 12 weeks)
|
||||
Write-ColorOutput "`nCleaning up old reports (keeping last 12 weeks)..." -Color Cyan
|
||||
Get-ChildItem $reportDir -File |
|
||||
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-84) } |
|
||||
Remove-Item -Force
|
||||
Write-ColorOutput "[OK] Cleanup complete" -Color Green
|
||||
|
||||
Write-ColorOutput "`n========================================" -Color Green
|
||||
Write-ColorOutput "[OK] WEEKLY MONITORING COMPLETE" -Color Green
|
||||
Write-ColorOutput "========================================" -Color Green
|
||||
Write-ColorOutput "Reports saved to: $reportDir" -Color Yellow
|
||||
Write-ColorOutput "Summary: summary_$timestamp.txt" -Color Yellow
|
||||
Write-ColorOutput "`nNext steps:" -Color Cyan
|
||||
Write-ColorOutput "1. Review summary_$timestamp.txt" -Color White
|
||||
Write-ColorOutput "2. Check for critical issues in diagnostics report" -Color White
|
||||
Write-ColorOutput "3. Analyze slow queries in query_analysis report" -Color White
|
||||
Write-ColorOutput "========================================`n" -Color Green
|
||||
|
||||
# Email report if requested (placeholder)
|
||||
if ($EmailReport) {
|
||||
Write-ColorOutput "[WARN] Email functionality not yet implemented" -Color Yellow
|
||||
Write-ColorOutput " Configure Send-MailMessage with your SMTP settings" -Color Gray
|
||||
}
|
||||
|
||||
exit 0
|
||||
Reference in New Issue
Block a user