Skip to content

Commit 9f4a035

Browse files
committed
comments addressed: fix in rules for field names and formatted. errors thrown when report directory cannot be made
1 parent e266458 commit 9f4a035

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

internal/inspect/db_stats/db_stats.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
var DBStatsQuery string
1919

2020
type Result struct {
21+
Name string
2122
Database_size string
2223
Total_index_size string
2324
Total_table_size string
@@ -43,9 +44,9 @@ func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...fu
4344
return err
4445
}
4546

46-
table := "|Database Size|Total Index Size|Total Table Size|Total Toast Size|Time Since Stats Reset|Index Hit Rate|Table Hit Rate|WAL Size|\n|-|-|-|-|-|-|-|-|\n"
47+
table := "|Name|Database Size|Total Index Size|Total Table Size|Total Toast Size|Time Since Stats Reset|Index Hit Rate|Table Hit Rate|WAL Size|\n|-|-|-|-|-|-|-|-|\n"
4748
for _, r := range result {
48-
table += fmt.Sprintf("|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|\n", r.Database_size, r.Total_index_size, r.Total_table_size, r.Total_toast_size, r.Time_since_stats_reset, r.Index_hit_rate, r.Table_hit_rate, r.WAL_size)
49+
table += fmt.Sprintf("|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|\n", r.Name, r.Database_size, r.Total_index_size, r.Total_table_size, r.Total_toast_size, r.Time_since_stats_reset, r.Index_hit_rate, r.Table_hit_rate, r.WAL_size)
4950
}
5051
return list.RenderTable(table)
5152
}

internal/inspect/db_stats/db_stats.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SELECT
2+
'postgres' AS name,
23
pg_size_pretty(pg_database_size('postgres')) AS database_size,
34
(SELECT pg_size_pretty(SUM(pg_relation_size(c.oid)))
45
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind = 'i') AS total_index_size,

internal/inspect/report.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func Report(ctx context.Context, out string, config pgconn.Config, fsys afero.Fs
2626
if err := utils.MkdirIfNotExistFS(fsys, out); err != nil {
2727
return err
2828
}
29-
// create a date-based subdirectory
3029
dateDir := filepath.Join(out, date)
3130
if err := utils.MkdirIfNotExistFS(fsys, dateDir); err != nil {
3231
return err
@@ -56,7 +55,10 @@ func Report(ctx context.Context, out string, config pgconn.Config, fsys afero.Fs
5655
}
5756
// print the actual save location
5857
if !filepath.IsAbs(dateDir) {
59-
dateDir, _ = filepath.Abs(dateDir)
58+
dateDir, err = filepath.Abs(dateDir)
59+
if err != nil {
60+
return err
61+
}
6062
}
6163
fmt.Fprintln(os.Stderr, "Reports saved to "+utils.Bold(dateDir))
6264
return nil

internal/inspect/total_table_sizes/total_table_sizes.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SELECT
2-
n.nspname || '.' || c.relname AS name,
2+
FORMAT('%I.%I', n.nspname, c.relname) AS name,
33
pg_size_pretty(pg_total_relation_size(c.oid)) AS size
44
FROM pg_class c
55
LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)

internal/inspect/unused_indexes/unused_indexes.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SELECT
2-
schemaname || '.' || relname AS name,
2+
FORMAT('%I.%I', schemaname, relname) AS name,
33
indexrelname AS index,
44
pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
55
idx_scan as index_scans

internal/inspect/vacuum_stats/vacuum_stats.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ WITH table_opts AS (
2020
table_opts
2121
)
2222
SELECT
23-
vacuum_settings.nspname || '.' || vacuum_settings.relname AS name,
23+
FORMAT('%I.%I', vacuum_settings.nspname, vacuum_settings.relname) AS name,
2424
coalesce(to_char(psut.last_vacuum, 'YYYY-MM-DD HH24:MI'), '') AS last_vacuum,
2525
coalesce(to_char(psut.last_autovacuum, 'YYYY-MM-DD HH24:MI'), '') AS last_autovacuum,
2626
to_char(pg_class.reltuples, '9G999G999G999') AS rowcount,

tools/inspect_rules.toml

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@
22

33
[[rule]]
44
query = "SELECT LISTAGG(stmt, ',') AS match FROM `locks.csv` WHERE age > '00:02:00'"
5-
name = "No old locks"
6-
pass = ""
7-
fail = "There is at least one lock older than 2 minutes"
5+
name = "No old locks"
6+
pass = ""
7+
fail = "There is at least one lock older than 2 minutes"
88

99
[[rule]]
1010
query = "SELECT LISTAGG(stmt, ',') AS match FROM `locks.csv` WHERE granted = 'f'"
11-
name = "No ungranted locks"
12-
pass = ""
13-
fail = "There is at least one ungranted lock"
14-
11+
name = "No ungranted locks"
12+
pass = ""
13+
fail = "There is at least one ungranted lock"
1514

1615
[[rule]]
1716
query = "SELECT LISTAGG(index, ',') AS match FROM `unused_indexes.csv`"
18-
pass = ""
19-
fail = "There is at least one unused index"
20-
name = "No unused indexes"
21-
17+
pass = ""
18+
fail = "There is at least one unused index"
19+
name = "No unused indexes"
2220

2321
[[rule]]
24-
name = "Check cache hit is within acceptable bounds"
22+
name = "Check cache hit is within acceptable bounds"
2523
query = "SELECT LISTAGG(name, ',') AS match FROM `db_stats.csv` WHERE index_hit_rate < 0.94 OR table_hit_rate < 0.94"
26-
pass = ""
27-
fail = "There is at least one table with a cache hit ratio below 94%"
24+
pass = ""
25+
fail = "There is a cache hit ratio (table or index) below 94%"
2826

2927
[[rule]]
30-
query = "SELECT LISTAGG(s.name, ',') AS match FROM `seq_scans.csv` s JOIN `table_record_counts.csv` t ON s.name = t.name WHERE t.estimated_count > 1000 AND s.seq_scans > t.estimated_count * 0.1;"
28+
query = "SELECT LISTAGG(t.name, ',') AS match FROM `table_stats.csv` t WHERE t.seq_scans > t.estimated_row_count * 0.1 AND t.estimated_row_count > 1000;"
3129
name = "No large tables with sequential scans more than 10% of rows"
3230
pass = ""
3331
fail = "At least one table is showing sequential scans more than 10% of total row count"
3432

35-
3633
[[rule]]
3734
query = "SELECT LISTAGG(s.tbl, ',') AS match FROM `vacuum_stats.csv` s WHERE s.expect_autovacuum = 'yes' and s.rowcount > 1000;"
3835
name = "No large tables waiting on autovacuum"

0 commit comments

Comments
 (0)