dbadvisor

package
v1.5.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	SeverityInfo     = "info"
	SeverityWarn     = "warn"
	SeverityCritical = "critical"
)

Severities, ordered from least to most urgent.

View Source
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

View Source
var ErrUnsupported = errors.New("performance advisor supports Postgres only")

ErrUnsupported reports an engine the advisor has no catalog queries for.

Functions

func CleanText

func CleanText(s string) string

CleanText masks secrets, hides string literals, collapses whitespace, drops control characters and cuts to textMax runes.

func QuoteIdent

func QuoteIdent(s string) string

QuoteIdent quotes a Postgres identifier for suggestion text.

func SafeReason

func SafeReason(err error) string

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.

func Order

func Order(fs []Finding, maxFindings int) ([]Finding, bool)

Order sorts by urgency and then object, and cuts to max findings.

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.

func Run

func Run(ctx context.Context, t dbviewer.Target, th Thresholds, now time.Time) (Report, error)

Run executes every check and always returns a report: a failing check becomes a check_failed finding.

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

type StatementsState struct {
	Installed bool `json:"installed"`
	Preloaded bool `json:"preloaded"`
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL