3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "SAVEPOINT" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 SAVEPOINT \- define a new savepoint within the current transaction
35 SAVEPOINT \fIsavepoint_name\fR
40 establishes a new savepoint within the current transaction\&.
42 A savepoint is a special mark inside a transaction that allows all commands that are executed after it was established to be rolled back, restoring the transaction state to what it was at the time of the savepoint\&.
47 The name to give to the new savepoint\&. If savepoints with the same name already exist, they will be inaccessible until newer identically\-named savepoints are released\&.
53 to rollback to a savepoint\&. Use
54 \fBRELEASE SAVEPOINT\fR
55 to destroy a savepoint, keeping the effects of commands executed after it was established\&.
57 Savepoints can only be established when inside a transaction block\&. There can be multiple savepoints defined within a transaction\&.
60 To establish a savepoint and later undo the effects of all commands executed after it was established:
67 INSERT INTO table1 VALUES (1);
68 SAVEPOINT my_savepoint;
69 INSERT INTO table1 VALUES (2);
70 ROLLBACK TO SAVEPOINT my_savepoint;
71 INSERT INTO table1 VALUES (3);
78 The above transaction will insert the values 1 and 3, but not 2\&.
80 To establish and later destroy a savepoint:
87 INSERT INTO table1 VALUES (3);
88 SAVEPOINT my_savepoint;
89 INSERT INTO table1 VALUES (4);
90 RELEASE SAVEPOINT my_savepoint;
97 The above transaction will insert both 3 and 4\&.
99 To use a single savepoint name:
106 INSERT INTO table1 VALUES (1);
107 SAVEPOINT my_savepoint;
108 INSERT INTO table1 VALUES (2);
109 SAVEPOINT my_savepoint;
110 INSERT INTO table1 VALUES (3);
112 \-\- rollback to the second savepoint
113 ROLLBACK TO SAVEPOINT my_savepoint;
114 SELECT * FROM table1; \-\- shows rows 1 and 2
116 \-\- release the second savepoint
117 RELEASE SAVEPOINT my_savepoint;
119 \-\- rollback to the first savepoint
120 ROLLBACK TO SAVEPOINT my_savepoint;
121 SELECT * FROM table1; \-\- shows only row 1
128 The above transaction shows row 3 being rolled back first, then row 2\&.
131 SQL requires a savepoint to be destroyed automatically when another savepoint with the same name is established\&. In
132 PostgreSQL, the old savepoint is kept, though only the more recent one will be used when rolling back or releasing\&. (Releasing the newer savepoint with
133 \fBRELEASE SAVEPOINT\fR
134 will cause the older one to again become accessible to
135 \fBROLLBACK TO SAVEPOINT\fR
137 \fBRELEASE SAVEPOINT\fR\&.) Otherwise,
139 is fully SQL conforming\&.
141 \fBBEGIN\fR(7), \fBCOMMIT\fR(7), RELEASE SAVEPOINT (\fBRELEASE_SAVEPOINT\fR(7)), \fBROLLBACK\fR(7), ROLLBACK TO SAVEPOINT (\fBROLLBACK_TO_SAVEPOINT\fR(7))