pgdesign v0.26.0 /internal/workload
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

Go go
const NPlusOneThreshold = 100

NPlusOneThreshold is the minimum calls ratio (child/parent) that triggers a W025 warning.

#MinSignificantCalls

Go go
const MinSignificantCalls int64 = 100

MinSignificantCalls is the minimum number of calls for a query to be considered significant.

#IndexInfo

Go go
type IndexInfo struct

IndexInfo contains minimal index information for duplicate detection.

#DuplicateIndex

Go go
type DuplicateIndex struct

DuplicateIndex describes a pair where one index is a leading-column prefix of another.

#TableScanStats

Go go
type TableScanStats struct

TableScanStats holds scan statistics for a single table from pg_stat_user_tables.

#StatementStats

Go go
type StatementStats struct

StatementStats holds a row from pg_stat_statements with parsed table references.

#FindDuplicateIndexes

Go go
func FindDuplicateIndexes(indexes []IndexInfo) []DuplicateIndex

FindDuplicateIndexes detects indexes where one is a leading-column prefix of another. Only strict prefixes count (same columns is not a duplicate).

#DetectNPlusOne

Go go
func DetectNPlusOne(fkGraph *model.FKGraph, stats []StatementStats) []diagnostic.Diagnostic

DetectNPlusOne 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

Go go
func QueryTableScanStats(ctx context.Context, conn *pgx.Conn, schemaNames []string) ([]TableScanStats, error)

QueryTableScanStats queries pg_stat_user_tables for scan statistics.

#DetectSeqScanHeavy

Go go
func DetectSeqScanHeavy(scanStats []TableScanStats) []diagnostic.Diagnostic

DetectSeqScanHeavy flags tables where seq_scan > 10 * idx_scan (sequential-scan-heavy).

#DetectLowSelectivityIndexes

Go go
func DetectLowSelectivityIndexes(schema *model.Schema) []diagnostic.Diagnostic

DetectLowSelectivityIndexes flags boolean columns that have dedicated indexes. Boolean columns have only two values, making btree indexes ineffective.

#DetectExcessiveIndexes

Go go
func DetectExcessiveIndexes(schema *model.Schema) []diagnostic.Diagnostic

DetectExcessiveIndexes flags tables with 10 or more indexes (write overhead).

#QueryStatements

Go go
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

Go go
func StructuralRecommendations(schema *model.Schema) []diagnostic.Diagnostic

StructuralRecommendations analyzes a schema and returns index recommendations based on column types and table properties, without requiring a live database.

Search