Skip to content

Conversation

@olavloite
Copy link
Collaborator

@olavloite olavloite commented Jul 1, 2025

Inline the BeginTransaction option with the first statement in the transaction, instead of executing a separate BeginTransaction RPC. This reduces the number of round-trips to Spanner by one for all transactions that have at least one SQL statement.

Using line-begin improves performance for most transaction shapes, as it requires one less round-trip to Spanner. Some transaction shapes do not benefit from this. These are:

  1. Transactions that only write mutations still need an explicit BeginTransaction RPC to be executed, as mutations are included in the Commit RPC. The Commit RPC can also start a transaction, but such transactions are not guaranteed to be applied only once to Spanner.
  2. Transactions that execute multiple parallel queries at the start of the transaction can see higher end-to-end execution times, as only one query can include the BeginTransaction option. All other queries must wait for the first query to return at least one result, which also includes the transaction identifier, before they can proceed.

The default for the database/sql driver is to use inline-begin. A follow-up pull request will add an option to the driver to set a different default for a connection.

BEGIN_COMMIT_OVERRIDE
perf: inline BeginTransaction with first statement

feat: add BeginTransactionOption to configure how to begin a transaction
END_COMMIT_OVERRIDE

Inline the BeginTransaction option with the first statement in the transaction,
instead of executing a separate BeginTransaction RPC. This reduces the number
of round-trips to Spanner by one for all transactions that have at least one
SQL statement.

Using line-begin improves performance for most transaction shapes, as it requires
one less round-trip to Spanner. Some transaction shapes do not benefit from this.
These are:
1. Transactions that only write mutations still need an explicit BeginTransaction
   RPC to be executed, as mutations are included in the Commit RPC. The Commit
   RPC can also start a transaction, but such transactions are not guaranteed to
   be applied only once to Spanner.
2. Transactions that execute multiple parallel queries at the start of the
   transaction can see higher end-to-end execution times, as only one query
   can include the BeginTransaction option. All other queries must wait for
   the first query to return at least one result, which also includes the
   transaction identifier, before they can proceed.

The default for the database/sql driver is to use inline-begin.
A follow-up pull request will add an option to the driver to set a different
default for a connection.
@olavloite olavloite requested a review from a team as a code owner July 1, 2025 15:34
@olavloite olavloite requested a review from rahul2393 July 1, 2025 18:18
rahul2393
rahul2393 previously approved these changes Jul 1, 2025
Adds a BeginTransactionOption configuration field that can be used to
determine how the database/sql driver should begin transactions. The
default is to inline the BeginTransaction option with the first SQL
statement of the transaction. This reduces the number of round-trips
needed per transaction by one for most transaction shapes.
@olavloite olavloite merged commit 21ed1e3 into main Jul 2, 2025
20 checks passed
@olavloite olavloite deleted the inline-begin-tx branch July 2, 2025 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants