Documentation
¶
Overview ¶
Package postgres provides a PostgreSQL connection pool factory with built-in support for decimal types, query tracing via zerolog, and configurable session-level timeouts.
The pool is created from a pgxpool.Pool and supports automatic health checks, connection limits, and idle timeout management. Shopspring decimal types are automatically registered for JSON marshaling.
Basic usage:
pool, err := postgres.New(&postgres.Config{
URL: "postgres://user:pass@localhost/dbname",
MaxConnection: 25,
LogLevel: "info",
})
if err != nil {
return err
}
// Use pool for queries
var user User
err = pool.QueryRow(ctx, "SELECT ... WHERE id = $1", userID).Scan(&user)
Session-level timeouts (statement_timeout, lock_timeout, idle_in_transaction_session_timeout) can be configured to prevent long-running queries from blocking other connections.
Index ¶
- Variables
- func BulkInsertStructs[T any](ctx context.Context, p *Postgres, tableName string, columns []string, ...) (int64, error)
- func ForceMigrationVersion(dbURI, source string, version int) error
- func IsRetryableError(err error, retryableCodes []string) bool
- func MigrateDown(dbURI, source string) error
- func MigrateSteps(dbURI, source string, steps int) error
- func MigrateUp(dbURI, source string) error
- func RunMigration(dbURI, source string) error
- func WithRetry(ctx context.Context, config RetryConfig, fn RetryableFunc) error
- type Config
- type DB
- type DBPool
- type Executor
- type Health
- type HealthCheckOption
- type HealthCheckOptions
- type Lifecycle
- type MigrationVersion
- type PoolStats
- type Postgres
- func (p *Postgres) BulkInsert(ctx context.Context, tableName string, columns []string, rows [][]any) (int64, error)
- func (p *Postgres) GetPoolStats() (*PoolStats, error)
- func (p *Postgres) HealthCheck(ctx context.Context, opts ...HealthCheckOption) error
- func (p *Postgres) IsHealthy(ctx context.Context) bool
- func (p *Postgres) Name() string
- func (p *Postgres) Start(ctx context.Context) error
- func (p *Postgres) Stop() error
- func (p *Postgres) WithReadOnlyTransaction(ctx context.Context, fn TxFunc) error
- func (p *Postgres) WithRepeatableReadTransaction(ctx context.Context, fn TxFunc) error
- func (p *Postgres) WithRetryTx(ctx context.Context, config RetryConfig, fn TxFunc) error
- func (p *Postgres) WithRetryTxDefault(ctx context.Context, fn TxFunc) error
- func (p *Postgres) WithSerializableTransaction(ctx context.Context, fn TxFunc) error
- func (p *Postgres) WithTransaction(ctx context.Context, fn TxFunc) error
- func (p *Postgres) WithTransactionOptions(ctx context.Context, txOptions pgx.TxOptions, fn TxFunc) error
- type RetryConfig
- type RetryableFunc
- type TxFunc
- type TxRunner
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") )
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 ForceMigrationVersion ¶
func IsRetryableError ¶
func MigrateDown ¶
func MigrateSteps ¶
func RunMigration ¶
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 MigrationVersion ¶
func GetMigrationVersion ¶
func GetMigrationVersion(dbURI, source string) (*MigrationVersion, error)
type Postgres ¶
type Postgres struct {
DBPool
}
func (*Postgres) BulkInsert ¶
func (*Postgres) GetPoolStats ¶
func (*Postgres) HealthCheck ¶
func (p *Postgres) HealthCheck(ctx context.Context, opts ...HealthCheckOption) error
func (*Postgres) WithReadOnlyTransaction ¶
func (*Postgres) WithRepeatableReadTransaction ¶
func (*Postgres) WithRetryTx ¶
func (*Postgres) WithRetryTxDefault ¶
func (*Postgres) WithSerializableTransaction ¶
func (*Postgres) WithTransaction ¶
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 TxRunner ¶ added in v1.3.14
type TxRunner interface {
WithTransaction(ctx context.Context, fn TxFunc) error
WithTransactionOptions(ctx context.Context, txOptions pgx.TxOptions, fn TxFunc) error
WithReadOnlyTransaction(ctx context.Context, fn TxFunc) error
WithSerializableTransaction(ctx context.Context, fn TxFunc) error
WithRepeatableReadTransaction(ctx context.Context, fn TxFunc) error
WithRetryTx(ctx context.Context, config RetryConfig, fn TxFunc) error
WithRetryTxDefault(ctx context.Context, fn TxFunc) error
}
Click to show internal directories.
Click to hide internal directories.