postgres

package
v1.3.8 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrHealthCheckTimeout   = errors.New("postgres: health check timeout")
	ErrNoActiveConnections  = errors.New("postgres: no active connections in pool")
	ErrPoolStatsUnavailable = errors.New("postgres: pool stats unavailable")
)
View Source
var (
	ErrConnectionPoolNil = errors.New("postgres: connection pool is nil")
	ErrConfigNil         = errors.New("postgres: configuration must not be nil")
)
View Source
var (
	ErrDBPoolCastFailed = errors.New("postgres: unable to cast DBPool to *pgxpool.Pool")
	ErrBeginTxFailed    = errors.New("postgres: failed to begin transaction")
	ErrTxRollbackFailed = errors.New("postgres: transaction rollback failed")
	ErrTxRolledBack     = errors.New("postgres: transaction rolled back")
	ErrTxCommitFailed   = errors.New("postgres: failed to commit transaction")
)

Functions

func BulkInsertStructs

func BulkInsertStructs[T any](
	ctx context.Context,
	p *Postgres,
	tableName string,
	columns []string,
	structs []T,
	valueExtractor func(T) []any,
) (int64, error)

func ForceMigrationVersion

func ForceMigrationVersion(dbURI, source string, version int) error

func IsRetryableError

func IsRetryableError(err error, retryableCodes []string) bool

func MigrateDown

func MigrateDown(dbURI, source string) error

func MigrateSteps

func MigrateSteps(dbURI, source string, steps int) error

func MigrateUp

func MigrateUp(dbURI, source string) error

func RunMigration

func RunMigration(dbURI, source string) error

func WithRetry

func WithRetry(ctx context.Context, config RetryConfig, fn RetryableFunc) error

Types

type Config

type Config struct {
	URL                      string
	MaxConnection            int32
	MinConnection            int32
	MaxConnectionIdleTime    time.Duration
	MaxConnectionLifetime    time.Duration
	HealthCheckPeriod        time.Duration
	ConnectTimeout           time.Duration
	LogLevel                 tracelog.LogLevel
	StatementTimeout         time.Duration
	LockTimeout              time.Duration
	IdleInTransactionTimeout time.Duration
}

type DBPool

type DBPool interface {
	pgxscan.Querier
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	Begin(ctx context.Context) (pgx.Tx, error)
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
	Ping(ctx context.Context) error
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	Close()
}

type HealthCheckOption

type HealthCheckOption func(*HealthCheckOptions)

func WithCustomHealthCheckSQL

func WithCustomHealthCheckSQL(sql string) HealthCheckOption

func WithHealthCheckTimeout

func WithHealthCheckTimeout(timeout time.Duration) HealthCheckOption

func WithMinIdleConns

func WithMinIdleConns(minConns int32) HealthCheckOption

func WithRequireActiveConns

func WithRequireActiveConns() HealthCheckOption

type HealthCheckOptions

type HealthCheckOptions struct {
	Timeout              time.Duration
	RequireActiveConns   bool
	MinIdleConns         int32
	CheckQueryExecution  bool
	CustomHealthCheckSQL string
}

type MigrationVersion

type MigrationVersion struct {
	Version uint
	Dirty   bool
}

func GetMigrationVersion

func GetMigrationVersion(dbURI, source string) (*MigrationVersion, error)

type PoolStats

type PoolStats struct {
	AcquireCount            int64
	AcquireDuration         time.Duration
	AcquiredConns           int32
	CanceledAcquireCount    int64
	ConstructingConns       int32
	EmptyAcquireCount       int64
	IdleConns               int32
	MaxConns                int32
	TotalConns              int32
	NewConnsCount           int64
	MaxLifetimeDestroyCount int64
	MaxIdleDestroyCount     int64
}

type Postgres

type Postgres struct {
	DBPool
}

func New

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

func (*Postgres) BulkInsert

func (p *Postgres) BulkInsert(
	ctx context.Context,
	tableName string,
	columns []string,
	rows [][]any,
) (int64, error)

func (*Postgres) GetPoolStats

func (p *Postgres) GetPoolStats() (*PoolStats, error)

func (*Postgres) HealthCheck

func (p *Postgres) HealthCheck(ctx context.Context, opts ...HealthCheckOption) error

func (*Postgres) IsHealthy

func (p *Postgres) IsHealthy(ctx context.Context) bool

func (*Postgres) Name

func (p *Postgres) Name() string

func (*Postgres) Start

func (p *Postgres) Start(ctx context.Context) error

func (*Postgres) Stop

func (p *Postgres) Stop() error

func (*Postgres) WithReadOnlyTransaction

func (p *Postgres) WithReadOnlyTransaction(ctx context.Context, fn TxFunc) error

func (*Postgres) WithRepeatableReadTransaction

func (p *Postgres) WithRepeatableReadTransaction(ctx context.Context, fn TxFunc) error

func (*Postgres) WithRetryTx

func (p *Postgres) WithRetryTx(ctx context.Context, config RetryConfig, fn TxFunc) error

func (*Postgres) WithRetryTxDefault

func (p *Postgres) WithRetryTxDefault(ctx context.Context, fn TxFunc) error

func (*Postgres) WithSerializableTransaction

func (p *Postgres) WithSerializableTransaction(ctx context.Context, fn TxFunc) error

func (*Postgres) WithTransaction

func (p *Postgres) WithTransaction(ctx context.Context, fn TxFunc) error

func (*Postgres) WithTransactionOptions

func (p *Postgres) WithTransactionOptions(
	ctx context.Context,
	txOptions pgx.TxOptions,
	fn TxFunc,
) error

type RetryConfig

type RetryConfig struct {
	MaxRetries    int
	InitialDelay  time.Duration
	MaxDelay      time.Duration
	Multiplier    float64
	RetryableErrs []string
}

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

type RetryableFunc

type RetryableFunc func(ctx context.Context) error

type TxFunc

type TxFunc func(ctx context.Context, tx pgx.Tx) error

Jump to

Keyboard shortcuts

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