Documentation
¶
Overview ¶
Package postgres provides database access utilities with tracing integration.
It offers pool initialization with tracing, health checks, connection metrics, and transaction management with auto-rollback.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDSNRequired = errors.New("dsn is required")
ErrDSNRequired is returned when DSN is empty in Config.
Functions ¶
func NewPool ¶
NewPool creates a new PostgreSQL connection pool with tracing. It registers a shutdown handler to close the pool gracefully. Returns *pgxpool.Pool directly for sqlc compatibility.
Types ¶
type Config ¶
type Config struct {
// DSN is the PostgreSQL connection string.
// Required. Example: "postgres://user:pass@localhost:5432/db?sslmode=disable"
DSN string `koanf:"dsn"`
// MaxConns is the maximum number of connections in the pool.
MaxConns int32 `koanf:"max_conns"`
// MinConns is the minimum number of connections to keep open.
MinConns int32 `koanf:"min_conns"`
// MaxConnLifetime is the maximum lifetime of a connection.
MaxConnLifetime time.Duration `koanf:"max_conn_lifetime"`
// MaxConnIdleTime is the maximum time a connection can be idle.
MaxConnIdleTime time.Duration `koanf:"max_conn_idle_time"`
// HealthCheckPeriod is the interval between health checks.
HealthCheckPeriod time.Duration `koanf:"health_check_period"`
}
Config holds configuration for PostgreSQL pool initialization.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible default values. DSN is required and must be set by the caller.
type HealthChecker ¶
type HealthChecker struct {
// contains filtered or unexported fields
}
HealthChecker checks PostgreSQL pool connectivity. Implements grpchealth.HealthChecker interface.
func NewHealthChecker ¶
func NewHealthChecker(pool *pgxpool.Pool) *HealthChecker
NewHealthChecker creates a health checker for the given pool.