2 .\" Title: SET TRANSACTION
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 "SET TRANSACTION" "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 SET_TRANSACTION \- set the characteristics of the current transaction
35 SET TRANSACTION \fItransaction_mode\fR [, \&.\&.\&.]
36 SET TRANSACTION SNAPSHOT \fIsnapshot_id\fR
37 SET SESSION CHARACTERISTICS AS TRANSACTION \fItransaction_mode\fR [, \&.\&.\&.]
39 where \fItransaction_mode\fR is one of:
41 ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
42 READ WRITE | READ ONLY
49 command sets the characteristics of the current transaction\&. It has no effect on any subsequent transactions\&.
50 \fBSET SESSION CHARACTERISTICS\fR
51 sets the default transaction characteristics for subsequent transactions of a session\&. These defaults can be overridden by
53 for an individual transaction\&.
55 The available transaction characteristics are the transaction isolation level, the transaction access mode (read/write or read\-only), and the deferrable mode\&. In addition, a snapshot can be selected, though only for the current transaction, not as a session default\&.
57 The isolation level of a transaction determines what data the transaction can see when other transactions are running concurrently:
61 A statement can only see rows committed before it began\&. This is the default\&.
66 All statements of the current transaction can only see rows committed before the first query or data\-modification statement was executed in this transaction\&.
71 All statements of the current transaction can only see rows committed before the first query or data\-modification statement was executed in this transaction\&. If a pattern of reads and writes among concurrent serializable transactions would create a situation which could not have occurred for any serial (one\-at\-a\-time) execution of those transactions, one of them will be rolled back with a
75 The SQL standard defines one additional level,
76 READ UNCOMMITTED\&. In
82 The transaction isolation level cannot be changed after the first query or data\-modification statement (\fBSELECT\fR,
88 \fBCOPY\fR) of a transaction has been executed\&. See
90 for more information about transaction isolation and concurrency control\&.
92 The transaction access mode determines whether the transaction is read/write or read\-only\&. Read/write is the default\&. When a transaction is read\-only, the following SQL commands are disallowed:
98 if the table they would write to is not a temporary table; all
110 if the command they would execute is among those listed\&. This is a high\-level notion of read\-only that does not prevent all writes to disk\&.
114 transaction property has no effect unless the transaction is also
117 READ ONLY\&. When all three of these properties are selected for a transaction, the transaction may block when first acquiring its snapshot, after which it is able to run without the normal overhead of a
119 transaction and without any risk of contributing to or being canceled by a serialization failure\&. This mode is well suited for long\-running reports or backups\&.
122 SET TRANSACTION SNAPSHOT
123 command allows a new transaction to run with the same
125 as an existing transaction\&. The pre\-existing transaction must have exported its snapshot with the
128 Section\ \&9.28.5)\&. That function returns a snapshot identifier, which must be given to
129 SET TRANSACTION SNAPSHOT
130 to specify which snapshot is to be imported\&. The identifier must be written as a string literal in this command, for example
131 \*(Aq00000003\-0000001B\-1\*(Aq\&.
132 SET TRANSACTION SNAPSHOT
133 can only be executed at the start of a transaction, before the first query or data\-modification statement (\fBSELECT\fR,
139 \fBCOPY\fR) of the transaction\&. Furthermore, the transaction must already be set to
143 isolation level (otherwise, the snapshot would be discarded immediately, since
145 mode takes a new snapshot for each command)\&. If the importing transaction uses
147 isolation level, then the transaction that exported the snapshot must also use that isolation level\&. Also, a non\-read\-only serializable transaction cannot import a snapshot from a read\-only transaction\&.
151 \fBSET TRANSACTION\fR
152 is executed without a prior
153 \fBSTART TRANSACTION\fR
155 \fBBEGIN\fR, it emits a warning and otherwise has no effect\&.
157 It is possible to dispense with
158 \fBSET TRANSACTION\fR
159 by instead specifying the desired
160 \fItransaction_modes\fR
164 \fBSTART TRANSACTION\fR\&. But that option is not available for
165 \fBSET TRANSACTION SNAPSHOT\fR\&.
167 The session default transaction modes can also be set or examined via the configuration parameters
168 default_transaction_isolation,
169 default_transaction_read_only, and
170 default_transaction_deferrable\&. (In fact
171 \fBSET SESSION CHARACTERISTICS\fR
172 is just a verbose equivalent for setting these variables with
173 \fBSET\fR\&.) This means the defaults can be set in the configuration file, via
174 \fBALTER DATABASE\fR, etc\&. Consult
176 for more information\&.
178 The current transaction\*(Aqs modes can similarly be set or examined via the configuration parameters
179 transaction_isolation,
180 transaction_read_only, and
181 transaction_deferrable\&. Setting one of these parameters acts the same as the corresponding
182 \fBSET TRANSACTION\fR
183 option, with the same restrictions on when it can be done\&. However, these parameters cannot be set in the configuration file, or from any source other than live SQL\&.
186 To begin a new transaction with the same snapshot as an already existing transaction, first export the snapshot from the existing transaction\&. That will return the snapshot identifier, for example:
192 BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
193 SELECT pg_export_snapshot();
195 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
196 00000003\-0000001B\-1
203 Then give the snapshot identifier in a
204 \fBSET TRANSACTION SNAPSHOT\fR
205 command at the beginning of the newly opened transaction:
211 BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
212 SET TRANSACTION SNAPSHOT \*(Aq00000003\-0000001B\-1\*(Aq;
219 These commands are defined in the
221 standard, except for the
223 transaction mode and the
224 \fBSET TRANSACTION SNAPSHOT\fR
230 is the default transaction isolation level in the standard\&. In
232 the default is ordinarily
233 READ COMMITTED, but you can change it as mentioned above\&.
235 In the SQL standard, there is one other transaction characteristic that can be set with these commands: the size of the diagnostics area\&. This concept is specific to embedded SQL, and therefore is not implemented in the
239 The SQL standard requires commas between successive
240 \fItransaction_modes\fR, but for historical reasons
242 allows the commas to be omitted\&.