On this page
Package workload analyzes query patterns and schema shape to recommend indexes, using structural heuristics and live pg_stat_statements telemetry.
#internal/workload
#internal/workload
#NPlusOneThreshold
const NPlusOneThreshold = 100NPlusOneThreshold is the minimum calls ratio (child/parent) that triggers a W025 warning.
#MinSignificantCalls
const MinSignificantCalls int64 = 100MinSignificantCalls is the minimum number of calls for a query to be considered significant.
#IndexInfo
type IndexInfo structIndexInfo contains minimal index information for duplicate detection.
#DuplicateIndex
type DuplicateIndex structDuplicateIndex describes a pair where one index is a leading-column prefix of another.
#TableScanStats
type TableScanStats structTableScanStats holds scan statistics for a single table from pg_stat_user_tables.
#StatementStats
type StatementStats structStatementStats holds a row from pg_stat_statements with parsed table references.
#FindDuplicateIndexes
func FindDuplicateIndexes(indexes []IndexInfo) []DuplicateIndexFindDuplicateIndexes detects indexes where one is a leading-column prefix of another. Only strict prefixes count (same columns is not a duplicate).
#DetectNPlusOne
func DetectNPlusOne(fkGraph *model.FKGraph, stats []StatementStats) []diagnostic.DiagnosticDetectNPlusOne analyzes statement statistics against the FK graph to detect potential N+1 query patterns. For each FK relationship (parent->child), if a child-table query has calls >> parent-table query calls (ratio > 100:1) and both have significant call counts, it signals a likely N+1 pattern.
#QueryTableScanStats
func QueryTableScanStats(ctx context.Context, conn *pgx.Conn, schemaNames []string) ([]TableScanStats, error)QueryTableScanStats queries pg_stat_user_tables for scan statistics.
#DetectSeqScanHeavy
func DetectSeqScanHeavy(scanStats []TableScanStats) []diagnostic.DiagnosticDetectSeqScanHeavy flags tables where seq_scan > 10 * idx_scan (sequential-scan-heavy).
#DetectLowSelectivityIndexes
func DetectLowSelectivityIndexes(schema *model.Schema) []diagnostic.DiagnosticDetectLowSelectivityIndexes flags boolean columns that have dedicated indexes. Boolean columns have only two values, making btree indexes ineffective.
#DetectExcessiveIndexes
func DetectExcessiveIndexes(schema *model.Schema) []diagnostic.DiagnosticDetectExcessiveIndexes flags tables with 10 or more indexes (write overhead).
#QueryStatements
func QueryStatements(ctx context.Context, conn *pgx.Conn, limit int) ([]StatementStats, error)QueryStatements queries pg_stat_statements for the top-N DML statements by total execution time and extracts table references from each normalized query. Only SELECT, INSERT, UPDATE, DELETE statements are returned; utility statements are excluded because their normalization is PG 16+ only.
#StructuralRecommendations
func StructuralRecommendations(schema *model.Schema) []diagnostic.DiagnosticStructuralRecommendations analyzes a schema and returns index recommendations based on column types and table properties, without requiring a live database.