transaction.proto 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. syntax = "proto3";
  15. package google.spanner.v1;
  16. import "google/protobuf/duration.proto";
  17. import "google/protobuf/timestamp.proto";
  18. option csharp_namespace = "Google.Cloud.Spanner.V1";
  19. option go_package = "google.golang.org/genproto/googleapis/spanner/v1;spanner";
  20. option java_multiple_files = true;
  21. option java_outer_classname = "TransactionProto";
  22. option java_package = "com.google.spanner.v1";
  23. option php_namespace = "Google\\Cloud\\Spanner\\V1";
  24. option ruby_package = "Google::Cloud::Spanner::V1";
  25. // Transactions:
  26. //
  27. // Each session can have at most one active transaction at a time (note that
  28. // standalone reads and queries use a transaction internally and do count
  29. // towards the one transaction limit). After the active transaction is
  30. // completed, the session can immediately be re-used for the next transaction.
  31. // It is not necessary to create a new session for each transaction.
  32. //
  33. // Transaction modes:
  34. //
  35. // Cloud Spanner supports three transaction modes:
  36. //
  37. // 1. Locking read-write. This type of transaction is the only way
  38. // to write data into Cloud Spanner. These transactions rely on
  39. // pessimistic locking and, if necessary, two-phase commit.
  40. // Locking read-write transactions may abort, requiring the
  41. // application to retry.
  42. //
  43. // 2. Snapshot read-only. Snapshot read-only transactions provide guaranteed
  44. // consistency across several reads, but do not allow
  45. // writes. Snapshot read-only transactions can be configured to read at
  46. // timestamps in the past, or configured to perform a strong read
  47. // (where Spanner will select a timestamp such that the read is
  48. // guaranteed to see the effects of all transactions that have committed
  49. // before the start of the read). Snapshot read-only transactions do not
  50. // need to be committed.
  51. //
  52. // Queries on change streams must be performed with the snapshot read-only
  53. // transaction mode, specifying a strong read. Please see
  54. // [TransactionOptions.ReadOnly.strong][google.spanner.v1.TransactionOptions.ReadOnly.strong]
  55. // for more details.
  56. //
  57. // 3. Partitioned DML. This type of transaction is used to execute
  58. // a single Partitioned DML statement. Partitioned DML partitions
  59. // the key space and runs the DML statement over each partition
  60. // in parallel using separate, internal transactions that commit
  61. // independently. Partitioned DML transactions do not need to be
  62. // committed.
  63. //
  64. // For transactions that only read, snapshot read-only transactions
  65. // provide simpler semantics and are almost always faster. In
  66. // particular, read-only transactions do not take locks, so they do
  67. // not conflict with read-write transactions. As a consequence of not
  68. // taking locks, they also do not abort, so retry loops are not needed.
  69. //
  70. // Transactions may only read-write data in a single database. They
  71. // may, however, read-write data in different tables within that
  72. // database.
  73. //
  74. // Locking read-write transactions:
  75. //
  76. // Locking transactions may be used to atomically read-modify-write
  77. // data anywhere in a database. This type of transaction is externally
  78. // consistent.
  79. //
  80. // Clients should attempt to minimize the amount of time a transaction
  81. // is active. Faster transactions commit with higher probability
  82. // and cause less contention. Cloud Spanner attempts to keep read locks
  83. // active as long as the transaction continues to do reads, and the
  84. // transaction has not been terminated by
  85. // [Commit][google.spanner.v1.Spanner.Commit] or
  86. // [Rollback][google.spanner.v1.Spanner.Rollback]. Long periods of
  87. // inactivity at the client may cause Cloud Spanner to release a
  88. // transaction's locks and abort it.
  89. //
  90. // Conceptually, a read-write transaction consists of zero or more
  91. // reads or SQL statements followed by
  92. // [Commit][google.spanner.v1.Spanner.Commit]. At any time before
  93. // [Commit][google.spanner.v1.Spanner.Commit], the client can send a
  94. // [Rollback][google.spanner.v1.Spanner.Rollback] request to abort the
  95. // transaction.
  96. //
  97. // Semantics:
  98. //
  99. // Cloud Spanner can commit the transaction if all read locks it acquired
  100. // are still valid at commit time, and it is able to acquire write
  101. // locks for all writes. Cloud Spanner can abort the transaction for any
  102. // reason. If a commit attempt returns `ABORTED`, Cloud Spanner guarantees
  103. // that the transaction has not modified any user data in Cloud Spanner.
  104. //
  105. // Unless the transaction commits, Cloud Spanner makes no guarantees about
  106. // how long the transaction's locks were held for. It is an error to
  107. // use Cloud Spanner locks for any sort of mutual exclusion other than
  108. // between Cloud Spanner transactions themselves.
  109. //
  110. // Retrying aborted transactions:
  111. //
  112. // When a transaction aborts, the application can choose to retry the
  113. // whole transaction again. To maximize the chances of successfully
  114. // committing the retry, the client should execute the retry in the
  115. // same session as the original attempt. The original session's lock
  116. // priority increases with each consecutive abort, meaning that each
  117. // attempt has a slightly better chance of success than the previous.
  118. //
  119. // Under some circumstances (for example, many transactions attempting to
  120. // modify the same row(s)), a transaction can abort many times in a
  121. // short period before successfully committing. Thus, it is not a good
  122. // idea to cap the number of retries a transaction can attempt;
  123. // instead, it is better to limit the total amount of time spent
  124. // retrying.
  125. //
  126. // Idle transactions:
  127. //
  128. // A transaction is considered idle if it has no outstanding reads or
  129. // SQL queries and has not started a read or SQL query within the last 10
  130. // seconds. Idle transactions can be aborted by Cloud Spanner so that they
  131. // don't hold on to locks indefinitely. If an idle transaction is aborted, the
  132. // commit will fail with error `ABORTED`.
  133. //
  134. // If this behavior is undesirable, periodically executing a simple
  135. // SQL query in the transaction (for example, `SELECT 1`) prevents the
  136. // transaction from becoming idle.
  137. //
  138. // Snapshot read-only transactions:
  139. //
  140. // Snapshot read-only transactions provides a simpler method than
  141. // locking read-write transactions for doing several consistent
  142. // reads. However, this type of transaction does not support writes.
  143. //
  144. // Snapshot transactions do not take locks. Instead, they work by
  145. // choosing a Cloud Spanner timestamp, then executing all reads at that
  146. // timestamp. Since they do not acquire locks, they do not block
  147. // concurrent read-write transactions.
  148. //
  149. // Unlike locking read-write transactions, snapshot read-only
  150. // transactions never abort. They can fail if the chosen read
  151. // timestamp is garbage collected; however, the default garbage
  152. // collection policy is generous enough that most applications do not
  153. // need to worry about this in practice.
  154. //
  155. // Snapshot read-only transactions do not need to call
  156. // [Commit][google.spanner.v1.Spanner.Commit] or
  157. // [Rollback][google.spanner.v1.Spanner.Rollback] (and in fact are not
  158. // permitted to do so).
  159. //
  160. // To execute a snapshot transaction, the client specifies a timestamp
  161. // bound, which tells Cloud Spanner how to choose a read timestamp.
  162. //
  163. // The types of timestamp bound are:
  164. //
  165. // - Strong (the default).
  166. // - Bounded staleness.
  167. // - Exact staleness.
  168. //
  169. // If the Cloud Spanner database to be read is geographically distributed,
  170. // stale read-only transactions can execute more quickly than strong
  171. // or read-write transactions, because they are able to execute far
  172. // from the leader replica.
  173. //
  174. // Each type of timestamp bound is discussed in detail below.
  175. //
  176. // Strong: Strong reads are guaranteed to see the effects of all transactions
  177. // that have committed before the start of the read. Furthermore, all
  178. // rows yielded by a single read are consistent with each other -- if
  179. // any part of the read observes a transaction, all parts of the read
  180. // see the transaction.
  181. //
  182. // Strong reads are not repeatable: two consecutive strong read-only
  183. // transactions might return inconsistent results if there are
  184. // concurrent writes. If consistency across reads is required, the
  185. // reads should be executed within a transaction or at an exact read
  186. // timestamp.
  187. //
  188. // Queries on change streams (see below for more details) must also specify
  189. // the strong read timestamp bound.
  190. //
  191. // See
  192. // [TransactionOptions.ReadOnly.strong][google.spanner.v1.TransactionOptions.ReadOnly.strong].
  193. //
  194. // Exact staleness:
  195. //
  196. // These timestamp bounds execute reads at a user-specified
  197. // timestamp. Reads at a timestamp are guaranteed to see a consistent
  198. // prefix of the global transaction history: they observe
  199. // modifications done by all transactions with a commit timestamp less than or
  200. // equal to the read timestamp, and observe none of the modifications done by
  201. // transactions with a larger commit timestamp. They will block until
  202. // all conflicting transactions that may be assigned commit timestamps
  203. // <= the read timestamp have finished.
  204. //
  205. // The timestamp can either be expressed as an absolute Cloud Spanner commit
  206. // timestamp or a staleness relative to the current time.
  207. //
  208. // These modes do not require a "negotiation phase" to pick a
  209. // timestamp. As a result, they execute slightly faster than the
  210. // equivalent boundedly stale concurrency modes. On the other hand,
  211. // boundedly stale reads usually return fresher results.
  212. //
  213. // See
  214. // [TransactionOptions.ReadOnly.read_timestamp][google.spanner.v1.TransactionOptions.ReadOnly.read_timestamp]
  215. // and
  216. // [TransactionOptions.ReadOnly.exact_staleness][google.spanner.v1.TransactionOptions.ReadOnly.exact_staleness].
  217. //
  218. // Bounded staleness:
  219. //
  220. // Bounded staleness modes allow Cloud Spanner to pick the read timestamp,
  221. // subject to a user-provided staleness bound. Cloud Spanner chooses the
  222. // newest timestamp within the staleness bound that allows execution
  223. // of the reads at the closest available replica without blocking.
  224. //
  225. // All rows yielded are consistent with each other -- if any part of
  226. // the read observes a transaction, all parts of the read see the
  227. // transaction. Boundedly stale reads are not repeatable: two stale
  228. // reads, even if they use the same staleness bound, can execute at
  229. // different timestamps and thus return inconsistent results.
  230. //
  231. // Boundedly stale reads execute in two phases: the first phase
  232. // negotiates a timestamp among all replicas needed to serve the
  233. // read. In the second phase, reads are executed at the negotiated
  234. // timestamp.
  235. //
  236. // As a result of the two phase execution, bounded staleness reads are
  237. // usually a little slower than comparable exact staleness
  238. // reads. However, they are typically able to return fresher
  239. // results, and are more likely to execute at the closest replica.
  240. //
  241. // Because the timestamp negotiation requires up-front knowledge of
  242. // which rows will be read, it can only be used with single-use
  243. // read-only transactions.
  244. //
  245. // See
  246. // [TransactionOptions.ReadOnly.max_staleness][google.spanner.v1.TransactionOptions.ReadOnly.max_staleness]
  247. // and
  248. // [TransactionOptions.ReadOnly.min_read_timestamp][google.spanner.v1.TransactionOptions.ReadOnly.min_read_timestamp].
  249. //
  250. // Old read timestamps and garbage collection:
  251. //
  252. // Cloud Spanner continuously garbage collects deleted and overwritten data
  253. // in the background to reclaim storage space. This process is known
  254. // as "version GC". By default, version GC reclaims versions after they
  255. // are one hour old. Because of this, Cloud Spanner cannot perform reads
  256. // at read timestamps more than one hour in the past. This
  257. // restriction also applies to in-progress reads and/or SQL queries whose
  258. // timestamp become too old while executing. Reads and SQL queries with
  259. // too-old read timestamps fail with the error `FAILED_PRECONDITION`.
  260. //
  261. // You can configure and extend the `VERSION_RETENTION_PERIOD` of a
  262. // database up to a period as long as one week, which allows Cloud Spanner
  263. // to perform reads up to one week in the past.
  264. //
  265. // Querying change Streams:
  266. //
  267. // A Change Stream is a schema object that can be configured to watch data
  268. // changes on the entire database, a set of tables, or a set of columns
  269. // in a database.
  270. //
  271. // When a change stream is created, Spanner automatically defines a
  272. // corresponding SQL Table-Valued Function (TVF) that can be used to query
  273. // the change records in the associated change stream using the
  274. // ExecuteStreamingSql API. The name of the TVF for a change stream is
  275. // generated from the name of the change stream: READ_<change_stream_name>.
  276. //
  277. // All queries on change stream TVFs must be executed using the
  278. // ExecuteStreamingSql API with a single-use read-only transaction with a
  279. // strong read-only timestamp_bound. The change stream TVF allows users to
  280. // specify the start_timestamp and end_timestamp for the time range of
  281. // interest. All change records within the retention period is accessible
  282. // using the strong read-only timestamp_bound. All other TransactionOptions
  283. // are invalid for change stream queries.
  284. //
  285. // In addition, if TransactionOptions.read_only.return_read_timestamp is set
  286. // to true, a special value of 2^63 - 2 will be returned in the
  287. // [Transaction][google.spanner.v1.Transaction] message that describes the
  288. // transaction, instead of a valid read timestamp. This special value should be
  289. // discarded and not used for any subsequent queries.
  290. //
  291. // Please see https://cloud.google.com/spanner/docs/change-streams
  292. // for more details on how to query the change stream TVFs.
  293. //
  294. // Partitioned DML transactions:
  295. //
  296. // Partitioned DML transactions are used to execute DML statements with a
  297. // different execution strategy that provides different, and often better,
  298. // scalability properties for large, table-wide operations than DML in a
  299. // ReadWrite transaction. Smaller scoped statements, such as an OLTP workload,
  300. // should prefer using ReadWrite transactions.
  301. //
  302. // Partitioned DML partitions the keyspace and runs the DML statement on each
  303. // partition in separate, internal transactions. These transactions commit
  304. // automatically when complete, and run independently from one another.
  305. //
  306. // To reduce lock contention, this execution strategy only acquires read locks
  307. // on rows that match the WHERE clause of the statement. Additionally, the
  308. // smaller per-partition transactions hold locks for less time.
  309. //
  310. // That said, Partitioned DML is not a drop-in replacement for standard DML used
  311. // in ReadWrite transactions.
  312. //
  313. // - The DML statement must be fully-partitionable. Specifically, the statement
  314. // must be expressible as the union of many statements which each access only
  315. // a single row of the table.
  316. //
  317. // - The statement is not applied atomically to all rows of the table. Rather,
  318. // the statement is applied atomically to partitions of the table, in
  319. // independent transactions. Secondary index rows are updated atomically
  320. // with the base table rows.
  321. //
  322. // - Partitioned DML does not guarantee exactly-once execution semantics
  323. // against a partition. The statement will be applied at least once to each
  324. // partition. It is strongly recommended that the DML statement should be
  325. // idempotent to avoid unexpected results. For instance, it is potentially
  326. // dangerous to run a statement such as
  327. // `UPDATE table SET column = column + 1` as it could be run multiple times
  328. // against some rows.
  329. //
  330. // - The partitions are committed automatically - there is no support for
  331. // Commit or Rollback. If the call returns an error, or if the client issuing
  332. // the ExecuteSql call dies, it is possible that some rows had the statement
  333. // executed on them successfully. It is also possible that statement was
  334. // never executed against other rows.
  335. //
  336. // - Partitioned DML transactions may only contain the execution of a single
  337. // DML statement via ExecuteSql or ExecuteStreamingSql.
  338. //
  339. // - If any error is encountered during the execution of the partitioned DML
  340. // operation (for instance, a UNIQUE INDEX violation, division by zero, or a
  341. // value that cannot be stored due to schema constraints), then the
  342. // operation is stopped at that point and an error is returned. It is
  343. // possible that at this point, some partitions have been committed (or even
  344. // committed multiple times), and other partitions have not been run at all.
  345. //
  346. // Given the above, Partitioned DML is good fit for large, database-wide,
  347. // operations that are idempotent, such as deleting old rows from a very large
  348. // table.
  349. message TransactionOptions {
  350. // Message type to initiate a read-write transaction. Currently this
  351. // transaction type has no options.
  352. message ReadWrite {
  353. // `ReadLockMode` is used to set the read lock mode for read-write
  354. // transactions.
  355. enum ReadLockMode {
  356. // Default value.
  357. //
  358. // If the value is not specified, the pessimistic read lock is used.
  359. READ_LOCK_MODE_UNSPECIFIED = 0;
  360. // Pessimistic lock mode.
  361. //
  362. // Read locks are acquired immediately on read.
  363. PESSIMISTIC = 1;
  364. // Optimistic lock mode.
  365. //
  366. // Locks for reads within the transaction are not acquired on read.
  367. // Instead the locks are acquired on a commit to validate that
  368. // read/queried data has not changed since the transaction started.
  369. OPTIMISTIC = 2;
  370. }
  371. // Read lock mode for the transaction.
  372. ReadLockMode read_lock_mode = 1;
  373. }
  374. // Message type to initiate a Partitioned DML transaction.
  375. message PartitionedDml {}
  376. // Message type to initiate a read-only transaction.
  377. message ReadOnly {
  378. // How to choose the timestamp for the read-only transaction.
  379. oneof timestamp_bound {
  380. // Read at a timestamp where all previously committed transactions
  381. // are visible.
  382. bool strong = 1;
  383. // Executes all reads at a timestamp >= `min_read_timestamp`.
  384. //
  385. // This is useful for requesting fresher data than some previous
  386. // read, or data that is fresh enough to observe the effects of some
  387. // previously committed transaction whose timestamp is known.
  388. //
  389. // Note that this option can only be used in single-use transactions.
  390. //
  391. // A timestamp in RFC3339 UTC \"Zulu\" format, accurate to nanoseconds.
  392. // Example: `"2014-10-02T15:01:23.045123456Z"`.
  393. google.protobuf.Timestamp min_read_timestamp = 2;
  394. // Read data at a timestamp >= `NOW - max_staleness`
  395. // seconds. Guarantees that all writes that have committed more
  396. // than the specified number of seconds ago are visible. Because
  397. // Cloud Spanner chooses the exact timestamp, this mode works even if
  398. // the client's local clock is substantially skewed from Cloud Spanner
  399. // commit timestamps.
  400. //
  401. // Useful for reading the freshest data available at a nearby
  402. // replica, while bounding the possible staleness if the local
  403. // replica has fallen behind.
  404. //
  405. // Note that this option can only be used in single-use
  406. // transactions.
  407. google.protobuf.Duration max_staleness = 3;
  408. // Executes all reads at the given timestamp. Unlike other modes,
  409. // reads at a specific timestamp are repeatable; the same read at
  410. // the same timestamp always returns the same data. If the
  411. // timestamp is in the future, the read will block until the
  412. // specified timestamp, modulo the read's deadline.
  413. //
  414. // Useful for large scale consistent reads such as mapreduces, or
  415. // for coordinating many reads against a consistent snapshot of the
  416. // data.
  417. //
  418. // A timestamp in RFC3339 UTC \"Zulu\" format, accurate to nanoseconds.
  419. // Example: `"2014-10-02T15:01:23.045123456Z"`.
  420. google.protobuf.Timestamp read_timestamp = 4;
  421. // Executes all reads at a timestamp that is `exact_staleness`
  422. // old. The timestamp is chosen soon after the read is started.
  423. //
  424. // Guarantees that all writes that have committed more than the
  425. // specified number of seconds ago are visible. Because Cloud Spanner
  426. // chooses the exact timestamp, this mode works even if the client's
  427. // local clock is substantially skewed from Cloud Spanner commit
  428. // timestamps.
  429. //
  430. // Useful for reading at nearby replicas without the distributed
  431. // timestamp negotiation overhead of `max_staleness`.
  432. google.protobuf.Duration exact_staleness = 5;
  433. }
  434. // If true, the Cloud Spanner-selected read timestamp is included in
  435. // the [Transaction][google.spanner.v1.Transaction] message that describes
  436. // the transaction.
  437. bool return_read_timestamp = 6;
  438. }
  439. // Required. The type of transaction.
  440. oneof mode {
  441. // Transaction may write.
  442. //
  443. // Authorization to begin a read-write transaction requires
  444. // `spanner.databases.beginOrRollbackReadWriteTransaction` permission
  445. // on the `session` resource.
  446. ReadWrite read_write = 1;
  447. // Partitioned DML transaction.
  448. //
  449. // Authorization to begin a Partitioned DML transaction requires
  450. // `spanner.databases.beginPartitionedDmlTransaction` permission
  451. // on the `session` resource.
  452. PartitionedDml partitioned_dml = 3;
  453. // Transaction will not write.
  454. //
  455. // Authorization to begin a read-only transaction requires
  456. // `spanner.databases.beginReadOnlyTransaction` permission
  457. // on the `session` resource.
  458. ReadOnly read_only = 2;
  459. }
  460. }
  461. // A transaction.
  462. message Transaction {
  463. // `id` may be used to identify the transaction in subsequent
  464. // [Read][google.spanner.v1.Spanner.Read],
  465. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql],
  466. // [Commit][google.spanner.v1.Spanner.Commit], or
  467. // [Rollback][google.spanner.v1.Spanner.Rollback] calls.
  468. //
  469. // Single-use read-only transactions do not have IDs, because
  470. // single-use transactions do not support multiple requests.
  471. bytes id = 1;
  472. // For snapshot read-only transactions, the read timestamp chosen
  473. // for the transaction. Not returned by default: see
  474. // [TransactionOptions.ReadOnly.return_read_timestamp][google.spanner.v1.TransactionOptions.ReadOnly.return_read_timestamp].
  475. //
  476. // A timestamp in RFC3339 UTC \"Zulu\" format, accurate to nanoseconds.
  477. // Example: `"2014-10-02T15:01:23.045123456Z"`.
  478. google.protobuf.Timestamp read_timestamp = 2;
  479. }
  480. // This message is used to select the transaction in which a
  481. // [Read][google.spanner.v1.Spanner.Read] or
  482. // [ExecuteSql][google.spanner.v1.Spanner.ExecuteSql] call runs.
  483. //
  484. // See [TransactionOptions][google.spanner.v1.TransactionOptions] for more
  485. // information about transactions.
  486. message TransactionSelector {
  487. // If no fields are set, the default is a single use transaction
  488. // with strong concurrency.
  489. oneof selector {
  490. // Execute the read or SQL query in a temporary transaction.
  491. // This is the most efficient way to execute a transaction that
  492. // consists of a single SQL query.
  493. TransactionOptions single_use = 1;
  494. // Execute the read or SQL query in a previously-started transaction.
  495. bytes id = 2;
  496. // Begin a new transaction and execute this read or SQL query in
  497. // it. The transaction ID of the new transaction is returned in
  498. // [ResultSetMetadata.transaction][google.spanner.v1.ResultSetMetadata.transaction],
  499. // which is a [Transaction][google.spanner.v1.Transaction].
  500. TransactionOptions begin = 3;
  501. }
  502. }