1SAVEPOINT(7) PostgreSQL 14.3 Documentation SAVEPOINT(7)
2
3
4
6 SAVEPOINT - define a new savepoint within the current transaction
7
9 SAVEPOINT savepoint_name
10
12 SAVEPOINT establishes a new savepoint within the current transaction.
13
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
17 savepoint.
18
20 savepoint_name
21 The name to give to the new savepoint.
22
24 Use ROLLBACK TO to rollback to a savepoint. Use RELEASE SAVEPOINT to
25 destroy a savepoint, keeping the effects of commands executed after it
26 was established.
27
28 Savepoints can only be established when inside a transaction block.
29 There can be multiple savepoints defined within a transaction.
30
32 To establish a savepoint and later undo the effects of all commands
33 executed after it was established:
34
35 BEGIN;
36 INSERT INTO table1 VALUES (1);
37 SAVEPOINT my_savepoint;
38 INSERT INTO table1 VALUES (2);
39 ROLLBACK TO SAVEPOINT my_savepoint;
40 INSERT INTO table1 VALUES (3);
41 COMMIT;
42
43 The above transaction will insert the values 1 and 3, but not 2.
44
45 To establish and later destroy a savepoint:
46
47 BEGIN;
48 INSERT INTO table1 VALUES (3);
49 SAVEPOINT my_savepoint;
50 INSERT INTO table1 VALUES (4);
51 RELEASE SAVEPOINT my_savepoint;
52 COMMIT;
53
54 The above transaction will insert both 3 and 4.
55
57 SQL requires a savepoint to be destroyed automatically when another
58 savepoint with the same name is established. In PostgreSQL, the old
59 savepoint is kept, though only the more recent one will be used when
60 rolling back or releasing. (Releasing the newer savepoint with RELEASE
61 SAVEPOINT will cause the older one to again become accessible to
62 ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT.) Otherwise, SAVEPOINT is
63 fully SQL conforming.
64
66 BEGIN(7), COMMIT(7), RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7)),
67 ROLLBACK(7), ROLLBACK TO SAVEPOINT (ROLLBACK_TO_SAVEPOINT(7))
68
69
70
71PostgreSQL 14.3 2022 SAVEPOINT(7)