Documentation
¶
Overview ¶
Package dbadvisor reads Postgres statistics through the viewer's read-only exec path and turns them into findings with evidence and a suggested fix as text. Nothing it suggests is ever executed.
Index ¶
- Constants
- Variables
- func CleanText(s string) string
- func QuoteIdent(s string) string
- func SafeReason(err error) string
- type CacheRow
- type FKRow
- type Finding
- func BuildBloat(rows []TableStatRow, th Thresholds) []Finding
- func BuildCacheHit(row CacheRow, th Thresholds) []Finding
- func BuildMissingFKIndexes(rows []FKRow, th Thresholds) []Finding
- func BuildSeqScans(rows []TableStatRow, th Thresholds) []Finding
- func BuildSessions(rows []SessionRow, th Thresholds) []Finding
- func BuildStatements(state StatementsState, rows []StatementRow, th Thresholds) []Finding
- func BuildUnusedIndexes(rows []UnusedIndexRow, th Thresholds) []Finding
- func Order(fs []Finding, maxFindings int) ([]Finding, bool)
- type Report
- type SessionRow
- type StatementRow
- type StatementsState
- type TableStatRow
- type Thresholds
- type UnusedIndexRow
Constants ¶
const ( SeverityInfo = "info" SeverityWarn = "warn" SeverityCritical = "critical" )
Severities, ordered from least to most urgent.
const ( KindUnusedIndex = "unused_index" KindMissingFKIdx = "missing_fk_index" KindSeqScan = "high_seq_scan" KindBloat = "dead_tuples" KindCacheHit = "low_cache_hit" KindSlowStatement = "slow_statement" KindStatementsOff = "statements_not_enabled" KindLongSession = "long_session" KindCheckFailed = "check_failed" )
Finding kinds.
Variables ¶
var ErrUnsupported = errors.New("performance advisor supports Postgres only")
ErrUnsupported reports an engine the advisor has no catalog queries for.
Functions ¶
func CleanText ¶
CleanText masks secrets, hides string literals, collapses whitespace, drops control characters and cuts to textMax runes.
func QuoteIdent ¶
QuoteIdent quotes a Postgres identifier for suggestion text.
func SafeReason ¶
SafeReason maps an error to fixed wording so database error text never reaches the report.
Types ¶
type CacheRow ¶
type CacheRow struct {
BlksHit int64 `json:"blks_hit"`
BlksRead int64 `json:"blks_read"`
StatsReset string `json:"stats_reset"`
}
CacheRow is the current database's pg_stat_database counters.
type FKRow ¶
type FKRow struct {
Schema string `json:"schema"`
Table string `json:"tbl"`
Constraint string `json:"con_name"`
Columns []string `json:"columns"`
RefTable string `json:"ref_table"`
LiveTup int64 `json:"n_live_tup"`
}
FKRow is one foreign key whose columns have no leading index.
type Finding ¶
type Finding struct {
ID string `json:"id"`
Kind string `json:"kind"`
Severity string `json:"severity"`
Title string `json:"title"`
Object string `json:"object,omitempty"`
Evidence map[string]any `json:"evidence,omitempty"`
Suggestion string `json:"suggestion,omitempty"`
Docs string `json:"docs,omitempty"`
}
Finding is one observation. Suggestion is text for a human to review and try on a branch first.
func BuildBloat ¶
func BuildBloat(rows []TableStatRow, th Thresholds) []Finding
BuildBloat flags tables whose dead tuples are a large share of their rows.
func BuildCacheHit ¶
func BuildCacheHit(row CacheRow, th Thresholds) []Finding
BuildCacheHit flags a buffer cache hit ratio below the minimum.
func BuildMissingFKIndexes ¶
func BuildMissingFKIndexes(rows []FKRow, th Thresholds) []Finding
BuildMissingFKIndexes flags foreign keys whose referencing columns have no index starting with them.
func BuildSeqScans ¶
func BuildSeqScans(rows []TableStatRow, th Thresholds) []Finding
BuildSeqScans flags large tables read mostly by sequential scans.
func BuildSessions ¶
func BuildSessions(rows []SessionRow, th Thresholds) []Finding
BuildSessions flags transactions open longer than the limit.
func BuildStatements ¶
func BuildStatements(state StatementsState, rows []StatementRow, th Thresholds) []Finding
BuildStatements turns pg_stat_statements rows into findings, or explains how to enable the extension.
func BuildUnusedIndexes ¶
func BuildUnusedIndexes(rows []UnusedIndexRow, th Thresholds) []Finding
BuildUnusedIndexes flags secondary indexes that were never scanned and are at least the configured size.
type Report ¶
type Report struct {
Engine string `json:"engine"`
CheckedAt time.Time `json:"checked_at"`
Findings []Finding `json:"findings"`
// Truncated is true when more findings existed than the configured cap.
Truncated bool `json:"truncated"`
}
Report is the whole advisor answer for one database.
type SessionRow ¶
type SessionRow struct {
PID int64 `json:"pid"`
User string `json:"usename"`
App string `json:"application_name"`
State string `json:"state"`
XactAgeSec int64 `json:"xact_age_s"`
StateAgeSec int64 `json:"state_age_s"`
WaitType string `json:"wait_event_type"`
Wait string `json:"wait_event"`
Query string `json:"query"`
}
SessionRow is one long or idle in transaction backend.
type StatementRow ¶
type StatementRow struct {
QueryID string `json:"queryid"`
Calls int64 `json:"calls"`
MeanMs float64 `json:"mean_exec_time"`
TotalMs float64 `json:"total_exec_time"`
Rows int64 `json:"rows"`
Query string `json:"query"`
}
StatementRow is one pg_stat_statements row.
type StatementsState ¶
StatementsState says whether pg_stat_statements can be read.
type TableStatRow ¶
type TableStatRow struct {
Schema string `json:"schema"`
Table string `json:"tbl"`
SeqScan int64 `json:"seq_scan"`
IdxScan int64 `json:"idx_scan"`
SeqTupRead int64 `json:"seq_tup_read"`
LiveTup int64 `json:"n_live_tup"`
DeadTup int64 `json:"n_dead_tup"`
LastAutovacuum string `json:"last_autovacuum"`
LastVacuum string `json:"last_vacuum"`
}
TableStatRow is one pg_stat_user_tables row.
type Thresholds ¶
type Thresholds struct {
UnusedIndexMinBytes int64
SeqScanMinRows int64
// SeqScanRatioPercent flags tables where sequential scans are at least this share of all scans.
SeqScanRatioPercent int
DeadTuplePercent int
CacheHitMinPercent int
IdleInTxMinutes int
SlowStatementMs int
MaxFindings int
Timeout time.Duration
}
Thresholds are the live settings one run uses.
type UnusedIndexRow ¶
type UnusedIndexRow struct {
Schema string `json:"schema"`
Table string `json:"tbl"`
Index string `json:"index"`
IdxScan int64 `json:"idx_scan"`
Bytes int64 `json:"bytes"`
Def string `json:"def"`
StatsReset string `json:"stats_reset"`
}
UnusedIndexRow is one pg_stat_user_indexes row.