postgres

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package postgres provides pgxpool creation with configurable connection limits and retry

Configuration

Config holds the connection URL and optional pool limits. URL is required (e.g. postgres://user:pass@host:5432/db?sslmode=disable) MaxConns and MinConns default to 10 and 0 when zero (so when only MinConns is set, MaxConns is still 10); MaxConns must be in 1..10000 when set, MinConns in 0..10000, and MinConns <= MaxConns RetryTimeout overrides the default 30s window for connection retry. Duration fields reject negative values. Do not log Config as-is; String and GoString mask the password in the URL. MaskURL(s) is a standalone helper for safe logging of any connection string. Error messages from New also mask the URL when reporting parse failures

Creating a pool

New creates a pgxpool.Pool with exponential backoff until the database is reachable (or ctx is cancelled). Pool settings (MaxConnLifetime, MaxConnIdleTime, HealthCheckPeriod) use sensible defaults. Use the returned pool for queries and call Close when shutting down. Config.Configure runs after defaults are applied and before pool creation, so applications can set pgx hooks, tracers, TLS config, or custom type registration without go-pgkit adding first-class options for each pgx setting

Transactions

WithinTx and WithinTxOptions run a callback inside a pgx transaction. The transaction commits when the callback returns nil and rolls back when it returns an error. Rollback uses context.WithoutCancel(ctx) so a cancelled request context does not leave a transaction open during cleanup

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MaskURL

func MaskURL(s string) string

MaskURL masks the password in a PostgreSQL connection URL for safe logging. On parse error uses regex fallback so that userinfo (including passwords containing @) is fully masked

func New

func New(ctx context.Context, cfg *Config) (*pgxpool.Pool, error)

New creates a pgxpool with exponential backoff until the database is reachable. ctx can cancel the retry. Returns an error if ctx or cfg is nil, the URL is invalid, or pool limits are out of range

func WithinTx added in v1.4.0

func WithinTx(ctx context.Context, pool *pgxpool.Pool, fn func(pgx.Tx) error) error

WithinTx runs fn inside a PostgreSQL transaction using default pgx options.

func WithinTxOptions added in v1.4.0

func WithinTxOptions(ctx context.Context, pool *pgxpool.Pool, opts pgx.TxOptions, fn func(pgx.Tx) error) (err error)

WithinTxOptions runs fn inside a PostgreSQL transaction. It commits when fn returns nil, rolls back when fn returns an error, and re-panics after rollback when fn panics.

Types

type Config

type Config struct {
	URL               string        // PostgreSQL connection URL (required). Password is masked in String/GoString
	MaxConns          int           // Max connections in pool; 0 uses default (10). When set must be 1..10000. Overrides URL
	MinConns          int           // Min idle connections; 0 uses default (0). Must be 0..10000 and <= MaxConns. Overrides URL
	RetryTimeout      time.Duration // Max time for connection retry; 0 uses default (30s)
	MaxConnLifetime   time.Duration // Max lifetime of a connection; 0 uses default (1h)
	MaxConnIdleTime   time.Duration // Max idle time of a connection; 0 uses default (30m)
	HealthCheckPeriod time.Duration // How often to check connection health; 0 uses default (15s)
	ConnectTimeout    time.Duration // Timeout for establishing a connection; 0 uses default (5s)
	Configure         func(*pgxpool.Config) error
}

Config holds connection settings for New. String and GoString mask the password; do not log raw Config MaxConns and MinConns override any pool parameters in the URL (e.g. pool_max_conns); when 0, defaults are used Duration fields use zero for library defaults (RetryTimeout 30s, MaxConnLifetime 1h, MaxConnIdleTime 30m, HealthCheckPeriod 15s, ConnectTimeout 5s). Negative durations are rejected.

func (Config) GoString

func (c Config) GoString() string

GoString implements fmt.GoStringer with the URL password masked. Use for %#v in logs

func (Config) String

func (c Config) String() string

String returns a string representation of the config with the URL password masked. Use for logging

Jump to

Keyboard shortcuts

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