postgres

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package postgres is the server's PostgreSQL: the connection pool, the check that the server is a supported version, and the schema migrations embedded in the binary.

Index

Constants

View Source
const DefaultMaxConns = 10

DefaultMaxConns is the pool size when neither the configuration nor pool_max_conns in DATABASE_URL sets one: enough for a replica's requests and workers on a small container, a known number on a large node.

View Source
const MinServerVersionNum = 160000

MinServerVersionNum is the oldest supported PostgreSQL, as server_version_num reports it: 16. Migrations and queries may use anything it offers and nothing newer.

Variables

This section is empty.

Functions

func Canary

func Canary(ctx context.Context, pool *pgxpool.Pool, fresh string, now time.Time) (string, error)

Canary returns the instance's canary, storing fresh as the canary at now when the instance has none yet. Replicas starting together all read the one canary the first of them stored.

func CheckServerVersion

func CheckServerVersion(num int) error

CheckServerVersion refuses a server older than MinServerVersionNum, naming both versions, so an unsupported database is found at start rather than at the first query that needs a newer feature.

func Migrations

func Migrations() fs.FS

Migrations returns the migration set embedded in the binary.

func NewTransactor

func NewTransactor(pool *pgxpool.Pool) *pgxtx.Transactor

NewTransactor returns the transactor of the server's pool. A deadlock or serialization failure comes back as unavailable, worth retrying by the client: the server does not retry it, because a unit of work fixes its lock order instead, and a retry would hide an order that is wrong.

func Open

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

Open creates a pool. It does not wait for PostgreSQL to be reachable: the first query does, and readiness reports it.

func PoolConfig

func PoolConfig(databaseURL string, maxConns int) (cfg *pgxpool.Config, overrode bool, err error)

PoolConfig is the pool configuration of databaseURL with maxConns connections. maxConns 0 keeps pool_max_conns of the URL, or DefaultMaxConns when the URL has none. overrode reports a maxConns that replaced the URL's own pool_max_conns, for the caller to log: an existing URL is never overridden silently.

func SchemaVersions

func SchemaVersions(ctx context.Context, pool *pgxpool.Pool, migrations fs.FS) (database, binary int64, err error)

SchemaVersions returns the version of the schema in the database, the last migration applied (0 when none ever was), and the version of migrations, its latest migration (0 for an empty set).

func ServerVersionNum

func ServerVersionNum(ctx context.Context, pool *pgxpool.Pool) (int, error)

ServerVersionNum reads the server's server_version_num, such as 170004.

func SidePool added in v0.2.0

func SidePool(pool *pgxpool.Pool, maxConns int32) (*pgxpool.Pool, error)

SidePool returns a pool of at most maxConns connections to the database of pool, configured as it is, for work that must never wait for one of pool's connections: work a holder of one of them waits on. It connects lazily, as Open does.

Types

type Listeners

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

Listeners run the replica's notification listeners on the database of a pool, each on a connection of its own, and report each one's state as flagnetic_listener_up{channel}. A listener that is down leaves readiness alone: the replica still serves, and what depends on notifications stops trusting itself instead.

func NewListeners

func NewListeners(ctx context.Context, pool *pgxpool.Pool, log *slog.Logger, reg prometheus.Registerer) *Listeners

NewListeners registers the listener gauge on reg. Listeners run on ctx, the replica's own context, until Stop.

func (*Listeners) Restart

func (l *Listeners) Restart(bound time.Duration) bool

Restart ends every listener, waiting at most bound for each to close, and starts them again on new connections, as a database failover makes them do; it reports whether they all closed in time.

func (*Listeners) Start

func (l *Listeners) Start(channel string, h pglisten.Handler, opts pglisten.Options)

Start listens on channel until Stop, delivering to h, in a goroutine whose panics are recovered and counted. opts may shorten the intervals, as tests do, and carries the test kit's fault hook.

func (*Listeners) Stop

func (l *Listeners) Stop(bound time.Duration) bool

Stop ends every listener and waits at most bound for their connections to close, so none outlives the pool. It reports whether they all did.

type MigrationReport

type MigrationReport struct {
	// Applied is the number of migrations this call applied.
	Applied int
	// Version is the schema's version afterwards: the last migration applied,
	// by this call or an earlier one.
	Version int64
}

MigrationReport says what Migrate did.

func Migrate

func Migrate(ctx context.Context, pool *pgxpool.Pool, migrations fs.FS) (MigrationReport, error)

Migrate applies every pending migration of migrations to the schema that pool's search_path points at. Replicas that start together serialise on a PostgreSQL session advisory lock, so each migration is applied exactly once; the lock belongs to the session, so it is released even when a migration fails half way. An empty migration set is valid and applies nothing.

type PoolCollector

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

PoolCollector exports a pool's statistics, so a saturated pool shows before requests start timing out: acquired close to max, and acquire waits growing.

func NewPoolCollector

func NewPoolCollector(pool *pgxpool.Pool) *PoolCollector

NewPoolCollector returns a collector of pool's statistics.

func (*PoolCollector) Collect

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

Collect sends the pool's statistics now.

func (*PoolCollector) Describe

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

Describe sends the descriptions of the pool metrics.

Directories

Path Synopsis
Package access is the PostgreSQL repository of the access context: members, API tokens and sessions.
Package access is the PostgreSQL repository of the access context: members, API tokens and sessions.
Package delivery is the PostgreSQL repository of the delivery context: for now, the ruleset version of each environment.
Package delivery is the PostgreSQL repository of the delivery context: for now, the ruleset version of each environment.
Package events is the PostgreSQL repository of the events context: the raw events SDKs post and the tables that keep them apart from retries.
Package events is the PostgreSQL repository of the events context: the raw events SDKs post and the tables that keep them apart from retries.
Package experiments is the PostgreSQL repository of the experiments context.
Package experiments is the PostgreSQL repository of the experiments context.
Package flags is the PostgreSQL repository of the flags context: flags, their configuration in each environment, and segments.
Package flags is the PostgreSQL repository of the flags context: flags, their configuration in each environment, and segments.
Package governance is the PostgreSQL repository of the governance context: the audit log.
Package governance is the PostgreSQL repository of the governance context: the audit log.
Package projectrow records, for the life of a transaction, the projects whose row it locked FOR UPDATE, the lock every write of a project takes so that the project's writes run one at a time.
Package projectrow records, for the life of a transaction, the projects whose row it locked FOR UPDATE, the lock every write of a project takes so that the project's writes run one at a time.
Package workspace is the PostgreSQL repository of the workspace context: projects, their environments, unit types and attributes.
Package workspace is the PostgreSQL repository of the workspace context: projects, their environments, unit types and attributes.

Jump to

Keyboard shortcuts

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