Skip to content

Commit

Permalink
document implicit txs
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Feb 6, 2024
1 parent 176a4ed commit 8b8e453
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion storage/postgres/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ func (c *Client) SendBatch(ctx context.Context, batch *storage.QueryBatch) error
// However, it reports errors poorly: If _any_ query is syntactically
// malformed, called with the wrong number of args, or has a type conversion problem,
// pgx will report the _first_ query as failing.
//
// For efficiency and simplicity, the method does not use explicit transactions
// with BEGIN/COMMIT unless required by the tx options. Even so, the batch is processed
// atomically, because:
// 1) We use pgx in its default QueryExecMode.
// 2) This in turn makes pgx use postgresql in pipeline mode.
// 3) Postgresql pipeline mode implies transactions-like behavior:
// https://www.postgresql.org/docs/15/libpq-pipeline-mode.html#LIBPQ-PIPELINE-ERRORS

func (c *Client) sendBatchWithOptionsFast(ctx context.Context, batch *storage.QueryBatch, opts pgx.TxOptions) error {
pgxBatch := batch.AsPgxBatch()
var batchResults pgx.BatchResults
Expand All @@ -108,7 +117,7 @@ func (c *Client) sendBatchWithOptionsFast(ctx context.Context, batch *storage.Qu
var err error

// Begin a transaction.
useExplicitTx := opts != emptyTxOptions
useExplicitTx := opts != emptyTxOptions // see function docstring for more info
if useExplicitTx {
// set up our own tx with the specified options
tx, err = c.pool.BeginTx(ctx, opts)
Expand Down

0 comments on commit 8b8e453

Please sign in to comment.