Help us shape the future of Go! Take the 2026 Go Developer Survey

db

package
v0.0.0-...-0930366 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxConnIdleTime time.Duration = 10 * time.Second
	// MinIngestMaxConns is the smallest pool live persist can run in: 6 connections
	// held at its commit barrier (the coordinator plus 5 siblings), plus the
	// advisory-lock session, which is held until the process exits. Below this, a
	// sibling waits forever for a connection nothing releases.
	MinIngestMaxConns int32 = 7
	// DefaultMaxConns leaves headroom for the transient classification reads.
	DefaultMaxConns        int32         = 12
	DefaultMinConns        int32         = 5
	DefaultMaxConnLifetime time.Duration = 5 * time.Minute
)

Variables

This section is empty.

Functions

func AcquireAdvisoryLock

func AcquireAdvisoryLock(ctx context.Context, q Querier, lockKey int) (bool, error)

AcquireAdvisoryLock attempt to acquire an advisory lock on the provided lockKey, returns true if acquired, or false not.

func Migrate

func Migrate(ctx context.Context, databaseURL string, direction migrate.MigrationDirection, count int) (int, error)

func OpenDBConnectionPool

func OpenDBConnectionPool(ctx context.Context, dataSourceName string, poolConfigs ...PoolConfig) (*pgxpool.Pool, error)

func QueryMany

func QueryMany[T any](ctx context.Context, q Querier, query string, args ...any) ([]T, error)

QueryMany executes a query and scans all result rows into []T. For struct types, it uses pgx named-field scanning (db tags). For scalar types (int, bool, string, etc.), it uses direct column scanning. Returns an empty slice (not nil) if no rows are found.

func QueryManyPtrs

func QueryManyPtrs[T any](ctx context.Context, q Querier, query string, args ...any) ([]*T, error)

QueryManyPtrs executes a query and scans all result rows into []*T. For struct types, it uses pgx named-field scanning (db tags). For scalar types (int, bool, string, etc.), it uses direct column scanning. Returns an empty slice (not nil) if no rows are found.

func QueryOne

func QueryOne[T any](ctx context.Context, q Querier, query string, args ...any) (T, error)

QueryOne executes a query and scans the single result row into T. For struct types, it uses pgx named-field scanning (db tags). For scalar types (int, bool, string, etc.), it uses direct column scanning. Returns pgx.ErrNoRows if no row is found.

func ReleaseAdvisoryLock

func ReleaseAdvisoryLock(ctx context.Context, q Querier, lockKey int) error

ReleaseAdvisoryLock releases an advisory lock on the provided lockKey.

func RunInReadSnapshot

func RunInReadSnapshot(ctx context.Context, pool *pgxpool.Pool, fn func(ctx context.Context) error) error

RunInReadSnapshot runs fn with a context whose reads (through QuerierFrom) share one read-only REPEATABLE READ transaction, so every query in fn sees the same committed state. fn must query from one goroutine at a time: a pgx.Tx is not safe for concurrent use.

func RunInTransaction

func RunInTransaction(ctx context.Context, beginner TxBeginner, fn func(pgx.Tx) error) error

RunInTransaction runs the given atomic function in a pgx transaction. It automatically rolls back on error and commits on success.

func RunProtocolMigrations

func RunProtocolMigrations(ctx context.Context, dbPool *pgxpool.Pool) (int, error)

RunProtocolMigrations executes all embedded protocol SQL files to register protocols in the database. Protocol SQL files are idempotent.

func SQLDBFromPool

func SQLDBFromPool(pool *pgxpool.Pool) *sql.DB

SQLDBFromPool returns a *sql.DB backed by the given pgx pool. This is only needed for libraries that require database/sql (e.g. sql-migrate).

Types

type PoolConfig

type PoolConfig struct {
	MaxConns        int32
	MinConns        int32
	MaxConnLifetime time.Duration
	MaxConnIdleTime time.Duration
	QueryExecMode   pgx.QueryExecMode // 0 = pgx default (CacheStatement)
}

PoolConfig holds configurable pgxpool settings. Zero values fall back to Default* constants.

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig returns a PoolConfig populated with the default values.

type Querier

type Querier interface {
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

Querier is the minimal interface shared by *pgxpool.Pool and pgx.Tx. It allows QueryOne/QueryMany to work with both pool and transaction.

func QuerierFrom

func QuerierFrom(ctx context.Context, fallback Querier) Querier

QuerierFrom returns the transaction RunInReadSnapshot put in ctx, or fallback when ctx carries none.

type TxBeginner

type TxBeginner interface {
	Begin(ctx context.Context) (pgx.Tx, error)
}

TxBeginner starts transactions. *pgxpool.Pool begins one on any pooled connection; *pgxpool.Conn begins one on that specific connection.

Directories

Path Synopsis
Package dbtest provides test database utilities with TimescaleDB support.
Package dbtest provides test database utilities with TimescaleDB support.

Jump to

Keyboard shortcuts

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