database

package
v0.0.0-...-a99bcb5 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: OSL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host              string
	LogLevel          string
	Database          string
	User              string
	Password          string //nolint:gosec // G101: field holds a user-supplied runtime password, not a hardcoded credential
	PasswordSecret    string
	SSLMode           string
	MigrationsPath    string
	MinConnections    int
	MaxConnLifetime   time.Duration
	MaxConnIdleTime   time.Duration
	HealthCheckPeriod time.Duration
	ConnectTimeout    time.Duration
	MaxConnections    int
	Port              int
	AutoMigrate       bool
}

Config holds database configuration.

func LoadFromEnv

func LoadFromEnv() (*Config, error)

LoadFromEnv loads database configuration from environment variables. A set but malformed numeric, boolean or duration variable is an error, like an out-of-range one: falling back to the default would let a typo such as DB_MAX_CONNECTIONS="100 " run the pool at a quarter of its intended size with nothing in the logs (#120).

func (*Config) DSN

func (c *Config) DSN(passwordOverride string) string

DSN generates a PostgreSQL connection string If passwordOverride is provided, it's used instead of config.Password.

func (*Config) RedactedDSN

func (c *Config) RedactedDSN() string

RedactedDSN returns a DSN string with the password masked, safe for logging.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid.

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) Acquire

func (c *Connection) Acquire(ctx context.Context) (*pgxpool.Conn, error)

Acquire gets a connection from the pool.

func (*Connection) AdvisoryLockHeldFor

func (c *Connection) AdvisoryLockHeldFor(ctx context.Context, lockID int64) (time.Duration, bool, error)

AdvisoryLockHeldFor reports how long another session has held the session-level advisory lock lockID. found is false when no session holds it (for example it was released between a failed TryAdvisoryLock and this call) or when the holder's age is not visible to this role.

The age is exact for locks taken through TryAdvisoryLock (see advisoryLockHolderAgeSQL). It under-reports for a lock stranded on a connection that went back to the pool and keeps running other queries, since each query refreshes state_change; ReleaseAdvisoryLock closes such connections instead of pooling them (issue #105), so that case should not arise. Read-only and safe to call concurrently.

func (*Connection) Begin

func (c *Connection) Begin(ctx context.Context) (pgx.Tx, error)

Begin starts a new transaction.

func (*Connection) BeginTx

func (c *Connection) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

BeginTx starts a new transaction with options.

func (*Connection) Close

func (c *Connection) Close()

Close closes the connection pool.

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) Query

func (c *Connection) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

Query executes a query.

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

QueryRow executes a query that returns at most one row.

func (*Connection) ReleaseAdvisoryLock

func (c *Connection) ReleaseAdvisoryLock(_ 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. The context parameter is deliberately unused (see below); it stays in the signature so the method keeps satisfying the server.TaskLocker interface.

func (*Connection) Stats

func (c *Connection) Stats() *pgxpool.Stat

Stats returns connection pool statistics.

func (*Connection) TryAdvisoryLock

func (c *Connection) TryAdvisoryLock(ctx context.Context, lockID int64) (bool, error)

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.

type SecretResolver

type SecretResolver interface {
	GetSecret(ctx context.Context, secretID string) (string, error)
	Close() error
}

SecretResolver interface for retrieving secrets from cloud providers.

Directories

Path Synopsis
postgres

Jump to

Keyboard shortcuts

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