Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -61,16 +61,21 @@ LIMIT 10;
|
||||
\echo '3. TABLE SIZES (Top 10)'
|
||||
\echo '-----------------------------------------'
|
||||
|
||||
SELECT
|
||||
SELECT
|
||||
s.schemaname,
|
||||
s.relname as tablename,
|
||||
pg_size_pretty(pg_total_relation_size(s.schemaname||'.'||s.relname)) AS total_size,
|
||||
pg_size_pretty(pg_relation_size(s.schemaname||'.'||s.relname)) AS table_size,
|
||||
pg_size_pretty(pg_total_relation_size(s.schemaname||'.'||s.relname) - pg_relation_size(s.schemaname||'.'||s.relname)) AS index_size,
|
||||
s.n_live_tup as row_count
|
||||
FROM pg_stat_user_tables s
|
||||
s.relname AS tablename,
|
||||
pg_size_pretty(pg_total_relation_size(fqname)) AS total_size,
|
||||
pg_size_pretty(pg_relation_size(fqname)) AS table_size,
|
||||
pg_size_pretty(pg_total_relation_size(fqname) - pg_relation_size(fqname)) AS index_size,
|
||||
s.n_live_tup AS row_count
|
||||
FROM (
|
||||
SELECT
|
||||
s.*,
|
||||
quote_ident(s.schemaname) || '.' || quote_ident(s.relname) AS fqname
|
||||
FROM pg_stat_user_tables s
|
||||
) s
|
||||
WHERE s.schemaname IN ('library', 'users', 'authentication', 'displaypreferences', 'activitylog')
|
||||
ORDER BY pg_total_relation_size(s.schemaname||'.'||s.relname) DESC
|
||||
ORDER BY pg_total_relation_size(fqname) DESC
|
||||
LIMIT 10;
|
||||
|
||||
\echo ''
|
||||
@@ -200,8 +205,8 @@ FROM pg_stat_user_indexes
|
||||
WHERE schemaname IN ('library', 'users', 'authentication', 'displaypreferences', 'activitylog')
|
||||
AND idx_scan < 50
|
||||
AND pg_relation_size(indexrelid) > 1000000
|
||||
ORDER BY pg_relation_size(indexrelid) DESC
|
||||
LIMIT 10;
|
||||
ORDER BY tablename, pg_relation_size(indexrelid) DESC;
|
||||
--LIMIT 10;
|
||||
|
||||
\echo ''
|
||||
|
||||
@@ -341,7 +346,7 @@ BEGIN
|
||||
FROM pg_stat_database WHERE datname = 'jellyfin';
|
||||
|
||||
IF hit_ratio < 95 THEN
|
||||
RAISE WARNING 'Low cache hit ratio: %%. Target: >95%%', hit_ratio;
|
||||
RAISE WARNING 'Low cache hit ratio: %. Target: >95%%', hit_ratio;
|
||||
RAISE NOTICE 'RECOMMENDATION: Increase shared_buffers and effective_cache_size';
|
||||
END IF;
|
||||
|
||||
|
||||
@@ -19,51 +19,51 @@ ORDER BY duration DESC;
|
||||
-- 2. Show index usage statistics for BaseItems table
|
||||
SELECT
|
||||
schemaname,
|
||||
reltablename,
|
||||
indexname,
|
||||
relname,
|
||||
indexrelname,
|
||||
idx_scan AS index_scans,
|
||||
idx_tup_read AS tuples_read,
|
||||
idx_tup_fetch AS tuples_fetched,
|
||||
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size,
|
||||
CASE
|
||||
CASE
|
||||
WHEN idx_scan = 0 THEN 'UNUSED'
|
||||
WHEN idx_scan < 10 THEN 'RARELY USED'
|
||||
WHEN idx_scan < 100 THEN 'OCCASIONALLY USED'
|
||||
ELSE 'FREQUENTLY USED'
|
||||
END AS usage_level
|
||||
FROM pg_stat_user_indexes
|
||||
WHERE tablename = 'BaseItems'
|
||||
WHERE relname = 'BaseItems'
|
||||
AND schemaname = 'library'
|
||||
ORDER BY idx_scan DESC, indexname;
|
||||
ORDER BY idx_scan DESC, indexrelname;
|
||||
|
||||
-- 3. Find missing indexes (sequential scans on BaseItems)
|
||||
SELECT
|
||||
schemaname,
|
||||
reltablename,
|
||||
relname,
|
||||
seq_scan AS sequential_scans,
|
||||
seq_tup_read AS rows_read_sequentially,
|
||||
idx_scan AS index_scans,
|
||||
n_tup_ins AS rows_inserted,
|
||||
n_tup_upd AS rows_updated,
|
||||
n_tup_del AS rows_deleted,
|
||||
CASE
|
||||
CASE
|
||||
WHEN seq_scan > 0 AND idx_scan = 0 THEN 'INDEX NEEDED'
|
||||
WHEN seq_scan > idx_scan THEN 'MORE INDEXES MAY HELP'
|
||||
ELSE 'INDEXES WORKING WELL'
|
||||
END AS recommendation
|
||||
FROM pg_stat_user_tables
|
||||
WHERE tablename = 'BaseItems'
|
||||
WHERE relname = 'BaseItems'
|
||||
AND schemaname = 'library';
|
||||
|
||||
-- 4. Show table and index sizes
|
||||
SELECT
|
||||
schemaname,
|
||||
reltablename,
|
||||
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 indexes_size
|
||||
relname,
|
||||
pg_size_pretty(pg_total_relation_size(schemaname||'.'||relname)) AS total_size,
|
||||
pg_size_pretty(pg_relation_size(schemaname||'.'||relname)) AS table_size,
|
||||
pg_size_pretty(pg_total_relation_size(schemaname||'.'||relname) - pg_relation_size(schemaname||'.'||relname)) AS indexes_size
|
||||
FROM pg_stat_user_tables
|
||||
WHERE tablename = 'BaseItems'
|
||||
WHERE relname = 'BaseItems'
|
||||
AND schemaname = 'library';
|
||||
|
||||
-- 5. Show query statistics from pg_stat_statements (if extension is enabled)
|
||||
|
||||
@@ -138,9 +138,9 @@ SELECT
|
||||
ELSE 0
|
||||
END AS avg_rows_per_call,
|
||||
CASE
|
||||
WHEN max_exec_time > 100000 THEN '🔥 CRITICAL (>100s)'
|
||||
WHEN max_exec_time > 10000 THEN '⚠️ SLOW (>10s)'
|
||||
WHEN max_exec_time > 1000 THEN '⚡ MODERATE (>1s)'
|
||||
WHEN max_exec_time > 100000 THEN 'CRITICAL (>100s)'
|
||||
WHEN max_exec_time > 10000 THEN 'SLOW (>10s)'
|
||||
WHEN max_exec_time > 1000 THEN 'MODERATE (>1s)'
|
||||
ELSE '✅ FAST'
|
||||
END AS performance_rating
|
||||
FROM pg_stat_statements
|
||||
|
||||
Reference in New Issue
Block a user