Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

pgx

v0.3.0
smeldr.dev/core/pgx

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

5 matches

Top ↑

Package pgx provides a [smeldr.DB] adapter for pgx/v5 native connection pools. It bridges pgxpool.Pool to the [smeldr.DB] interface so Smeldr modules can use pgx's maximum-throughput connection pool without any changes to core Smeldr code.

Usage

pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
    log.Fatal(err)
}
app := smeldr.New(smeldr.Config{
    BaseURL: "https://example.com",
    Secret:  []byte(os.Getenv("SECRET")),
    DB:      pgx.Wrap(pool),
})

Transactions

The handle Wrap returns also provides BeginTx, so core's multi-statement writes (the conflict policy's winner and loser writes, an audited governance mutation, a relation diff) are atomic on Postgres from adapter v0.3.0. Before it they ran statement by statement.

Cross-process locking

The handle Wrap returns also provides AcquireLock, which core's conflict policy uses to exclude other application processes on the same database, not only other goroutines of this one: a Postgres advisory lock on a dedicated pooled connection. A wrapper that embeds smeldr.DB hides the method; forward it to keep the lock. The lock is refused (logged at Error by core, the transition goes ahead without it) whenever fewer than two pool connections are free at that moment, so it can trip under ordinary load on a small or busy pool: a recurring Error line means raise MaxConns.

See [Decision 22] in DECISIONS.md for performance rationale and driver comparison.

Functions

Wrap

func Wrap(p *pgxpool.Pool) smeldr.DB

Wrap returns a [smeldr.DB] backed by the given pgx connection pool.

The pool must not be nil. Wrap calls stdlib.OpenDBFromPool once to create a *sql.DB wrapper; no network connections are established at this point.

pool, _ := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
db := pgx.Wrap(pool)
app := smeldr.New(smeldr.Config{DB: db, ...})

Dependency graph