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 ¶
- func InTx(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error) (err error)
- func Listen(ctx context.Context, pool *pgxpool.Pool, channel string) (<-chan string, error)
- func Migrate(ctx context.Context, pool *pgxpool.Pool, migrationsFS fs.FS) error
- func MustPool(ctx context.Context, dsn string) *pgxpool.Pool
- func Notify(ctx context.Context, pool *pgxpool.Pool, channel, payload string) error
- func Pool(ctx context.Context, dsn string) (*pgxpool.Pool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InTx ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.