Documentation
¶
Overview ¶
Package storage owns the Postgres connection pool and runs migrations.
In the new arch the catalog "domain repos" that used to live here moved out: each `app/X.Store` constructs its own sqlc-backed queries via `gen.New(pool)` against the pool surfaced by Storage.Pool(). Storage's remaining job is composition-root plumbing — open, ping, hand out the pool, close.
Index ¶
- func ForceVersion(dsn string, version uint) error
- func InTx(ctx context.Context, pool *pgxpool.Pool, ...) error
- func IsConflict(err error) bool
- func MigrateTo(dsn string, version uint) error
- func MigrateUp(dsn string) error
- func MigrationDSN(dsn string, d time.Duration) (string, error)
- func TryAdvisoryLock(ctx context.Context, pool *pgxpool.Pool, key int64, ...) (bool, error)
- func WithAdvisoryLock(ctx context.Context, pool *pgxpool.Pool, key int64, ...) error
- type DB
- type PoolOption
- type Storage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ForceVersion ¶ added in v0.10.0
ForceVersion records version as the schema's current one and clears the dirty flag, running no migration. A migration that failed half-way leaves the schema dirty and every later migrate refuses it; the operator repairs the schema by hand, then records where it now stands.
func InTx ¶ added in v0.10.1
InTx runs fn in one repeatable-read transaction and commits when fn returns nil; an error or a panic rolls back every write fn made. Repeatable read keeps each row fn reads as it was when fn started, and fails a write to a row another transaction changed since rather than overwrite it.
func IsConflict ¶ added in v0.10.1
IsConflict reports whether err is Postgres refusing a transaction because a concurrent one changed what it read (serialization failure) or locked what it needed (deadlock). Nothing was written; the same request may be retried.
func MigrateTo ¶ added in v0.10.0
MigrateTo moves the schema to an exact version, running the down migrations when it is below the current one. Exported for the rollback path: an old binary cannot read rows a newer schema wrote, so downgrading the image means downgrading the schema first.
func MigrateUp ¶ added in v0.10.6
MigrateUp runs the pending up-migrations, as boot does, for a deployment that applies them in a step of its own before the pods roll.
func MigrationDSN ¶ added in v0.10.8
MigrationDSN is dsn with Postgres' lock_timeout set to d, for the connections migrations run on: a migration queued behind a long transaction would otherwise hold every later query on its table behind it. A dsn that sets lock_timeout itself keeps it, and 0 leaves dsn unchanged.
func TryAdvisoryLock ¶ added in v0.11.6
func TryAdvisoryLock(ctx context.Context, pool *pgxpool.Pool, key int64, fn func(context.Context) error) (bool, error)
TryAdvisoryLock runs fn only when the transaction-scoped advisory lock named by key is free, and reports whether it ran. Background work that one pod at a time should do uses it: the others skip the round instead of queueing behind the holder.
func WithAdvisoryLock ¶ added in v0.10.0
func WithAdvisoryLock(ctx context.Context, pool *pgxpool.Pool, key int64, fn func(context.Context) error) error
WithAdvisoryLock runs fn while holding the transaction-scoped Postgres advisory lock named by key, so concurrent pods serialize on it. The lock is released when the transaction ends, including on a panic or a lost connection — nothing can leave it held.
Types ¶
type DB ¶ added in v0.10.1
DB is what a store runs its statements on: the pool, or a transaction InTx opened. Begin on a transaction opens a savepoint, so a store whose own write spans tables works unchanged inside a caller's transaction.
type PoolOption ¶ added in v0.9.3
type PoolOption func(*poolSettings)
PoolOption tunes the connection pool. A zero/negative value is ignored, so callers can pass config knobs directly and unset ones keep the defaults.
func WithMaxConns ¶ added in v0.9.3
func WithMaxConns(n int) PoolOption
WithMaxConns overrides the pool's maximum connections (ignored if n is not in 1..MaxInt32).
func WithMigrateLockTimeout ¶ added in v0.10.8
func WithMigrateLockTimeout(d time.Duration) PoolOption
WithMigrateLockTimeout bounds how long Open's migrations wait for a table lock; see MigrationDSN. The pool itself is not affected.
func WithMigrateOnBoot ¶ added in v0.10.0
func WithMigrateOnBoot(on bool) PoolOption
WithMigrateOnBoot(false) stops Open from running the up-migrations. An operator rolling back runs `relay migrate down` and needs the pods that restart meanwhile to leave the schema where they put it.
func WithMinConns ¶ added in v0.9.3
func WithMinConns(n int) PoolOption
WithMinConns overrides the pool's warm-floor connections (ignored if n is not in 1..MaxInt32). A higher floor keeps connections pre-established, so bursty load never pays cold-connection (dial + DNS) latency on the request path.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage is the data-access handle the composition root passes around.
func Open ¶
Open opens a connection pool, runs pending migrations, and returns a ready-to-use *Storage. The returned Storage must be closed with Close when no longer needed. Pool sizing defaults to MaxConns 10 / MinConns 2; pass WithMaxConns / WithMinConns to override, WithMigrateOnBoot(false) to skip the migrations.
func WrapPool ¶
WrapPool wraps an existing *pgxpool.Pool into a *Storage without opening a new pool or running migrations. Intended for tests that supply their own pool. The returned Storage must NOT be closed — the caller owns the pool's lifetime.