1RELEASE SAVEPOINT(7) PostgreSQL 16.1 Documentation RELEASE SAVEPOINT(7)
2
3
4
6 RELEASE_SAVEPOINT - release a previously defined savepoint
7
9 RELEASE [ SAVEPOINT ] savepoint_name
10
12 RELEASE SAVEPOINT releases the named savepoint and all active
13 savepoints that were created after the named savepoint, and frees their
14 resources. All changes made since the creation of the savepoint that
15 didn't already get rolled back are merged into the transaction or
16 savepoint that was active when the named savepoint was created. Changes
17 made after RELEASE SAVEPOINT will also be part of this active
18 transaction or savepoint.
19
21 savepoint_name
22 The name of the savepoint to release.
23
25 Specifying a savepoint name that was not previously defined is an
26 error.
27
28 It is not possible to release a savepoint when the transaction is in an
29 aborted state; to do that, use ROLLBACK TO SAVEPOINT
30 (ROLLBACK_TO_SAVEPOINT(7)).
31
32 If multiple savepoints have the same name, only the most recently
33 defined unreleased one is released. Repeated commands will release
34 progressively older savepoints.
35
37 To establish and later release a savepoint:
38
39 BEGIN;
40 INSERT INTO table1 VALUES (3);
41 SAVEPOINT my_savepoint;
42 INSERT INTO table1 VALUES (4);
43 RELEASE SAVEPOINT my_savepoint;
44 COMMIT;
45
46 The above transaction will insert both 3 and 4.
47
48 A more complex example with multiple nested subtransactions:
49
50 BEGIN;
51 INSERT INTO table1 VALUES (1);
52 SAVEPOINT sp1;
53 INSERT INTO table1 VALUES (2);
54 SAVEPOINT sp2;
55 INSERT INTO table1 VALUES (3);
56 RELEASE SAVEPOINT sp2;
57 INSERT INTO table1 VALUES (4))); -- generates an error
58
59 In this example, the application requests the release of the savepoint
60 sp2, which inserted 3. This changes the insert's transaction context to
61 sp1. When the statement attempting to insert value 4 generates an
62 error, the insertion of 2 and 4 are lost because they are in the same,
63 now-rolled back savepoint, and value 3 is in the same transaction
64 context. The application can now only choose one of these two commands,
65 since all other commands will be ignored:
66
67 ROLLBACK;
68 ROLLBACK TO SAVEPOINT sp1;
69
70 Choosing ROLLBACK will abort everything, including value 1, whereas
71 ROLLBACK TO SAVEPOINT sp1 will retain value 1 and allow the transaction
72 to continue.
73
75 This command conforms to the SQL standard. The standard specifies that
76 the key word SAVEPOINT is mandatory, but PostgreSQL allows it to be
77 omitted.
78
80 BEGIN(7), COMMIT(7), ROLLBACK(7), ROLLBACK TO SAVEPOINT
81 (ROLLBACK_TO_SAVEPOINT(7)), SAVEPOINT(7)
82
83
84
85PostgreSQL 16.1 2023 RELEASE SAVEPOINT(7)