The abort statement, when used without the optional to savepoint_name clause, allows the user to terminate an in-progress multi-statement transaction (MST) at any time before the transaction is committed with an explicit end transaction statement. The abort statement causes all database changes effected by the MST to be undone and terminates the MST.
You also have the option of aborting part of a transaction to a pre-declared savepoint. The abort statement with the optional to savepoint_name clause undoes the database effects of all statements in the MST that follow the declaration of the named savepoint. Following an abort to savepoint_name, the MST remains in progress, and new statements can be added to the MST in the normal fashion. Repeated aborts can be executed to the same savepoint.
This statement has the following format:
ABORT [TO savepoint_name]
Note: When executing an abort to a given savepoint, all savepoints declared after the named savepoint are nullified.
The following example begins a transaction, executes some SQL statements, and aborts the transaction before committing the database changes:
begin transaction;
insert into emp (name, sal, bdate)
values ('Jones,Bill', 100000, 1814);
insert into emp (name, sal, bdate)
values ('Jones,Bill', 100000, 1714);
abort; \g
/* undoes both inserts; table is unchanged */
The following example begins a transaction, establishes savepoints, and does a partial abort of the MST:
begin transaction;
insert into emp (name, sal, bdate)
values ('Jones,Bill', 100000, 1945);
savepoint setone;
insert into emp (name, sal, bdate)
values ('Smith,Stan', 50000, 1911);
savepoint 2; \g
/* undoes second insert; deactivates savepoint 2 */
abort to setone; \g
insert into emp (name, sal, bdate)
values ('Smith,Stan', 50000, 1948);
abort to setone; \g
end transaction; \g
/* only the first insert is committed */