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
- func AdoptCluster(ctx context.Context, pool *pgxpool.Pool) (bool, error)
- func Canary(ctx context.Context, pool *pgxpool.Pool, fresh string, now time.Time) (string, error)
- func CheckServerVersion(num int) error
- func MigrationLockID(ctx context.Context, pool *pgxpool.Pool) (int64, error)
- func Migrations() fs.FS
- func NewTransactor(pool *pgxpool.Pool) *pgxtx.Transactor
- func Open(ctx context.Context, cfg *pgxpool.Config) (*pgxpool.Pool, error)
- func PoolConfig(databaseURL string, maxConns int) (cfg *pgxpool.Config, overrode bool, err error)
- func SchemaVersions(ctx context.Context, pool *pgxpool.Pool, migrations fs.FS) (database, binary int64, err error)
- func ServerVersionNum(ctx context.Context, pool *pgxpool.Pool) (int, error)
- func SidePool(pool *pgxpool.Pool, maxConns int32) (*pgxpool.Pool, error)
- type AfterCommit
- type Listeners
- type MigrationReport
- type PoolCollector
Constants ¶
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.
const MigrationLockTimeout = 5 * time.Second
MigrationLockTimeout is how long a statement of a migration waits for a lock before it fails. An ALTER TABLE queues for an ACCESS EXCLUSIVE lock behind every reader of the table, and every query that arrives after it queues behind the ALTER, so without a bound one long reader (a dump, an idle transaction) would stall the replicas still serving until it ends. A migration that times out fails, each migration runs in a transaction of its own, and running it again is safe.
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 AdoptCluster ¶ added in v0.3.0
AdoptCluster makes the events in the database follow the transactions of the PostgreSQL cluster the server now runs on, and reports whether it had to.
A cursor names the transaction of the last row its stream read, and a stream reads rows of later transactions. Within one cluster a cursor is always behind the next transaction. A database restored from a logical dump or moved by logical replication keeps its rows and cursors while the new cluster numbers its transactions from the start, so every new row would lie behind the cursors and nothing would read it. When the database was last used on another cluster (its system identifier differs), the offset added to the transaction IDs of new rows is raised so that they all come after every ID stored, and the identifier is recorded; rows and cursors stay as they were, and the order of every stream is kept. A physical copy keeps the identifier and the counter, and nothing happens.
It must run before the replica accepts events: a row written under the old offset would be lost behind the cursors.
func Canary ¶
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 ¶
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 MigrationLockID ¶ added in v0.7.0
MigrationLockID is the advisory lock that serialises migrations of the schema pool's search_path points at. It is derived from the schema, as the server's other locks are, so instances sharing a database in separate schemas never wait for each other's migrations.
func Migrations ¶
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 ¶
Open creates a pool. It does not wait for PostgreSQL to be reachable: the first query does, and readiness reports it.
func PoolConfig ¶
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 ¶
ServerVersionNum reads the server's server_version_num, such as 170004.
Types ¶
type AfterCommit ¶ added in v0.3.0
type AfterCommit struct{}
AfterCommit runs functions once the unit of work their context carries has committed, and never if it rolls back.
func (AfterCommit) AfterCommit ¶ added in v0.3.0
func (AfterCommit) AfterCommit(ctx context.Context, fn func(committed context.Context))
AfterCommit runs fn as pgxtx.AfterCommit does, handing it the context without the transaction, which has ended by then.
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 ¶
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.
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 ¶
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 of that schema, so each migration is applied exactly once; the lock belongs to the session, so it is released even when a migration fails half way. Statements of a migration wait at most MigrationLockTimeout for a lock. 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, change requests, webhook subscriptions with their outbox and deliveries, the change stream and stale-flag digests.
|
Package governance is the PostgreSQL repository of the governance context: the audit log, change requests, webhook subscriptions with their outbox and deliveries, the change stream and stale-flag digests. |
|
Package identity is the PostgreSQL repository of the identity context: auth providers.
|
Package identity is the PostgreSQL repository of the identity context: auth providers. |
|
Package projectrow records, for the life of a transaction, the projects whose row it locked FOR NO KEY 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 NO KEY UPDATE, the lock every write of a project takes so that the project's writes run one at a time. |
|
Package sinks is the PostgreSQL repository of a project's sinks.
|
Package sinks is the PostgreSQL repository of a project's sinks. |
|
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. |