Documentation
¶
Overview ¶
Package pgxtx lets an app whose transactions are pgx transactions (not database/sql ones) write workflow runs inside them: Run hands a database/sql *sql.Tx whose every statement runs on the app's pgx.Tx, so sqlstore.Client.EnqueueTx and outbox.Outbox.Write commit or roll back with the app's own pgx writes.
tx, _ := pool.Begin(ctx) // a pgx transaction
defer tx.Rollback(ctx)
// ... the app's own writes on tx ...
err := pgxtx.Run(ctx, tx, func(stx *sql.Tx) error {
_, err := client.EnqueueTx(ctx, stx, job)
return err
})
// ... then tx.Commit(ctx), and client.Wake()
The *sql.Tx is a view, not a transaction: its Commit and Rollback do nothing, and it is valid only inside fn. Committing or rolling back is the app's, on its pgx transaction, as always. The pgx transaction must be open on the database the sqlstore client's tables live in.
It works through a minimal database/sql driver over the pgx transaction (one connection, no prepared statements), so the store's SQL, its savepoints and its error classification (pgconn.PgError carries the SQLSTATE) run unchanged. Postgres only, as pgx is.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.New("pgxtx: not supported on a pgx transaction view")
ErrUnsupported is returned for a database/sql feature the view does not provide (prepared statements, a nested BeginTx).
Functions ¶
Types ¶
type Querier ¶
type Querier interface {
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
}
Querier is what Run needs from a pgx transaction: pgx.Tx satisfies it, and so do *pgx.Conn and a pgxpool connection, for an app that manages its transaction with BEGIN itself.