Updated on 2024-08-20 GMT+08:00

ROLLBACK

Description

Rolls back the current transaction and backs out all updates in the transaction.

If a fault occurs during the running of a transaction, the transaction cannot be executed. The system cancels all completed operations on the database in the transaction, and the database status returns to the time when the transaction starts.

Precautions

If a ROLLBACK statement is executed out of a transaction, no error occurs, but a notice is displayed.

Syntax

ROLLBACK [ WORK | TRANSACTION ];

Parameters

WORK | TRANSACTION

Specifies the optional keyword that more clearly illustrates the syntax.

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
-- Create a table.
gaussdb=# CREATE TABLE test (id int, name text);

-- Start a transaction.
gaussdb=# START TRANSACTION;

-- Alter the table structure.
gaussdb=# ALTER TABLE test ADD COLUMN score int;

-- View the table structure.
gaussdb=# \d test;
     Table "public.test"
 Column |  Type   
--------+---------
 id     | integer 
 name   | text    
 score  | integer

-- Perform rollback.
gaussdb=# ROLLBACK;

-- The table structure is restored to the initial state.
gaussdb=# \d test;
     Table "public.test"
 Column |  Type    
--------+---------
 id     | integer  
 name   | text    

Helpful Links

COMMIT | END