Purpose
You can use this statement to create a savepoint in a transaction to partially roll back the transaction.
Syntax
Create a savepoint:
SAVEPOINT savepoint_name;Roll back a transaction to a savepoint:
ROLLBACK [WORK] TO [SAVEPOINT] savepoint_name;Release a savepoint:
RELEASE SAVEPOINT savepoint_name;
Parameters
| Parameter | Description |
|---|---|
| [WORK] | An optional keyword that does not affect the semantics. |
| savepoint_name | The name of the savepoint. The specified savepoint is unique within the transaction. Only the latest one of the savepoints with the same name is retained. After you create a savepoint, you can roll back the transaction to the specified savepoint or use the ROLLBACK statement to roll back the entire transaction. |
Examples
Assume that you have executed the statements in the following table for a transaction.
| sql_no | Statement | Partition |
|---|---|---|
| 1 | UPDATE …; | p1, p4 |
| SAVEPOINT sp1; | ||
| 2 | UPDATE …; | p2, p4 |
| 3 | UPDATE …; | p3, p5 |
| SAVEPOINT sp2; | ||
| 4 | UPDATE …; | p1, p3, p6 |
| 5 | UPDATE …; | p1, p5 |
| SAVEPOINT sp3; | ||
| 6 | SELECT …; | |
| 7 | UPDATE …; | p5, p6 |
| SAVEPOINT sp4; |
Record savepoints
You can create savepoints before committing the transaction. You must create a linked list of savepoints based on the order in which they are created. The preceding transaction contains seven SQL statements and four savepoints. The following figure shows the linked list of savepoints. Each node records the mapping of <savepoint_name, sql_no>.

Participant partition list of a transaction
To enable the rollback of all modifications made after an SQL statement was executed, the system records the involved partitions and the corresponding sql_no values. The preceding transaction has executed seven SQL statements, involving six partitions, which are partitions p1 to p6.

Roll back a transaction to a savepoint
Query the
sql_novalue corresponding tosavepoint_namebased on the linked list of savepoints.Assume that you need to execute
ROLLBACK to SAVEPOINT sp2. It can be learned from the linked list of savepoints that thesql_novalue corresponding tosp2is3.Query the partition corresponding to the
sql_novalue based on the participant partition list of the transaction.It can be learned from the participant partition list of the transaction that statements with a
sql_nogreater than3are executed on partitionsp1,p3,p5, andp6.When rolling back partition data, the scheduler initiates rollback requests to the partitions determined in Step 2 to roll back all the modifications made by this transaction on these partitions after
sp2.Some modifications made by this transaction on
p1,p3, andp5are rolled back, and all modifications made by this transaction onp6are rolled back.Update the participant partition list of the transaction.
Delete
sql_novalues greater than3from the participant partition list of the transaction. All modifications onp6are rolled back. Therefore,p6can be deleted from the list.
Release invalid savepoints.
After you execute
ROLLBACK to SAVEPOINT sp2, the system releases savepointssp3andsp4. The transaction can no longer be rolled back tosp3orsp4.