// Package postgres is anchorage's Postgres adapter: pgxpool lifecycle, // schema migrations, and the concrete implementation of the store // interfaces defined in internal/pkg/store. package postgres import ( "context" "fmt" "time" "github.com/jackc/pgx/v5/pgxpool" ) // PoolConfig is the subset of config.PostgresConfig the pool needs. Kept // as a local struct so callers (tests, migrate subcommand) can construct // one without importing the full config package. type PoolConfig struct { DSN string MaxConns int } // NewPool constructs a pgxpool.Pool and verifies connectivity with a ping. // // The caller is responsible for calling pool.Close() at shutdown. A zero // or negative MaxConns falls back to pgxpool's default. func NewPool(ctx context.Context, cfg PoolConfig) (*pgxpool.Pool, error) { if cfg.DSN == "" { return nil, fmt.Errorf("postgres dsn is required") } poolCfg, err := pgxpool.ParseConfig(cfg.DSN) if err != nil { return nil, fmt.Errorf("parse postgres dsn: %w", err) } if cfg.MaxConns > 0 { poolCfg.MaxConns = int32(cfg.MaxConns) } // Keep idle conns short so Postgres restarts don't wedge old tcp // connections that will never work again. poolCfg.MaxConnIdleTime = 5 * time.Minute poolCfg.MaxConnLifetime = 30 * time.Minute poolCfg.HealthCheckPeriod = 30 * time.Second pool, err := pgxpool.NewWithConfig(ctx, poolCfg) if err != nil { return nil, fmt.Errorf("open postgres pool: %w", err) } pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() if err := pool.Ping(pingCtx); err != nil { pool.Close() return nil, fmt.Errorf("ping postgres: %w", err) } return pool, nil }