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