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

pg

package
v0.0.0-...-65d2c91 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package pg is the boot spine for everything Postgres-connection-shaped: the pool, migrations, transactions, and LISTEN/NOTIFY.

See docs/architecture/02-boot-spine.md. House rule: NOTIFY payloads are ids, not data — listeners re-query.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InTx

func InTx(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error) (err error)

InTx runs fn inside a transaction, committing on nil and rolling back on error or panic. It removes the manual Begin/WithTx/Rollback boilerplate from handlers.

func Listen

func Listen(ctx context.Context, pool *pgxpool.Pool, channel string) (<-chan string, error)

Listen subscribes to a Postgres channel on a dedicated connection (hijacked from the pool, never a pooled borrow) and returns a receive-only channel of payloads. It reconnects with exponential backoff capped at 30s and drops payloads rather than block a slow consumer. The returned channel is closed when ctx is cancelled.

func Migrate

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

Migrate drives goose through the pool, serialized by a pg_advisory_lock keyed off the app's module path so N containers can boot concurrently without racing the schema. It returns early when the schema is already current and never migrates down automatically.

migrationsFS is an embed.FS (or any fs.FS) rooted such that the SQL files live under a "migrations" directory.

func MustPool

func MustPool(ctx context.Context, dsn string) *pgxpool.Pool

MustPool builds a pgxpool, pings it, and panics on failure. Boot-time wiring has no graceful path: if the database is unreachable the process cannot serve.

func Notify

func Notify(ctx context.Context, pool *pgxpool.Pool, channel, payload string) error

Notify sends a NOTIFY on channel with payload. Per house rule, payloads are ids, not data ({table, id, op} JSON or a bare id) — listeners re-query.

func Pool

func Pool(ctx context.Context, dsn string) (*pgxpool.Pool, error)

Pool builds a pgxpool and pings it, returning the error instead of panicking. It is the canonical pool opener; use it when the caller wants to report a clean boot failure itself. MustPool wraps it for the panic-on-failure path.

Types

This section is empty.

Jump to

Keyboard shortcuts

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