Documentation
¶
Index ¶
- type Config
- type Connection
- func (c *Connection) Acquire(ctx context.Context) (*pgxpool.Conn, error)
- func (c *Connection) Begin(ctx context.Context) (pgx.Tx, error)
- func (c *Connection) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
- func (c *Connection) Close()
- func (c *Connection) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
- func (c *Connection) HealthCheck(ctx context.Context) error
- func (c *Connection) Ping(ctx context.Context) error
- func (c *Connection) Pool() *pgxpool.Pool
- func (c *Connection) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
- func (c *Connection) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
- func (c *Connection) ReleaseAdvisoryLock(ctx context.Context, lockID int64)
- func (c *Connection) Stats() *pgxpool.Stat
- func (c *Connection) TryAdvisoryLock(ctx context.Context, lockID int64) (bool, error)
- type SecretResolver
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Connection details
Host string
Port int
Database string
User string
// Password can be direct value or secret reference
Password string // Direct password (local dev only)
PasswordSecret string // Secret ARN/ID/name for cloud secret managers
// SSL configuration
SSLMode string // disable, require, verify-ca, verify-full
// Connection pool settings
MaxConnections int
MinConnections int
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
HealthCheckPeriod time.Duration
ConnectTimeout time.Duration
// Migration settings
AutoMigrate bool
MigrationsPath string
// Logging
LogLevel string // error, warn, info, debug
}
Config holds database configuration
func LoadFromEnv ¶
LoadFromEnv loads database configuration from environment variables
func (*Config) DSN ¶
DSN generates a PostgreSQL connection string If passwordOverride is provided, it's used instead of config.Password
func (*Config) RedactedDSN ¶
RedactedDSN returns a DSN string with the password masked, safe for logging
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection wraps a PostgreSQL connection pool
func NewConnection ¶
func NewConnection(ctx context.Context, config *Config, secretResolver SecretResolver) (*Connection, error)
NewConnection creates a new database connection pool If secretResolver is provided and config.PasswordSecret is set, password will be retrieved from secret manager
func OpenFromEnv ¶
func OpenFromEnv(ctx context.Context) (*Connection, error)
OpenFromEnv creates a database connection using environment-variable configuration.
It loads the DB config from the environment, optionally builds a secrets.Resolver when DB_PASSWORD_SECRET is set (resolving the password synchronously before returning), then returns an open connection pool. The resolver is closed as soon as the password has been resolved; callers do not need to manage it.
This is the canonical bootstrap helper for cmd/* entrypoints that only need a DB connection and have no other use for the secrets resolver. If you also need the resolver for a different purpose (e.g. cmd/rekey loads a key via credentials.LoadKey before opening the DB), wire the resolver yourself and call NewConnection directly.
func (*Connection) Exec ¶
func (c *Connection) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
Exec executes a command
func (*Connection) HealthCheck ¶
func (c *Connection) HealthCheck(ctx context.Context) error
HealthCheck verifies the database connection is healthy
func (*Connection) Ping ¶
func (c *Connection) Ping(ctx context.Context) error
Ping checks the database connection
func (*Connection) Pool ¶
func (c *Connection) Pool() *pgxpool.Pool
Pool returns the underlying connection pool
func (*Connection) ReleaseAdvisoryLock ¶
func (c *Connection) ReleaseAdvisoryLock(ctx context.Context, lockID int64)
ReleaseAdvisoryLock releases a previously acquired advisory lock. It reuses the same pinned connection used by TryAdvisoryLock to ensure the unlock targets the correct PostgreSQL session.
func (*Connection) Stats ¶
func (c *Connection) Stats() *pgxpool.Stat
Stats returns connection pool statistics
func (*Connection) TryAdvisoryLock ¶
TryAdvisoryLock attempts to acquire a PostgreSQL session-level advisory lock. Returns true if the lock was acquired, false if another session holds it.
Session-level advisory locks in PostgreSQL are tied to the backend session, not the transaction. With a connection pool, lock and unlock must use the same underlying connection. This method pins a connection for the duration of the lock and stores it internally; callers MUST call ReleaseAdvisoryLock to release both the lock and the pinned connection.