4 SAVEPOINT — define a new savepoint within the current transaction
8 SAVEPOINT savepoint_name
12 SAVEPOINT establishes a new savepoint within the current transaction.
14 A savepoint is a special mark inside a transaction that allows all
15 commands that are executed after it was established to be rolled back,
16 restoring the transaction state to what it was at the time of the
22 The name to give to the new savepoint. If savepoints with the
23 same name already exist, they will be inaccessible until newer
24 identically-named savepoints are released.
28 Use ROLLBACK TO to rollback to a savepoint. Use RELEASE SAVEPOINT to
29 destroy a savepoint, keeping the effects of commands executed after it
32 Savepoints can only be established when inside a transaction block.
33 There can be multiple savepoints defined within a transaction.
37 To establish a savepoint and later undo the effects of all commands
38 executed after it was established:
40 INSERT INTO table1 VALUES (1);
41 SAVEPOINT my_savepoint;
42 INSERT INTO table1 VALUES (2);
43 ROLLBACK TO SAVEPOINT my_savepoint;
44 INSERT INTO table1 VALUES (3);
47 The above transaction will insert the values 1 and 3, but not 2.
49 To establish and later destroy a savepoint:
51 INSERT INTO table1 VALUES (3);
52 SAVEPOINT my_savepoint;
53 INSERT INTO table1 VALUES (4);
54 RELEASE SAVEPOINT my_savepoint;
57 The above transaction will insert both 3 and 4.
59 To use a single savepoint name:
61 INSERT INTO table1 VALUES (1);
62 SAVEPOINT my_savepoint;
63 INSERT INTO table1 VALUES (2);
64 SAVEPOINT my_savepoint;
65 INSERT INTO table1 VALUES (3);
67 -- rollback to the second savepoint
68 ROLLBACK TO SAVEPOINT my_savepoint;
69 SELECT * FROM table1; -- shows rows 1 and 2
71 -- release the second savepoint
72 RELEASE SAVEPOINT my_savepoint;
74 -- rollback to the first savepoint
75 ROLLBACK TO SAVEPOINT my_savepoint;
76 SELECT * FROM table1; -- shows only row 1
79 The above transaction shows row 3 being rolled back first, then row 2.
83 SQL requires a savepoint to be destroyed automatically when another
84 savepoint with the same name is established. In PostgreSQL, the old
85 savepoint is kept, though only the more recent one will be used when
86 rolling back or releasing. (Releasing the newer savepoint with RELEASE
87 SAVEPOINT will cause the older one to again become accessible to
88 ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT.) Otherwise, SAVEPOINT is
93 BEGIN, COMMIT, RELEASE SAVEPOINT, ROLLBACK, ROLLBACK TO SAVEPOINT