]> begriffs open source - ai-pg/blob - full-docs/man7/SAVEPOINT.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / SAVEPOINT.7
1 '\" t
2 .\"     Title: SAVEPOINT
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 SAVEPOINT \- define a new savepoint within the current transaction
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 SAVEPOINT \fIsavepoint_name\fR
36 .fi
37 .SH "DESCRIPTION"
38 .PP
39 \fBSAVEPOINT\fR
40 establishes a new savepoint within the current transaction\&.
41 .PP
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\&.
43 .SH "PARAMETERS"
44 .PP
45 \fIsavepoint_name\fR
46 .RS 4
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\&.
48 .RE
49 .SH "NOTES"
50 .PP
51 Use
52 \fBROLLBACK TO\fR
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\&.
56 .PP
57 Savepoints can only be established when inside a transaction block\&. There can be multiple savepoints defined within a transaction\&.
58 .SH "EXAMPLES"
59 .PP
60 To establish a savepoint and later undo the effects of all commands executed after it was established:
61 .sp
62 .if n \{\
63 .RS 4
64 .\}
65 .nf
66 BEGIN;
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);
72 COMMIT;
73 .fi
74 .if n \{\
75 .RE
76 .\}
77 .sp
78 The above transaction will insert the values 1 and 3, but not 2\&.
79 .PP
80 To establish and later destroy a savepoint:
81 .sp
82 .if n \{\
83 .RS 4
84 .\}
85 .nf
86 BEGIN;
87     INSERT INTO table1 VALUES (3);
88     SAVEPOINT my_savepoint;
89     INSERT INTO table1 VALUES (4);
90     RELEASE SAVEPOINT my_savepoint;
91 COMMIT;
92 .fi
93 .if n \{\
94 .RE
95 .\}
96 .sp
97 The above transaction will insert both 3 and 4\&.
98 .PP
99 To use a single savepoint name:
100 .sp
101 .if n \{\
102 .RS 4
103 .\}
104 .nf
105 BEGIN;
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);
111
112     \-\- rollback to the second savepoint
113     ROLLBACK TO SAVEPOINT my_savepoint;
114     SELECT * FROM table1;               \-\- shows rows 1 and 2
115
116     \-\- release the second savepoint
117     RELEASE SAVEPOINT my_savepoint;
118
119     \-\- rollback to the first savepoint
120     ROLLBACK TO SAVEPOINT my_savepoint;
121     SELECT * FROM table1;               \-\- shows only row 1
122 COMMIT;
123 .fi
124 .if n \{\
125 .RE
126 .\}
127 .sp
128 The above transaction shows row 3 being rolled back first, then row 2\&.
129 .SH "COMPATIBILITY"
130 .PP
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
136 and
137 \fBRELEASE SAVEPOINT\fR\&.) Otherwise,
138 \fBSAVEPOINT\fR
139 is fully SQL conforming\&.
140 .SH "SEE ALSO"
141 \fBBEGIN\fR(7), \fBCOMMIT\fR(7), RELEASE SAVEPOINT (\fBRELEASE_SAVEPOINT\fR(7)), \fBROLLBACK\fR(7), ROLLBACK TO SAVEPOINT (\fBROLLBACK_TO_SAVEPOINT\fR(7))