Project: YugabyteDB (App Definition and Development / Database)
File: acid-transactions-ysql.md

title: Transactions in YSQL headerTitle: Transactions linkTitle: 4. Transactions description: Learn how to use Transactions in YSQL on YugabyteDB. menu: v2.18: identifier: acid-transactions-1-ysql parent: learn weight: 566 rightNav: hideH4: true type: docs {{<tabs>}} {{<tabitem href="../acid-transactions-ysql/" text="YSQL" icon="postgres" active="true" >}} {{<tabitem href="../acid-transactions-ycql/" text="YCQL" icon="cassandra" >}} {{</tabs>}} In YugabyteDB, a transaction is a sequence of operations performed as a single logical unit of work. The essential point of a transaction is that it bundles multiple steps into a single, all-or-nothing operation. The intermediate states between the steps are not visible to other concurrent transactions, and if some failure occurs that prevents the transaction from completing, then none of the steps affect the database at all. In YugabyteDB, a transaction is the set of commands inside a - block. For example: ```plpgsql BEGIN; UPDATE accounts SET balance = balance + 1000.00 WHERE name = 'John Smith'; -- other statements COMMIT; ``` The `BEGIN` and `COMMIT` block is needed when you have multiple statements to be executed as part of a transaction. YugabyteDB treats every ad-hoc individual SQL statement as being executed in a transaction. If you decide to cancel the transaction and not commit it, you can issue a `ROLLBACK` instead of a `COMMIT`. You can also control the rollback of a subset of statements using `SAVEPOINT`. After rolling back to a savepoint, it continues to be defined, so you can roll back to it several times. As all transactions in YugabyteDB are guaranteed to be compliant, errors can be thrown during transaction processing to ensure correctness guarantees are not violated. YugabyteDB returns different for each case with details. Applications need to be designed to do correctly for high availability. {{<tip title="In action">}} For an example of how a transaction is run, see . {{</tip>}} The following commands are typically involved in a transaction flow: | Command | Description | Example | | : | :- | : | | | Start a transaction. This is the first statement in a transaction. | BEGIN TRANSACTION | | | Set session-level transaction settings | SET idleintransactionsessiontimeout = 10000 | | | Display session-level transaction settings | SHOW idleintransactionsessiontimeout | | | Set the isolation level. | SET TRANSACTION SERIALIZABLE | | | Create a checkpoint. | SAVEPOINT yb_save| | | Rollback to a specific savepoint. | ROLLBACK TO SAVEPOINT yb_save | | | Destroy a savepoint. | RELEASE yb_save | | | Cancel a transaction. | ROLLBACK | | | Apply the transaction to the tables. | COMMIT | {{<tip title="Settings">}} For an overview of what settings can be set for a transaction, see . {{</tip>}} The isolation level defines the level of data visibility to the transaction. YugabyteDB supports , which enables the isolation of concurrent transactions without the need for locking. YugabyteDB supports three kinds of isolation levels to support different application needs. | Level | Description | | :- | :- | | | Only the data that is committed before the transaction began is visible to the transaction. Effectively, the transaction sees the snapshot of the database as of the start of the

{{<note>}}Applications using this isolation level should be designed to on serialization failures.{{</note>}} | | | Each statement of the transaction sees the latest data committed by any concurrent transaction just before the execution of the statement. If another transaction has modified a row related to the current transaction, the current transaction waits for the other transaction to commit or rollback its changes. {{<note>}} The server internally waits and retries on conflicts, so applications on serialization failures.{{</note>}} | | | This is the strictest isolation level and has the effect of all transactions being executed in a serial manner, one after the other rather than in parallel. {{<note>}} Applications using this isolation level should be designed to on serialization failures.{{</note>}} | {{<tip title="Examples">}} See to understand the effect of these different levels of isolation. {{</tip>}} Typically statements do not automatically lock the rows fetched during a transaction. Depending on your application needs, you might have to lock the rows retrieved during SELECT. YugabyteDB supports for such cases and ensures that no two transactions can hold locks on the same row. Lock acquisition conflicts are resolved according to policies. Lock acquisition has the following format: ```plpgsql SELECT * FROM txndemo WHERE k=1 FOR UPDATE; ``` YugabyteDB supports the following types of explicit row locks: | Lock | Description | | : | :- | | FOR UPDATE | Strongest and exclusive lock. Prevents all other locks on these rows till the transaction ends.| | FOR&nbsp;NO&nbsp;KEY&nbsp;UPDATE | Weaker than `FOR UPDATE` and exclusive. Will not block `FOR KEY SHARE` commands.| | FOR SHARE | Shared lock that does not block other `FOR SHARE` and `FOR KEY SHARE` commands.| | FOR KEY SHARE | Shared lock that does not block other `FOR SHARE`, `FOR KEY SHARE`, and `FOR NO KEY UPDATE` commands.| {{<tip title="Examples">}} For more details and examples related to these locking policies, see . {{</tip>}} During transaction processing, failures can happen due to the strong properties guaranteed by YugabyteDB. Appropriate are returned for each scenario and applications should adopt the right specific to the isolation levels it uses to be highly available. In general, the error codes can be classified into the following three types: WARNING. Informational messages that explain why a statement failed. For example: ```output -- When a BEGIN statement is issued inside a transaction WARNING: 25001: there is already a transaction in progress ``` Most client libraries hide warnings, but you might notice the messages when you execute statements directly from a terminal. The statement execution can continue without interruption but would need to be modified to avoid the re-occurrence of the message. ERROR: Errors are returned when a transaction cannot continue and has to be restarted by the client. For example: ```output -- When multiple transactions are modifying the same key. ERROR: 40001: Operation expired: Transaction XXXX expired or aborted by a conflict ``` These errors need to be handled by the application to take appropriate action. FATAL. Fatal messages are returned to notify that the connection to a server has been

For example: ```output -- When the application takes a long time to issue a statement in the middle of a transaction. FATAL: 25P03: terminating connection due to idle-in-transaction timeout ``` At this point, the application should reconnect to the server. {{<tip>}} For more details on how to handle failures and retry, see . For an example application and try it out yourself, see . {{</tip>}} All applications need to be tuned to get the best performance. YugabyteDB supports various constructs and that can be adopted and tuned to your needs. Adopting the correct constructs in the right scenarios can immensely improve the performance of your application. Some examples are: Convert a multi-statement transaction affecting a single row into a transaction. with the right timeouts. with `ON CONFLICT` clause. with stored procedures. {{<tip>}} For more examples and details on how to tune your application's performance, see . {{</tip>}} YugabyteDB exports a lot of so that you can see what is going on in your cluster. These metrics can be exported to and visualized in . Many of these metrics are also displayed as charts in YugabyteDB Anywhere and YugabyteDB Managed. The following are key transaction-related metrics. Shows the number of transactions that are currently active. This provides an overview of how transaction intensive the cluster currently is. Describes the number of times transactions have conflicted with other transactions. An increase in the number of conflicts could directly result in increased latency of your applications. Shows the number of transactions that did not complete because the status tablet did not receive enough heartbeats from the node to which the client had connected. This usually happens if that node or process managing the transaction has crashed. The following YSQL parameters affect transactions and can be configured to your application needs. These settings can be set using the command and the current values can be fetched using the command. {{<note title="Note">}} These settings impact all transactions in the current session only. {{</note>}} Turn this setting `ON/TRUE/1` to make all the transactions in the current session read-only. This is helpful when you want to run reports or set up . ```plpgsql SET defaulttransactionread_only = TRUE; ``` Set this to one of `serializable`, `repeatable read`, or `read committed`. This sets the default isolation level for all transactions in the current session. ```plpgsql SET defaulttransactionisolation = 'serializable'; ``` Turn this setting `ON/TRUE/1` to make all the transactions in the current session . This ensures that the transactions are not canceled by a serialization failure. ```plpgsql SET defaulttransactiondeferrable = TRUE; ``` {{<note title="Note">}} The `DEFERRABLE` transaction property has no effect unless the transaction is also `SERIALIZABLE` and `READ ONLY`. {{</note>}} Set this to a duration (for example, `'10s or 1000'`) to limit delays in transaction statements. The default time unit is milliseconds. See . Set this to values in the range `. Set this to values in the range `. - Various error codes returned during transaction processing. - Methods to handle various error codes to design highly available applications. - Various isolation levels supported by YugabyteDB. - Policies to handle conflicts between transactions. - Priority buckets for transactions. - Options supported by transactions.