postgres

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveSchemas

func ResolveSchemas(schemas []string) []string

ResolveSchemas normalizes and expands schema filter values. Empty input means "all non-system schemas" (no filtering). "all" or "*" means the same. Otherwise returns the provided schemas.

Types

type ColumnInfo

type ColumnInfo struct {
	Schema          string  `json:"schema"`
	Table           string  `json:"table"`
	Name            string  `json:"name"`
	OrdinalPosition int     `json:"ordinalPosition"`
	DataType        string  `json:"dataType"`
	IsNullable      bool    `json:"isNullable"`
	ColumnDefault   *string `json:"columnDefault,omitempty"`
}

ColumnInfo describes a table column.

type Config

type Config struct {
	URL string
}

Config holds PostgreSQL connection settings.

type ConstraintInfo

type ConstraintInfo struct {
	Schema     string   `json:"schema"`
	Table      string   `json:"table"`
	Name       string   `json:"name"`
	Type       string   `json:"type"` // p=primary key, u=unique, f=foreign key, c=check
	Columns    []string `json:"columns"`
	RefTable   *string  `json:"refTable,omitempty"`
	RefColumns []string `json:"refColumns,omitempty"`
}

ConstraintInfo describes a table constraint.

type IndexInfo

type IndexInfo struct {
	Schema     string `json:"schema"`
	Table      string `json:"table"`
	Name       string `json:"name"`
	Definition string `json:"definition"`
	SizeBytes  int64  `json:"sizeBytes"`
	IndexScans int64  `json:"indexScans"`
	TupRead    int64  `json:"tupRead"`
	TupFetch   int64  `json:"tupFetch"`
}

IndexInfo describes an index with definition and usage stats.

type Inspector

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

Inspector reads PostgreSQL catalog metadata and statistics.

func NewInspector

func NewInspector(ctx context.Context, cfg Config) (*Inspector, error)

NewInspector connects to PostgreSQL with retry on transient errors.

func (*Inspector) Close

func (i *Inspector) Close()

Close releases the connection pool.

func (*Inspector) GetColumns

func (i *Inspector) GetColumns(ctx context.Context) ([]ColumnInfo, error)

GetColumns fetches all user table columns.

func (*Inspector) GetConstraints

func (i *Inspector) GetConstraints(ctx context.Context) ([]ConstraintInfo, error)

GetConstraints fetches all user table constraints with column names.

func (*Inspector) GetIndexes

func (i *Inspector) GetIndexes(ctx context.Context) ([]IndexInfo, error)

GetIndexes fetches all user indexes with definitions and usage stats.

func (*Inspector) GetTableStats

func (i *Inspector) GetTableStats(ctx context.Context) ([]TableStats, error)

GetTableStats fetches usage statistics for all user tables.

func (*Inspector) GetTables

func (i *Inspector) GetTables(ctx context.Context) ([]TableInfo, error)

GetTables fetches all user tables with row estimates.

func (*Inspector) Inspect

func (i *Inspector) Inspect(ctx context.Context) (*Snapshot, error)

Inspect gathers the full catalog snapshot for the connected database.

func (*Inspector) ServerVersion

func (i *Inspector) ServerVersion(ctx context.Context) (string, error)

ServerVersion returns the PostgreSQL server version string.

type Snapshot

type Snapshot struct {
	Tables      []TableInfo      `json:"tables"`
	Columns     []ColumnInfo     `json:"columns"`
	Indexes     []IndexInfo      `json:"indexes"`
	Stats       []TableStats     `json:"stats"`
	Constraints []ConstraintInfo `json:"constraints"`
}

Snapshot holds the complete catalog metadata for a database.

func FilterSnapshot

func FilterSnapshot(snap *Snapshot, schemas []string) *Snapshot

FilterSnapshot returns a new snapshot containing only objects in the given schemas. If schemas is nil or empty, the original snapshot is returned unmodified.

type TableInfo

type TableInfo struct {
	Schema        string `json:"schema"`
	Name          string `json:"name"`
	Type          string `json:"type"`          // BASE TABLE, VIEW, etc.
	EstimatedRows int64  `json:"estimatedRows"` // from pg_class.reltuples
	SizeBytes     int64  `json:"sizeBytes"`     // from pg_total_relation_size
}

TableInfo describes a table from information_schema + pg_class.

type TableStats

type TableStats struct {
	Schema           string     `json:"schema"`
	Name             string     `json:"name"`
	SeqScan          int64      `json:"seqScan"`
	SeqTupRead       int64      `json:"seqTupRead"`
	IdxScan          int64      `json:"idxScan"`
	IdxTupFetch      int64      `json:"idxTupFetch"`
	LiveTuples       int64      `json:"liveTuples"`
	DeadTuples       int64      `json:"deadTuples"`
	LastVacuum       *time.Time `json:"lastVacuum,omitempty"`
	LastAutovacuum   *time.Time `json:"lastAutovacuum,omitempty"`
	LastAnalyze      *time.Time `json:"lastAnalyze,omitempty"`
	LastAutoanalyze  *time.Time `json:"lastAutoanalyze,omitempty"`
	VacuumCount      int64      `json:"vacuumCount"`
	AutovacuumCount  int64      `json:"autovacuumCount"`
	AnalyzeCount     int64      `json:"analyzeCount"`
	AutoanalyzeCount int64      `json:"autoanalyzeCount"`
}

TableStats holds usage statistics from pg_stat_user_tables.

Jump to

Keyboard shortcuts

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