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 ¶
- Variables
- func NewPool(ctx context.Context, cfg Config) (*pgxpool.Pool, error)
- func Ping(ctx context.Context, pool *pgxpool.Pool) error
- func WithTx(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error) error
- type Config
- type HealthChecker
- type InMemoryUnitOfWork
- type PgUnitOfWork
- type Transaction
- type UnitOfWork
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.
type InMemoryUnitOfWork ¶ added in v0.5.0
type InMemoryUnitOfWork struct{}
InMemoryUnitOfWork implements UnitOfWork as a no-op for testing. The callback receives a nilTransaction where Tx() returns nil. Use with components that handle nil transactions (e.g., InMemoryEventStore).
func NewInMemoryUnitOfWork ¶ added in v0.5.0
func NewInMemoryUnitOfWork() *InMemoryUnitOfWork
NewInMemoryUnitOfWork creates a no-op UnitOfWork for unit testing.
func (*InMemoryUnitOfWork) Execute ¶ added in v0.5.0
func (u *InMemoryUnitOfWork) Execute(ctx context.Context, fn func(ctx context.Context, tx Transaction) error) error
Execute runs fn without transaction management.
type PgUnitOfWork ¶ added in v0.5.0
type PgUnitOfWork struct {
// contains filtered or unexported fields
}
PgUnitOfWork implements UnitOfWork using a PostgreSQL connection pool.
func NewUnitOfWork ¶ added in v0.5.0
func NewUnitOfWork(pool *pgxpool.Pool) *PgUnitOfWork
NewUnitOfWork creates a UnitOfWork backed by the given connection pool.
func (*PgUnitOfWork) Execute ¶ added in v0.5.0
func (u *PgUnitOfWork) Execute(ctx context.Context, fn func(ctx context.Context, tx Transaction) error) error
Execute runs fn within a transaction. Commits on success, rolls back on error or panic.
type Transaction ¶ added in v0.5.0
Transaction represents an active database transaction. For in-memory implementations, Tx() returns nil.
type UnitOfWork ¶ added in v0.5.0
type UnitOfWork interface {
Execute(ctx context.Context, fn func(ctx context.Context, tx Transaction) error) error
}
UnitOfWork manages transaction boundaries.