postgres

package
v2.5.0 Latest Latest
Warning

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

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

Documentation

Overview

Package postgres provides shared PostgreSQL connection helpers.

It focuses on predictable connection lifecycle and configuration defaults that are safe for service startup and shutdown flows.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilClient is returned when a postgres client receiver is nil.
	ErrNilClient = errors.New("postgres client is nil")
	// ErrNilContext is returned when a required context is nil.
	ErrNilContext = errors.New("context is nil")
	// ErrInvalidConfig indicates invalid postgres or migration configuration.
	ErrInvalidConfig = errors.New("invalid postgres config")
	// ErrNotConnected indicates operations requiring an active connection were called before connect.
	ErrNotConnected = errors.New("postgres client is not connected")
	// ErrInvalidDatabaseName indicates an invalid database identifier.
	ErrInvalidDatabaseName = errors.New("invalid database name")
	// ErrMigrationDirty indicates migrations stopped at a dirty version.
	ErrMigrationDirty = errors.New("postgres migration dirty")
	// ErrNilMigrator is returned when a migrator receiver is nil.
	ErrNilMigrator = errors.New("postgres migrator is nil")
)

Functions

This section is empty.

Types

type Client

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

Client is the v2 postgres connection manager.

func New

func New(cfg Config) (*Client, error)

New creates a postgres client with immutable configuration.

func (*Client) Close

func (c *Client) Close() error

Close releases database resources. All three handles (resolver, primary, replica) are always explicitly closed to prevent leaks -- the resolver may not own the underlying sql.DB connections.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect establishes a new primary/replica resolver and swaps it atomically.

func (*Client) IsConnected

func (c *Client) IsConnected() (bool, error)

IsConnected reports whether the resolver is currently initialized.

func (*Client) Primary

func (c *Client) Primary() (*sql.DB, error)

Primary returns the current primary sql.DB, useful for admin operations.

func (*Client) Resolver

func (c *Client) Resolver(ctx context.Context) (dbresolver.DB, error)

Resolver returns the resolver, connecting lazily if needed. Unlike sync.Once, this uses double-checked locking so that a transient failure on the first call does not permanently break the client -- subsequent calls will retry the connection.

type Config

type Config struct {
	PrimaryDSN         string
	ReplicaDSN         string
	Logger             log.Logger
	MetricsFactory     *metrics.MetricsFactory
	MaxOpenConnections int
	MaxIdleConnections int
	ConnMaxLifetime    time.Duration
	ConnMaxIdleTime    time.Duration
}

Config stores immutable connection options for a postgres client.

type MigrationConfig

type MigrationConfig struct {
	PrimaryDSN     string
	DatabaseName   string
	MigrationsPath string
	Component      string
	// AllowMultiStatements enables multi-statement execution in migrations.
	// SECURITY: Only enable when migration files are from trusted, version-controlled sources.
	// Multi-statement mode increases the blast radius of compromised migration files.
	AllowMultiStatements bool
	Logger               log.Logger
}

MigrationConfig stores migration-only settings.

type Migrator

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

Migrator runs schema migrations explicitly.

func NewMigrator

func NewMigrator(cfg MigrationConfig) (*Migrator, error)

NewMigrator creates a migrator with explicit migration config.

func (*Migrator) Up

func (m *Migrator) Up(ctx context.Context) error

Up runs all up migrations.

Note: golang-migrate's m.Up() does not accept a context, so cancellation cannot stop a migration in progress. This method checks context state before starting but cannot interrupt a running migration.

type SanitizedError added in v2.3.0

type SanitizedError struct {
	// Message is the credential-free error description.
	Message string
}

SanitizedError wraps a database error with a credential-free message. Error() returns only the sanitized text.

Unwrap deliberately returns nil so the original error chain is never exposed, preventing database credentials from leaking through error inspection. Note: errors.Is will NOT match sentinels via chain traversal (since Unwrap returns nil). Match on the sanitized Message or use typed assertions instead.

func (*SanitizedError) Error added in v2.3.0

func (e *SanitizedError) Error() string

func (*SanitizedError) Unwrap added in v2.3.0

func (e *SanitizedError) Unwrap() error

Unwrap deliberately returns nil to prevent error chain traversal from leaking the original error which may contain database credentials. errors.Is will not traverse into the original cause.

Jump to

Keyboard shortcuts

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