pgxtx

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

View Source
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

func Run

func Run(ctx context.Context, tx Querier, fn func(*sql.Tx) error) error

Run calls fn with a *sql.Tx whose statements run on tx, and returns fn's error. It neither commits nor rolls back tx.

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.

Jump to

Keyboard shortcuts

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