1SAVEPOINT(7)             PostgreSQL 15.4 Documentation            SAVEPOINT(7)
2
3
4

NAME

6       SAVEPOINT - define a new savepoint within the current transaction
7

SYNOPSIS

9       SAVEPOINT savepoint_name
10

DESCRIPTION

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

PARAMETERS

20       savepoint_name
21           The name to give to the new savepoint. If savepoints with the same
22           name already exist, they will be inaccessible until newer
23           identically-named savepoints are released.
24

NOTES

26       Use ROLLBACK TO to rollback to a savepoint. Use RELEASE SAVEPOINT to
27       destroy a savepoint, keeping the effects of commands executed after it
28       was established.
29
30       Savepoints can only be established when inside a transaction block.
31       There can be multiple savepoints defined within a transaction.
32

EXAMPLES

34       To establish a savepoint and later undo the effects of all commands
35       executed after it was established:
36
37           BEGIN;
38               INSERT INTO table1 VALUES (1);
39               SAVEPOINT my_savepoint;
40               INSERT INTO table1 VALUES (2);
41               ROLLBACK TO SAVEPOINT my_savepoint;
42               INSERT INTO table1 VALUES (3);
43           COMMIT;
44
45       The above transaction will insert the values 1 and 3, but not 2.
46
47       To establish and later destroy a savepoint:
48
49           BEGIN;
50               INSERT INTO table1 VALUES (3);
51               SAVEPOINT my_savepoint;
52               INSERT INTO table1 VALUES (4);
53               RELEASE SAVEPOINT my_savepoint;
54           COMMIT;
55
56       The above transaction will insert both 3 and 4.
57
58       To use a single savepoint name:
59
60           BEGIN;
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);
66
67               -- rollback to the second savepoint
68               ROLLBACK TO SAVEPOINT my_savepoint;
69               SELECT * FROM table1;               -- shows rows 1 and 2
70
71               -- release the second savepoint
72               RELEASE SAVEPOINT my_savepoint;
73
74               -- rollback to the first savepoint
75               ROLLBACK TO SAVEPOINT my_savepoint;
76               SELECT * FROM table1;               -- shows only row 1
77           COMMIT;
78
79       The above transaction shows row 3 being rolled back first, then row 2.
80

COMPATIBILITY

82       SQL requires a savepoint to be destroyed automatically when another
83       savepoint with the same name is established. In PostgreSQL, the old
84       savepoint is kept, though only the more recent one will be used when
85       rolling back or releasing. (Releasing the newer savepoint with RELEASE
86       SAVEPOINT will cause the older one to again become accessible to
87       ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT.) Otherwise, SAVEPOINT is
88       fully SQL conforming.
89

SEE ALSO

91       BEGIN(7), COMMIT(7), RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7)),
92       ROLLBACK(7), ROLLBACK TO SAVEPOINT (ROLLBACK_TO_SAVEPOINT(7))
93
94
95
96PostgreSQL 15.4                      2023                         SAVEPOINT(7)
Impressum