Skip to content

gopherex/pgtx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgtx

CI

Transaction management for Go + PostgreSQL, built on pgx/v5 and avito-tech/go-transaction-manager.

A Trm manager owns transaction boundaries; a context-aware DB executor transparently routes queries to the active transaction (or the pool when there is none), so repositories stay unaware of whether they run inside a transaction. Generic Do* helpers wrap a unit of work in a chosen isolation level and return its result.

pgtx/                 NewTxManager / NewTxDB constructors (single pgtx.go)
pkg/tx/               Trm, DB, executor, settings, Do* isolation helpers
contrib/otel/         pgxpool → OpenTelemetry metrics (its own go.mod)

Install

go get github.com/gopherex/pgtx

Usage

pool, _ := pgxpool.New(ctx, dsn)

mgr, err := pgtx.NewTxManager(pool, tx.Serializable())
if err != nil {
    return err
}

// context-aware executor: uses the in-context transaction when present
db := pgtx.NewTxDB(trmpgx.DefaultCtxGetter)

// run a unit of work in a transaction, get a typed result back
user, err := tx.DoSerializableRet(ctx, mgr, func(ctx context.Context) (User, error) {
    return repo.FindAndUpdate(ctx, db, id)
})

Isolation-level helpers: tx.Serializable, tx.RepeatableRead, tx.ReadCommitted, tx.ReadUncommitted, plus the Do* / Do*Ret runners.

OpenTelemetry metrics

The contrib/otel module exposes pgxpool statistics as OpenTelemetry metrics. It is a separate module so the core stays free of the OTel dependency:

go get github.com/gopherex/pgtx/contrib/otel
import pgtxotel "github.com/gopherex/pgtx/contrib/otel"

if err := pgtxotel.RegisterMetrics(pool); err != nil {
    return err
}

License

MIT — see LICENSE.

About

Package for transaction management around pgxv5 library

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors