jackc/pgx · error

commit unexpectedly resulted in rollback

Error message

commit unexpectedly resulted in rollback

What it means

Error "commit unexpectedly resulted in rollback" thrown in jackc/pgx.

Source

Thrown at tx.go:90

	}
	if txOptions.AccessMode != "" {
		buf.WriteByte(' ')
		buf.WriteString(string(txOptions.AccessMode))
	}
	if txOptions.DeferrableMode != "" {
		buf.WriteByte(' ')
		buf.WriteString(string(txOptions.DeferrableMode))
	}

	return buf.String()
}

var ErrTxClosed = errors.New("tx is closed")

// ErrTxCommitRollback occurs when an error has occurred in a transaction and
// Commit() is called. PostgreSQL accepts COMMIT on aborted transactions, but
// it is treated as ROLLBACK.
var ErrTxCommitRollback = errors.New("commit unexpectedly resulted in rollback")

// Begin starts a transaction. Unlike [database/sql], the context only affects the begin command. i.e. there is no
// auto-rollback on context cancellation.
func (c *Conn) Begin(ctx context.Context) (Tx, error) {
	return c.BeginTx(ctx, TxOptions{})
}

// BeginTx starts a transaction with txOptions determining the transaction mode. Unlike [database/sql], the context only
// affects the begin command. i.e. there is no auto-rollback on context cancellation.
func (c *Conn) BeginTx(ctx context.Context, txOptions TxOptions) (Tx, error) {
	_, err := c.Exec(ctx, txOptions.beginSQL())
	if err != nil {
		// begin kills the connection upon receiving fatal, panic or non PGError errors,
		// but does not otherwise as the connection should be reusable.
		var pgErr *pgconn.PgError
		if !errors.As(err, &pgErr) || isConnectionFatal(pgErr) {
			c.die()
		}

View on GitHub (pinned to ec1a0befd2)

When it happens

Trigger: Thrown at tx.go:90 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /data/errors/ebcfb881353471f0.json. Report an issue: GitHub.