After you mark savepoints for a transaction, you can use the ROLLBACK TO SAVEPOINT statement to roll back the transaction to a specified savepoint.
Prerequisites
Savepoints are marked. For more information, see Mark a savepoint.
Roll back a transaction to a savepoint
You can use the following statement to roll back a transaction to a savepoint:
ROLLBACK TO SAVEPOINT pointname;
Here, pointname specifies the name of the transaction savepoint.
The transaction in the following example contains several DML statements and several savepoints. After the transaction is rolled back to a specific savepoint, only the changes after this savepoint are discarded.
View the records in the table.
obclient [test]> SELECT * FROM ordr; +----+------+-------+---------------------+ | id | name | value | gmt_create | +----+------+-------+---------------------+ | 1 | CN | 10001 | 2022-10-19 14:51:12 | | 2 | US | 10002 | 2022-10-19 14:51:12 | | 3 | EN | 10003 | 2022-10-19 14:51:12 | | 4 | JP | NULL | 2022-10-19 14:51:44 | | 5 | CN | NULL | 2022-10-19 14:53:56 | +----+------+-------+---------------------+ 5 rows in setStart a transaction and set several savepoints.
obclient [test]> SET SESSION autocommit=0; Query OK, 0 rows affected obclient [test]> BEGIN; Query OK, 0 rows affected obclient [test]> INSERT INTO ordr(id, name) VALUES(6,'FR'); Query OK, 1 row affected obclient [test]> SAVEPOINT fr; Query OK, 0 rows affected obclient [test]> INSERT INTO ordr(id, name) VALUES(7,'RU'); Query OK, 1 row affected obclient [test]> SAVEPOINT ru; Query OK, 0 rows affected obclient [test]> INSERT INTO ordr(id, name) VALUES(8,'CA'); Query OK, 1 row affected obclient [test]> SAVEPOINT ca; Query OK, 0 rows affectedView the uncommitted changes in the transaction of the current session.
obclient [test]> SELECT * FROM ordr; +----+------+-------+---------------------+ | id | name | value | gmt_create | +----+------+-------+---------------------+ | 1 | CN | 10001 | 2022-10-19 14:51:12 | | 2 | US | 10002 | 2022-10-19 14:51:12 | | 3 | EN | 1003 | 2022-10-19 14:51:12 | | 4 | JP | NULL | 2022-10-19 14:51:44 | | 5 | CN | NULL | 2022-10-19 14:53:56 | | 6 | FR | NULL | 2022-10-19 15:01:39 | | 7 | RU | NULL | 2022-10-19 15:01:50 | | 8 | CA | NULL | 2022-10-19 15:02:15 | +----+------+-------+---------------------+ 8 rows in setRoll back the transaction to the savepoint
ru.obclient [test]> ROLLBACK TO SAVEPOINT ru; Query OK, 0 rows affected obclient [test]> SELECT * FROM ordr; +----+------+-------+---------------------+ | id | name | value | gmt_create | +----+------+-------+---------------------+ | 1 | CN | 10001 | 2022-10-19 14:51:12 | | 2 | US | 10002 | 2022-10-19 14:51:12 | | 3 | EN | 1003 | 2022-10-19 14:51:12 | | 4 | JP | NULL | 2022-10-19 14:51:44 | | 5 | CN | NULL | 2022-10-19 14:53:56 | | 6 | FR | NULL | 2022-10-19 15:01:39 | | 7 | RU | NULL | 2022-10-19 15:01:50 | +----+------+-------+---------------------+ 7 rows in setThe query result shows that the record
(8,'CA')is inserted after the savepointru. Therefore, this change is discarded after the rollback.Commit the transaction and make sure that the latest changes of the table include those made before the savepoint
ru.obclient [test]> COMMIT; Query OK, 0 rows affected obclient [test]> SELECT * FROM ordr; +----+------+-------+---------------------+ | id | name | value | gmt_create | +----+------+-------+---------------------+ | 1 | CN | 10001 | 2022-10-19 14:51:12 | | 2 | US | 10002 | 2022-10-19 14:51:12 | | 3 | EN | 1003 | 2022-10-19 14:51:12 | | 4 | JP | NULL | 2022-10-19 14:51:44 | | 5 | CN | NULL | 2022-10-19 14:53:56 | | 6 | FR | NULL | 2022-10-19 15:01:39 | | 7 | RU | NULL | 2022-10-19 15:01:50 | +----+------+-------+---------------------+ 7 rows in set
For more information about transaction rollback, see Roll back a transaction.