explain

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 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 explain decodes and safely executes PostgreSQL JSON EXPLAIN. Its NativePlan is PostgreSQL-specific and is never used as a common planner IR.

Index

Constants

View Source
const ExplainPrefix = "EXPLAIN (FORMAT JSON, VERBOSE TRUE, SETTINGS TRUE, COSTS TRUE) "

ExplainPrefix is the only EXPLAIN form executed by the library. It omits ANALYZE so SELECT and DML statements are planned but never executed.

Variables

This section is empty.

Functions

func Summarize

func Summarize(plan *NativePlan) *analysis.NativeExplainSummary

Summarize projects a PostgreSQL NativePlan into the public display DTO. It does not feed rules or cost evaluation.

Types

type Condition

type Condition struct {
	Kind       string
	Expression string
}

Condition retains one native plan predicate with its PostgreSQL label.

type NativePlan

type NativePlan struct {
	Version  string
	Settings []PlannerSetting
	Root     *PlanNode
	RawJSON  []byte
}

NativePlan is the complete PostgreSQL decoder output.

func Decode

func Decode(raw []byte, serverVersion string) (*NativePlan, error)

Decode decodes PostgreSQL 16/17 EXPLAIN FORMAT JSON without assuming that every extension or future plan node is known.

func Execute

func Execute(ctx context.Context, queryer explainQueryer, sqlText, serverVersion string) (*NativePlan, error)

Execute validates the SQL safety boundary before issuing native EXPLAIN.

type NativeValue

type NativeValue[T any] struct {
	Value   T
	Present bool
	Valid   bool
}

NativeValue distinguishes an absent attribute from an explicit zero or an invalid value. Consumers must check Valid before using Value. Present is true for malformed values (including JSON null); invalid values retain zero Value. Extraction never coerces numeric strings or changes the decoded plan.

type PlanNode

type PlanNode struct {
	Path               string
	NodeType           string
	ParentRelationship string
	JoinType           string
	Operation          string
	RelationOID        uint32
	Schema             string
	Relation           string
	Alias              string
	IndexName          string
	ScanDirection      string
	StartupCost        float64
	TotalCost          float64
	PlanRows           int64
	PlanWidth          int64
	ParallelAware      bool
	Known              bool
	Conditions         []Condition
	SortKeys           []SortKey
	Children           []*PlanNode
	NativeAttrs        map[string]json.RawMessage
}

PlanNode is one PostgreSQL-native plan node. Unknown nodes remain intact and are marked Known=false instead of being mapped to a familiar operator.

func (*PlanNode) Runtime

func (node *PlanNode) Runtime() RuntimeStats

Runtime extracts independent, presence-aware fields from NativeAttrs without requiring ANALYZE, performing I/O, or mutating the caller's decoded plan. A malformed optional attribute invalidates only that attribute.

type PlannerSetting

type PlannerSetting struct {
	Name  string
	Value string
}

PlannerSetting is one setting reported diagnostically by EXPLAIN SETTINGS.

type RuntimeStats

type RuntimeStats struct {
	ActualRows                NativeValue[float64]
	ActualLoops               NativeValue[uint64]
	ActualTotalTime           NativeValue[float64]
	RowsRemovedByFilter       NativeValue[float64]
	RowsRemovedByIndexRecheck NativeValue[float64]
	LossyHeapBlocks           NativeValue[uint64]
	ExactHeapBlocks           NativeValue[uint64]
	SortMethod                NativeValue[string]
	SortSpaceType             NativeValue[string]
	SortSpaceUsed             NativeValue[uint64]
	HashBatches               NativeValue[uint64]
	HashAggBatches            NativeValue[uint64]
	DiskUsage                 NativeValue[uint64]
	TempReadBlocks            NativeValue[uint64]
	TempWrittenBlocks         NativeValue[uint64]
	PlanRows                  NativeValue[float64]
	Strategy                  NativeValue[string]
	InnerUnique               NativeValue[bool]
	RowsRemovedByJoinFilter   NativeValue[float64]
	HeapFetches               NativeValue[uint64]
	WorkersPlanned            NativeValue[uint64]
	WorkersLaunched           NativeValue[uint64]
	SharedHitBlocks           NativeValue[uint64]
	SharedReadBlocks          NativeValue[uint64]
	LocalHitBlocks            NativeValue[uint64]
	LocalReadBlocks           NativeValue[uint64]
}

RuntimeStats is a typed view of node-level PostgreSQL 16/17 EXPLAIN evidence. ActualRows, row-removal counts, and ActualTotalTime (milliseconds) are averages per execution loop. PostgreSQL may round row averages. Block counts are accumulated counters; batch counts and space usage (kB) are node-reported measurements, not per-loop averages. None should be multiplied by loops. Temp block counts include children and alone cannot establish that this node spilled. Worker-only and Incremental Sort group statistics are not projected into node-level fields. PlanRows, Strategy, and InnerUnique retain planning context for runtime checks.

func (RuntimeStats) Executed

func (stats RuntimeStats) Executed() bool

Executed reports whether valid, positive Actual Loops prove node execution. A missing or malformed Actual Rows value does not negate other runtime facts.

type SortKey

type SortKey struct {
	Expression string
	Presorted  bool
}

SortKey retains a native sort or presorted expression.

Jump to

Keyboard shortcuts

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