1SAVEPOINT() SQL Commands SAVEPOINT()
2
3
4
6 SAVEPOINT - define a new savepoint within the current transaction
7
8
10 SAVEPOINT savepoint_name
11
12
14 SAVEPOINT establishes a new savepoint within the current transaction.
15
16 A savepoint is a special mark inside a transaction that allows all com‐
17 mands that are executed after it was established to be rolled back,
18 restoring the transaction state to what it was at the time of the save‐
19 point.
20
22 savepoint_name
23 The name to give to the new savepoint.
24
26 Use ROLLBACK TO SAVEPOINT [rollback_to_savepoint(7)] to rollback to a
27 savepoint. Use RELEASE SAVEPOINT [release_savepoint(7)] to destroy a
28 savepoint, keeping the effects of commands executed after it was estab‐
29 lished.
30
31 Savepoints can only be established when inside a transaction block.
32 There can be multiple savepoints defined within a transaction.
33
35 To establish a savepoint and later undo the effects of all commands
36 executed after it was established:
37
38 BEGIN;
39 INSERT INTO table1 VALUES (1);
40 SAVEPOINT my_savepoint;
41 INSERT INTO table1 VALUES (2);
42 ROLLBACK TO SAVEPOINT my_savepoint;
43 INSERT INTO table1 VALUES (3);
44 COMMIT;
45
46 The above transaction will insert the values 1 and 3, but not 2.
47
48 To establish and later destroy a savepoint:
49
50 BEGIN;
51 INSERT INTO table1 VALUES (3);
52 SAVEPOINT my_savepoint;
53 INSERT INTO table1 VALUES (4);
54 RELEASE SAVEPOINT my_savepoint;
55 COMMIT;
56
57 The above transaction will insert both 3 and 4.
58
60 SQL requires a savepoint to be destroyed automatically when another
61 savepoint with the same name is established. In PostgreSQL, the old
62 savepoint is kept, though only the more recent one will be used when
63 rolling back or releasing. (Releasing the newer savepoint will cause
64 the older one to again become accessible to ROLLBACK TO SAVEPOINT and
65 RELEASE SAVEPOINT.) Otherwise, SAVEPOINT is fully SQL conforming.
66
68 BEGIN [begin(7)], COMMIT [commit(l)], RELEASE SAVEPOINT [release_save‐
69 point(l)], ROLLBACK [rollback(l)], ROLLBACK TO SAVEPOINT [roll‐
70 back_to_savepoint(l)]
71
72
73
74SQL - Language Statements 2008-06-08 SAVEPOINT()