|
| 1 | +WITH |
| 2 | +write_adjust AS ( |
| 3 | + -- change the below to 1.0 if pg_stats goes back to |
| 4 | + -- the creation of the database |
| 5 | + SELECT 0.0 AS adjustment |
| 6 | +), |
| 7 | +index_usage AS ( |
| 8 | + SELECT sut.relid, |
| 9 | + current_database() AS database, |
| 10 | + sut.schemaname::text as schema_name, |
| 11 | + sut.relname::text AS table_name, |
| 12 | + sut.seq_scan as table_scans, |
| 13 | + sut.idx_scan as index_scans, |
| 14 | + pg_total_relation_size(relid) as table_bytes, |
| 15 | + round(sut.n_tup_ins + sut.n_tup_del + sut.n_tup_upd + sut.n_tup_hot_upd) / |
| 16 | + (seq_tup_read::NUMERIC + 2), 2) as writes_per_scan |
| 17 | + FROM pg_stat_user_tables sut, |
| 18 | + write_adjust |
| 19 | +), |
| 20 | +index_counts AS ( |
| 21 | + SELECT sut.relid, |
| 22 | + count(*) as index_count |
| 23 | + FROM pg_stat_user_tables sut LEFT OUTER JOIN pg_indexes |
| 24 | + ON sut.schemaname = pg_indexes.schemaname AND |
| 25 | + sut.relname = pg_indexes.tablename |
| 26 | + GROUP BY relid |
| 27 | +), |
| 28 | +too_many_tablescans AS ( |
| 29 | + SELECT 'many table scans'::TEXT as reason, |
| 30 | + database, schema_name, table_name, |
| 31 | + table_scans, pg_size_pretty(table_bytes) as table_size, |
| 32 | + writes_per_scan, index_count, table_bytes |
| 33 | + FROM index_usage JOIN index_counts USING ( relid ) |
| 34 | + WHERE table_scans > 1000 |
| 35 | + AND table_scans > ( index_scans * 2 ) |
| 36 | + AND table_bytes > 32000000 |
| 37 | + AND writes_per_scan < ( 1.0 ) |
| 38 | + ORDER BY table_scans DESC |
| 39 | +), |
| 40 | +scans_no_index AS ( |
| 41 | + SELECT 'scans, few indexes'::TEXT as reason, |
| 42 | + database, schema_name, table_name, |
| 43 | + table_scans, pg_size_pretty(table_bytes) as table_size, |
| 44 | + writes_per_scan, index_count, table_bytes |
| 45 | + FROM index_usage JOIN index_counts USING ( relid ) |
| 46 | + WHERE table_scans > 100 |
| 47 | + AND table_scans > ( index_scans ) |
| 48 | + AND index_count < 2 |
| 49 | + AND table_bytes > 32000000 |
| 50 | + AND writes_per_scan < ( 1.0 ) |
| 51 | + ORDER BY table_scans DESC |
| 52 | +), |
| 53 | +big_tables_with_scans AS ( |
| 54 | + SELECT 'big table scans'::TEXT as reason, |
| 55 | + database, schema_name, table_name, |
| 56 | + table_scans, pg_size_pretty(table_bytes) as table_size, |
| 57 | + writes_per_scan, index_count, table_bytes |
| 58 | + FROM index_usage JOIN index_counts USING ( relid ) |
| 59 | + WHERE table_scans > 100 |
| 60 | + AND table_scans > ( index_scans / 10 ) |
| 61 | + AND table_bytes > 1000000000 |
| 62 | + AND writes_per_scan < ( 1.0 ) |
| 63 | + ORDER BY table_bytes DESC |
| 64 | +), |
| 65 | +scans_no_writes AS ( |
| 66 | + SELECT 'scans, no writes'::TEXT as reason, |
| 67 | + database, schema_name, table_name, |
| 68 | + table_scans, pg_size_pretty(table_bytes) as table_size, |
| 69 | + writes_per_scan, index_count, table_bytes |
| 70 | + FROM index_usage JOIN index_counts USING ( relid ) |
| 71 | + WHERE table_scans > 100 |
| 72 | + AND table_scans > ( index_scans / 4 ) |
| 73 | + AND table_bytes > 32000000 |
| 74 | + AND writes_per_scan < ( 0.1 ) |
| 75 | + ORDER BY writes_per_scan ASC |
| 76 | +) |
| 77 | +SELECT reason, database, schema_name, table_name, table_scans, |
| 78 | + table_size, writes_per_scan, index_count |
| 79 | +FROM too_many_tablescans |
| 80 | +UNION ALL |
| 81 | +SELECT reason, database, schema_name, table_name, table_scans, |
| 82 | + table_size, writes_per_scan, index_count |
| 83 | +FROM scans_no_index |
| 84 | +UNION ALL |
| 85 | +SELECT reason, database, schema_name, table_name, table_scans, |
| 86 | + table_size, writes_per_scan, index_count |
| 87 | +FROM big_tables_with_scans |
| 88 | +UNION ALL |
| 89 | +SELECT reason, database, schema_name, table_name, table_scans, |
| 90 | + table_size, writes_per_scan, index_count |
| 91 | +FROM scans_no_writes; |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments