postgresql

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: 25 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 postgresql provides an analysis.Agent implementation for PostgreSQL. Its parser, metadata, EXPLAIN, rules, cost model, and caches are independent from the existing MySQL pipeline.

Index

Constants

This section is empty.

Variables

View Source
var ErrAgentClosed = errors.New("PostgreSQL AnalysisAgent is closed")

ErrAgentClosed means the PostgreSQL agent no longer admits requests.

View Source
var ErrConnectionNotConfigured = errors.New("PostgreSQL connection is not configured")

ErrConnectionNotConfigured means an online request has no configured DSN.

View Source
var ErrInvalidSchemaName = errors.New("invalid PostgreSQL schema name")

ErrInvalidSchemaName identifies a namespace that PostgreSQL cannot represent.

Functions

func WithRequestBuiltinRuleSet

func WithRequestBuiltinRuleSet(name string) analysis.RequestOption

WithRequestBuiltinRuleSet selects a profile for one request. Explicit request selections override the Agent default while disabled-rule policy still wins.

func WithRequestDefaultSchema

func WithRequestDefaultSchema(schema string) analysis.RequestOption

WithRequestDefaultSchema overrides one static request's namespace assumption. Empty values preserve the last nonempty request value or Agent default. Connected requests reject this option rather than silently change semantics.

Types

type AgentConfig

type AgentConfig struct {
	DiagnosticPolicy   analysis.DiagnosticPolicy
	RulePolicy         pgoptimizer.RulePolicy
	OptimizationConfig OptimizationConfig
}

AgentConfig groups the immutable PostgreSQL diagnostic, rule, and optimization policies.

func DefaultAgentConfig

func DefaultAgentConfig() AgentConfig

DefaultAgentConfig returns a fresh PostgreSQL Agent configuration.

func (AgentConfig) Clone

func (config AgentConfig) Clone() AgentConfig

Clone returns a defensive deep copy.

func (AgentConfig) Resolve

func (config AgentConfig) Resolve() (AgentConfig, error)

Resolve validates every policy group, normalizes forbidden function names, and returns a frozen-ready defensive copy.

type AnalysisAgent

type AnalysisAgent struct {
	// contains filtered or unexported fields
}

AnalysisAgent is the PostgreSQL implementation of analysis.Agent.

func NewAnalysisAgent

func NewAnalysisAgent(ctx context.Context, dsn string, options ...Option) (*AnalysisAgent, error)

NewAnalysisAgent validates and freezes local configuration without connecting. Empty DSNs enable offline analysis. A nonempty DSN is checked syntactically; authentication, server discovery and metadata wait for the first online request.

func (*AnalysisAgent) Analyze

func (agent *AnalysisAgent) Analyze(ctx context.Context, sqlText string, options ...analysis.RequestOption) (*analysis.AnalyzeResult, error)

Analyze performs the PostgreSQL connected parser, identity, metadata, binding, native EXPLAIN, dynamic-rule, and index-recommendation pipeline.

func (*AnalysisAgent) AnalyzeDDL

func (agent *AnalysisAgent) AnalyzeDDL(ctx context.Context, sqlText string, options ...analysis.RequestOption) (*analysis.AnalyzeResult, error)

AnalyzeDDL parses and audits one supported destructive PostgreSQL DDL statement. A connected agent augments the AST finding with read-only catalog evidence; the statement is never executed or sent to EXPLAIN.

func (*AnalysisAgent) AnalyzeDDLBatch

func (agent *AnalysisAgent) AnalyzeDDLBatch(ctx context.Context, sqlText string, options ...analysis.RequestOption) (*analysis.AnalyzeResult, error)

AnalyzeDDLBatch reviews an ordered migration script. Analyze, AnalyzeStatic, and AnalyzeDDL remain one-statement. The script is never executed or EXPLAINed.

func (*AnalysisAgent) AnalyzeRuntimePlan

func (agent *AnalysisAgent) AnalyzeRuntimePlan(ctx context.Context, raw []byte, serverVersion string, options ...analysis.RequestOption) (*analysis.AnalyzeResult, error)

AnalyzeRuntimePlan consumes a caller-provided PostgreSQL JSON EXPLAIN plan. It never connects to PostgreSQL or executes the represented statement. Runtime diagnostics are emitted only when ANALYZE fields are present in the input.

func (*AnalysisAgent) AnalyzeStatic

func (agent *AnalysisAgent) AnalyzeStatic(ctx context.Context, sql string, options ...analysis.RequestOption) (*analysis.AnalyzeResult, error)

AnalyzeStatic parses one statement and runs PostgreSQL AST-only rules. It never accesses connected services, metadata, or EXPLAIN.

func (*AnalysisAgent) Capabilities

func (agent *AnalysisAgent) Capabilities() []analysis.CapabilityStatus

Capabilities returns a copy in stable ID order.

func (*AnalysisAgent) Close

func (agent *AnalysisAgent) Close()

Close rejects new requests, waits for admitted work, then releases resources. Concurrent and repeated callers wait for the same cleanup to finish.

func (*AnalysisAgent) Connect

func (agent *AnalysisAgent) Connect(ctx context.Context) error

Connect explicitly initializes the read-only connection and version profile. It is optional: Analyze, Explain and NativeExplain initialize on demand.

func (*AnalysisAgent) Engine

func (*AnalysisAgent) Engine() analysis.Engine

Engine returns PostgreSQL.

func (*AnalysisAgent) Explain

func (agent *AnalysisAgent) Explain(ctx context.Context, sql string, options ...analysis.RequestOption) (*analysis.ExplainResult, error)

Explain safely plans one non-parameterized statement without ANALYZE and returns only the stable public summary.

func (*AnalysisAgent) InvalidateTableCacheAll

func (agent *AnalysisAgent) InvalidateTableCacheAll(schemaName, tableName string)

InvalidateTableCacheAll removes every cached level for a named relation.

func (*AnalysisAgent) InvalidateTableInfoCache

func (agent *AnalysisAgent) InvalidateTableInfoCache(schemaName, tableName string)

InvalidateTableInfoCache removes every cached level for a named relation.

func (*AnalysisAgent) NativeExplain

func (agent *AnalysisAgent) NativeExplain(ctx context.Context, sqlText string) (*pgexplain.NativePlan, error)

NativeExplain returns the PostgreSQL-native decoded plan for callers that explicitly opt into the engine extension API.

func (*AnalysisAgent) RenderMessage

func (agent *AnalysisAgent) RenderMessage(ref *analysis.MessageRef, lang string) string

RenderMessage re-renders one persisted message reference in the requested language, falling back to the persisted default text when the template is unknown or has no translation for that language.

func (*AnalysisAgent) RenderResult

func (agent *AnalysisAgent) RenderResult(result *analysis.AnalyzeResult, lang string) *analysis.AnalyzeResult

RenderResult re-renders every message-backed field of a previously produced result in the requested language without re-running parsing, metadata collection, or EXPLAIN. It renders from the persisted MessageRef values and therefore works on a result restored from storage. The input result is not mutated; an isolated copy with re-rendered text is returned.

func (*AnalysisAgent) ServerVersion

func (agent *AnalysisAgent) ServerVersion() analysis.ServerVersion

ServerVersion returns the connected server summary, or zero values offline.

func (*AnalysisAgent) VersionProfile

func (agent *AnalysisAgent) VersionProfile() analysis.VersionProfile

VersionProfile returns the selected 16/17 profile, or zero values offline.

type HypoPGMode

type HypoPGMode string

HypoPGMode controls use of an already-installed HypoPG extension.

const (
	// HypoPGDisabled skips what-if evaluation. This is the default.
	HypoPGDisabled HypoPGMode = "DISABLED"
	// HypoPGPreferred keeps advisory findings when evaluation is unavailable and
	// keeps trusted comparisons after a scenario failure. Cleanup failures are fatal.
	HypoPGPreferred HypoPGMode = "PREFERRED"
	// HypoPGRequired returns a provider error on unavailability or scenario failure,
	// alongside the report and any comparisons that passed cleanup.
	HypoPGRequired HypoPGMode = "REQUIRED"
)

type OptimizationConfig

type OptimizationConfig struct {
	HypoPGMode HypoPGMode
}

OptimizationConfig controls optional PostgreSQL optimization providers.

func DefaultOptimizationConfig

func DefaultOptimizationConfig() OptimizationConfig

DefaultOptimizationConfig returns PostgreSQL optimization defaults.

func (OptimizationConfig) Clone

func (config OptimizationConfig) Clone() OptimizationConfig

Clone returns an independent optimization configuration.

func (OptimizationConfig) Resolve

func (config OptimizationConfig) Resolve() (OptimizationConfig, error)

Resolve validates and fills PostgreSQL optimization defaults.

type Option

type Option func(*agentOptions)

Option configures a PostgreSQL AnalysisAgent.

func WithAgentConfig

func WithAgentConfig(config AgentConfig) Option

WithAgentConfig sets all PostgreSQL diagnostic, rule, and optimization policies. The caller may safely mutate config after this call.

func WithBuiltinRuleSet

func WithBuiltinRuleSet(name string) Option

WithBuiltinRuleSet selects an Agent default profile: all, static, dynamic or plan. An unknown name is rejected locally by NewAnalysisAgent.

func WithDefaultSchema

func WithDefaultSchema(schema string) Option

WithDefaultSchema declares the assumed schema of unqualified physical names for AnalyzeStatic. It does not change a connected session's search_path or prove catalog existence. Connected Analyze ignores this static default.

func WithLanguage

func WithLanguage(language string) Option

WithLanguage sets the agent-level response language. A request-level analysis.WithLanguage option takes precedence.

func WithLocaleCatalog

func WithLocaleCatalog(catalog *sqli18n.Catalog) Option

WithLocaleCatalog installs a caller-owned localization catalog.

func WithMetadataCacheConfig

func WithMetadataCacheConfig(config *pgdb.MetadataCacheConfig) Option

WithMetadataCacheConfig freezes retention and optional cleanup policy. Nil restores bounded defaults without a background sweeper.

func WithRuleRegistry

func WithRuleRegistry(registry *optimizer.Registry) Option

WithRuleRegistry replaces the builtin diagnostic registry. Registry structure and Agent policies are frozen at construction; custom Rule implementations must be safe for concurrent requests. Nil restores the builtin registry.

Directories

Path Synopsis
cost
native_hypopg
Package native_hypopg implements the explicitly enabled PostgreSQL-native HypoPG what-if provider.
Package native_hypopg implements the explicitly enabled PostgreSQL-native HypoPG what-if provider.
Package db implements PostgreSQL-only connections, planning-context identity, catalog metadata, statistics, and caches.
Package db implements PostgreSQL-only connections, planning-context identity, catalog metadata, statistics, and caches.
Package explain decodes and safely executes PostgreSQL JSON EXPLAIN.
Package explain decodes and safely executes PostgreSQL JSON EXPLAIN.
Package locale provides PostgreSQL-scoped message catalogs.
Package locale provides PostgreSQL-scoped message catalogs.
Package profile defines PostgreSQL-major version profiles and effective capability calculation.
Package profile defines PostgreSQL-major version profiles and effective capability calculation.

Jump to

Keyboard shortcuts

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