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

db

package
v0.0.0-...-c827789 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package db is the PostgreSQL access layer shared by all services (spec 02 §7, ADR-0004): a pgx connection pool, transactions, optimistic locking, forward-only migrations and pool metrics.

Every service owns its own database and role (REQ-PLT-080); nothing in this package crosses that boundary.

Index

Constants

View Source
const DefaultVersionTable = "goose_db_version"

DefaultVersionTable is goose's own version table, used by Migrate for a service's schema.

Variables

View Source
var ErrConflict = errors.New("db: version conflict")

ErrConflict means an optimistic-locking update matched no row: the row was changed (version bumped) or deleted concurrently. Services map it to apierr.EtagMismatch.

Functions

func Check

func Check(pool *pgxpool.Pool) func(ctx context.Context) error

Check returns a readiness check (telemetry.Health.AddCheck) that pings the pool.

func ExpectRow

func ExpectRow(tag pgconn.CommandTag, err error) error

ExpectRow checks the result of a versioned write such as

UPDATE instances SET ..., version = version + 1 WHERE id = $1 AND version = $2

and returns ErrConflict if no row was affected.

func InTx

func InTx(ctx context.Context, b Beginner, opts pgx.TxOptions, fn func(tx pgx.Tx) error) (err error)

InTx runs fn in a transaction: committed if fn returns nil, rolled back if it returns an error or panics (the panic is re-raised).

func IsUniqueViolation

func IsUniqueViolation(err error, constraint string) bool

IsUniqueViolation reports whether err is a unique-constraint violation, optionally on the named constraint (e.g. a resource name index).

func Migrate

func Migrate(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS) ([]string, error)

Migrate validates and applies all pending migrations from fsys and returns the names applied in this run.

func MigrateTable

func MigrateTable(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS, versionTable string) ([]string, error)

MigrateTable is Migrate with a separate version table. Shared libraries that ship tables into every service database (pkg/operation, pkg/outbox) number their migrations independently of the service and track them in their own table, e.g. "qs_operation_db_version".

func Open

func Open(ctx context.Context, cfg Config) (*pgxpool.Pool, error)

Open creates a pool and verifies connectivity.

func ValidateMigrations

func ValidateMigrations(fsys fs.FS) error

ValidateMigrations checks the migration files in fsys (embed.FS of a service's store/migrations): names are NNNN_name.sql with consecutive numbers from 0001, every file has an Up section and none has a non-empty Down section — migrations only go forward (spec 02 §7, expand/contract).

Types

type Beginner

type Beginner interface {
	BeginTx(ctx context.Context, opts pgx.TxOptions) (pgx.Tx, error)
}

Beginner is satisfied by *pgxpool.Pool and pgx.Tx (nested transactions become savepoints).

type Config

type Config struct {
	// DSN is a PostgreSQL URL or key/value string. It contains a password:
	// never log it.
	DSN string
	// ApplicationName is reported to PostgreSQL (pg_stat_activity).
	ApplicationName string
	// MaxConns and MinConns bound the pool size; 0 keeps the pgx default.
	MaxConns int32
	MinConns int32
	// MaxConnLifetime recycles connections, e.g. after a primary switchover.
	MaxConnLifetime time.Duration
	// ConnectTimeout bounds Open's initial ping; default 10s.
	ConnectTimeout time.Duration
}

Config configures the connection pool.

type PoolCollector

type PoolCollector struct {
	// contains filtered or unexported fields
}

PoolCollector exports pgxpool statistics as qs_db_pool_* metrics (REQ-OBS-001).

func NewPoolCollector

func NewPoolCollector(pool *pgxpool.Pool) *PoolCollector

NewPoolCollector returns a collector for pool; register it with prometheus.Registerer.MustRegister.

func (*PoolCollector) Collect

func (c *PoolCollector) Collect(ch chan<- prometheus.Metric)

Collect implements prometheus.Collector.

func (*PoolCollector) Describe

func (c *PoolCollector) Describe(ch chan<- *prometheus.Desc)

Describe implements prometheus.Collector.

Jump to

Keyboard shortcuts

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