class Google::Apis::SpannerV1::TransactionOptions
as deleting old rows from a very large table.
DML is good fit for large, database-wide, operations that are idempotent, such
and other partitions have not been run at all. Given the above, Partitioned
point, some partitions have been committed (or even committed multiple times),
stopped at that point and an error is returned. It is possible that at this
value that cannot be stored due to schema constraints), then the operation is
operation (for instance, a UNIQUE INDEX violation, division by zero, or a
If any error is encountered during the execution of the partitioned DML
execution of a single DML statement via ExecuteSql or ExecuteStreamingSql. -
against other rows. - Partitioned DML transactions may only contain the
on them successfully. It is also possible that statement was never executed
ExecuteSql call dies, it is possible that some rows had the statement executed
Rollback. If the call returns an error, or if the client issuing the
partitions are committed automatically - there is no support for Commit or
column + 1` as it could be run multiple times against some rows. - The
is potentially dangerous to run a statement such as ‘UPDATE table SET column =
statement should be idempotent to avoid unexpected results. For instance, it
least once to each partition. It is strongly recommended that the DML
execution semantics against a partition. The statement will be applied at
the base table rows. - Partitioned DML does not guarantee exactly-once
in independent transactions. Secondary index rows are updated atomically with
table. Rather, the statement is applied atomically to partitions of the table,
row of the table. - The statement is not applied atomically to all rows of the
expressible as the union of many statements which each access only a single
statement must be fully-partitionable. Specifically, the statement must be
replacement for standard DML used in ReadWrite transactions. - The DML
hold locks for less time. That said, Partitioned DML is not a drop-in
clause of the statement. Additionally, the smaller per-partition transactions
this execution strategy only acquires read locks on rows that match the WHERE
complete, and run independently from one another. To reduce lock contention,
separate, internal transactions. These transactions commit automatically when
partitions the keyspace and runs the DML statement on each partition in
workload, should prefer using ReadWrite transactions. Partitioned DML
DML in a ReadWrite transaction. Smaller scoped statements, such as an OLTP
and often better, scalability properties for large, table-wide operations than
DML statements with a different execution strategy that provides different,
Partitioned DML Transactions Partitioned DML transactions are used to execute
with too-old read timestamps fail with the error `FAILED_PRECONDITION`. ##
queries whose timestamp become too old while executing. Reads and SQL queries
in the past. This restriction also applies to in-progress reads and/or SQL
this, Cloud Spanner cannot perform reads at read timestamps more than one hour
default, version GC reclaims versions after they are one hour old. Because of
background to reclaim storage space. This process is known as “version GC”. By
Spanner continuously garbage collects deleted and overwritten data in the
min_read_timestamp. ### Old Read Timestamps and Garbage Collection Cloud
TransactionOptions.ReadOnly.max_staleness and TransactionOptions.ReadOnly.
it can only be used with single-use read-only transactions. See
timestamp negotiation requires up-front knowledge of which rows will be read,
results, and are more likely to execute at the closest replica. Because the
exact staleness reads. However, they are typically able to return fresher
execution, bounded staleness reads are usually a little slower than comparable
reads are executed at the negotiated timestamp. As a result of the two phase
timestamp among all replicas needed to serve the read. In the second phase,
Boundedly stale reads execute in two phases: the first phase negotiates a
execute at different timestamps and thus return inconsistent results.
repeatable: two stale reads, even if they use the same staleness bound, can
all parts of the read see the transaction. Boundedly stale reads are not
consistent with each other – if any part of the read observes a transaction,
reads at the closest available replica without blocking. All rows yielded are
the newest timestamp within the staleness bound that allows execution of the
timestamp, subject to a user-provided staleness bound. Cloud Spanner chooses
Staleness Bounded staleness modes allow Cloud Spanner to pick the read
read_timestamp and TransactionOptions.ReadOnly.exact_staleness. ### Bounded
stale reads usually return fresher results. See TransactionOptions.ReadOnly.
equivalent boundedly stale concurrency modes. On the other hand, boundedly
to pick a timestamp. As a result, they execute slightly faster than the
relative to the current time. These modes do not require a “negotiation phase”
expressed as an absolute Cloud Spanner commit timestamp or a staleness
timestamps <= the read timestamp have finished. The timestamp can either be
They will block until all conflicting transactions that may be assigned commit
none of the modifications done by transactions with a larger commit timestamp.
all transactions with a commit timestamp <= the read timestamp, and observe
prefix of the global transaction history: they observe modifications done by
specified timestamp. Reads at a timestamp are guaranteed to see a consistent
strong. ### Exact Staleness These timestamp bounds execute reads at a user-
transaction or at an exact read timestamp. See TransactionOptions.ReadOnly.
If consistency across reads is required, the reads should be executed within a
transactions might return inconsistent results if there are concurrent writes.
transaction. Strong reads are not repeatable: two consecutive strong read-only
part of the read observes a transaction, all parts of the read see the
all rows yielded by a single read are consistent with each other – if any
all transactions that have committed before the start of the read. Furthermore,
in detail below. ### Strong Strong reads are guaranteed to see the effects of
execute far from the leader replica. Each type of timestamp bound is discussed
quickly than strong or read-write transaction, because they are able to
is geographically distributed, stale read-only transactions can execute more
Bounded staleness. - Exact staleness. If the Cloud Spanner database to be read
read timestamp. The types of timestamp bound are: - Strong (the default). -
client specifies a timestamp bound, which tells Cloud Spanner how to choose a
fact are not permitted to do so). To execute a snapshot transaction, the
Snapshot read-only transactions do not need to call Commit or Rollback (and in
enough that most applications do not need to worry about this in practice.
garbage collected; however, the default garbage collection policy is generous
transactions never abort. They can fail if the chosen read timestamp is
transactions. Unlike locking read-write transactions, snapshot read-only
Since they do not acquire locks, they do not block concurrent read-write
choosing a Cloud Spanner timestamp, then executing all reads at that timestamp.
support writes. Snapshot transactions do not take locks. Instead, they work by
for doing several consistent reads. However, this type of transaction does not
transactions provides a simpler method than locking read-write transactions
becoming idle. ## Snapshot Read-Only Transactions Snapshot read-only
SQL query in the transaction (e.g., `SELECT 1`) prevents the transaction from
`ABORTED`. If this behavior is undesirable, periodically executing a simple
t hold on to locks indefinitely. In that case, the commit will fail with error
10 seconds. Idle transactions can be aborted by Cloud Spanner so that they don’
reads or SQL queries and has not started a read or SQL query within the last
# Idle Transactions A transaction is considered idle if it has no outstanding
instead, it is better to limit the total amount of wall time spent retrying. ##
it is not a good idea to cap the number of retries a transaction can attempt;
can abort many times in a short period before successfully committing. Thus,
e.g., many transactions attempting to modify the same row(s)), a transaction
slightly better chance of success than the previous. Under some circumstances (
increases with each consecutive abort, meaning that each attempt has a
same session as the original attempt. The original session’s lock priority
successfully committing the retry, the client should execute the retry in the
choose to retry the whole transaction again. To maximize the chances of
Retrying Aborted Transactions When a transaction aborts, the application can
exclusion other than between Cloud Spanner transactions themselves. ###
held for. It is an error to use Cloud Spanner locks for any sort of mutual
Cloud Spanner makes no guarantees about how long the transaction’s locks were
not modified any user data in Cloud Spanner. Unless the transaction commits,
attempt returns ‘ABORTED`, Cloud Spanner guarantees that the transaction has
writes. Cloud Spanner can abort the transaction for any reason. If a commit
still valid at commit time, and it is able to acquire write locks for all
Cloud Spanner can commit the transaction if all read locks it acquired are
the client can send a Rollback request to abort the transaction. ### Semantics
more reads or SQL statements followed by Commit. At any time before Commit,
locks and abort it. Conceptually, a read-write transaction consists of zero or
inactivity at the client may cause Cloud Spanner to release a transaction’s
transaction has not been terminated by Commit or Rollback. Long periods of
locks active as long as the transaction continues to do reads, and the
probability and cause less contention. Cloud Spanner attempts to keep read
amount of time a transaction is active. Faster transactions commit with higher
transaction is externally consistent. Clients should attempt to minimize the
to atomically read-modify-write data anywhere in a database. This type of
database. ## Locking Read-Write Transactions Locking transactions may be used
database. They may, however, read/write data in different tables within that
retry loops are not needed. Transactions may only read/write data in a single
transactions. As a consequence of not taking locks, they also do not abort, so
transactions do not take locks, so they do not conflict with read-write
simpler semantics and are almost always faster. In particular, read-only
For transactions that only read, snapshot read-only transactions provide
commit independently. Partitioned DML transactions do not need to be committed.
over each partition in parallel using separate, internal transactions that
statement. Partitioned DML partitions the key space and runs the DML statement
DML. This type of transaction is used to execute a single Partitioned DML
Snapshot read-only transactions do not need to be committed. 3. Partitioned
only transactions can be configured to read at timestamps in the past.
consistency across several reads, but does not allow writes. Snapshot read-
to retry. 2. Snapshot read-only. This transaction type provides guaranteed
commit. Locking read-write transactions may abort, requiring the application
These transactions rely on pessimistic locking and, if necessary, two-phase
This type of transaction is the only way to write data into Cloud Spanner.
Modes Cloud Spanner supports three transaction modes: 1. Locking read-write.
is not necessary to create a new session for each transaction. # Transaction
completed, the session can immediately be re-used for the next transaction. It
count towards the one transaction limit). After the active transaction is
note that standalone reads and queries use a transaction internally and do
# Transactions Each session can have at most one active transaction at a time (
def initialize(**args)
def initialize(**args) update!(**args) end
def update!(**args)
def update!(**args) @partitioned_dml = args[:partitioned_dml] if args.key?(:partitioned_dml) @read_only = args[:read_only] if args.key?(:read_only) @read_write = args[:read_write] if args.key?(:read_write) end