SAVEPOINT
Description
SAVEPOINT establishes a new savepoint within the current transaction.
A savepoint is a special mark inside a transaction. It allows all statements that are executed after it was established to be rolled back, restoring the transaction state to what it was at the time of the savepoint.
Precautions
- Use ROLLBACK TO SAVEPOINT to roll back to a savepoint. Use RELEASE SAVEPOINT to destroy a savepoint but keep the effects of the statements executed after the savepoint was established.
- Savepoints can only be established when inside a transaction block. Multiple savepoints can be defined in a transaction.
- In the case of an unexpected termination of a distributed thread or process caused by a node or connection failure, or of an error caused by the inconsistency between source and destination table structures in a COPY FROM operation, the transaction cannot be rolled back to the established savepoint. Instead, the entire transaction will be rolled back.
- According to the SQL standard, when a savepoint with the same name is created, the previous savepoint with the same name is automatically deleted. In GaussDB, the old savepoint is retained, but only the latest one is used during rollback or release. If the latest savepoint is released, the previous savepoint will again become accessible to ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT. In addition, SAVEPOINT fully complies with the SQL standard.
Syntax
SAVEPOINT savepoint_name;
Parameters
savepoint_name
Specifies the name of the new savepoint.
When using SAVEPOINT, you are advised to release SAVEPOINT promptly to avoid too many nested subtransactions. It is recommended that the number of nested subtransactions be less than or equal to 10,000. If the number of nested subtransactions is too large, the current transaction performance may deteriorate.
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
-- Create a table. gaussdb=# CREATE TABLE table1(a int); -- Start a transaction. gaussdb=# START TRANSACTION; -- Insert data. gaussdb=# INSERT INTO table1 VALUES (1); -- Create a savepoint. gaussdb=# SAVEPOINT my_savepoint; -- Insert data. gaussdb=# INSERT INTO table1 VALUES (2); -- Roll back the savepoint. gaussdb=# ROLLBACK TO SAVEPOINT my_savepoint; -- Insert data. gaussdb=# INSERT INTO table1 VALUES (3); -- Commit the transaction. gaussdb=# COMMIT; -- Query the content of the table. You can see 1 and 3 at the same time, but cannot see 2 because 2 is rolled back. gaussdb=# SELECT * FROM table1; -- Delete the table. gaussdb=# DROP TABLE table1; -- Create a table. gaussdb=# CREATE TABLE table2(a int); -- Start a transaction. gaussdb=# START TRANSACTION; -- Insert data. gaussdb=# INSERT INTO table2 VALUES (3); -- Create a savepoint. gaussdb=# SAVEPOINT my_savepoint; -- Insert data. gaussdb=# INSERT INTO table2 VALUES (4); -- Roll back the savepoint. gaussdb=# RELEASE SAVEPOINT my_savepoint; -- Commit the transaction. gaussdb=# COMMIT; -- Query the table content. You can see 3 and 4 at the same time. gaussdb=# SELECT * FROM table2; -- Delete the table. gaussdb=# DROP TABLE table2; |
Helpful Links
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot