1SET TRANSACTION(7) PostgreSQL 9.2.24 Documentation SET TRANSACTION(7)
2
3
4
6 SET_TRANSACTION - set the characteristics of the current transaction
7
9 SET TRANSACTION transaction_mode [, ...]
10 SET TRANSACTION SNAPSHOT snapshot_id
11 SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
12
13 where transaction_mode is one of:
14
15 ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
16 READ WRITE | READ ONLY
17 [ NOT ] DEFERRABLE
18
20 The SET TRANSACTION command sets the characteristics of the current
21 transaction. It has no effect on any subsequent transactions. SET
22 SESSION CHARACTERISTICS sets the default transaction characteristics
23 for subsequent transactions of a session. These defaults can be
24 overridden by SET TRANSACTION for an individual transaction.
25
26 The available transaction characteristics are the transaction isolation
27 level, the transaction access mode (read/write or read-only), and the
28 deferrable mode. In addition, a snapshot can be selected, though only
29 for the current transaction, not as a session default.
30
31 The isolation level of a transaction determines what data the
32 transaction can see when other transactions are running concurrently:
33
34 READ COMMITTED
35 A statement can only see rows committed before it began. This is
36 the default.
37
38 REPEATABLE READ
39 All statements of the current transaction can only see rows
40 committed before the first query or data-modification statement was
41 executed in this transaction.
42
43 SERIALIZABLE
44 All statements of the current transaction can only see rows
45 committed before the first query or data-modification statement was
46 executed in this transaction. If a pattern of reads and writes
47 among concurrent serializable transactions would create a situation
48 which could not have occurred for any serial (one-at-a-time)
49 execution of those transactions, one of them will be rolled back
50 with a serialization_failure error.
51 The SQL standard defines one additional level, READ UNCOMMITTED. In
52 PostgreSQLREAD UNCOMMITTED is treated as READ COMMITTED.
53
54 The transaction isolation level cannot be changed after the first query
55 or data-modification statement (SELECT, INSERT, DELETE, UPDATE, FETCH,
56 or COPY) of a transaction has been executed. See Chapter 13,
57 Concurrency Control, in the documentation for more information about
58 transaction isolation and concurrency control.
59
60 The transaction access mode determines whether the transaction is
61 read/write or read-only. Read/write is the default. When a transaction
62 is read-only, the following SQL commands are disallowed: INSERT,
63 UPDATE, DELETE, and COPY FROM if the table they would write to is not a
64 temporary table; all CREATE, ALTER, and DROP commands; COMMENT, GRANT,
65 REVOKE, TRUNCATE; and EXPLAIN ANALYZE and EXECUTE if the command they
66 would execute is among those listed. This is a high-level notion of
67 read-only that does not prevent all writes to disk.
68
69 The DEFERRABLE transaction property has no effect unless the
70 transaction is also SERIALIZABLE and READ ONLY. When all three of these
71 properties are selected for a transaction, the transaction may block
72 when first acquiring its snapshot, after which it is able to run
73 without the normal overhead of a SERIALIZABLE transaction and without
74 any risk of contributing to or being canceled by a serialization
75 failure. This mode is well suited for long-running reports or backups.
76
77 The SET TRANSACTION SNAPSHOT command allows a new transaction to run
78 with the same snapshot as an existing transaction. The pre-existing
79 transaction must have exported its snapshot with the pg_export_snapshot
80 function (see Section 9.26.5, “Snapshot Synchronization Functions”, in
81 the documentation). That function returns a snapshot identifier, which
82 must be given to SET TRANSACTION SNAPSHOT to specify which snapshot is
83 to be imported. The identifier must be written as a string literal in
84 this command, for example '000003A1-1'. SET TRANSACTION SNAPSHOT can
85 only be executed at the start of a transaction, before the first query
86 or data-modification statement (SELECT, INSERT, DELETE, UPDATE, FETCH,
87 or COPY) of the transaction. Furthermore, the transaction must already
88 be set to SERIALIZABLE or REPEATABLE READ isolation level (otherwise,
89 the snapshot would be discarded immediately, since READ COMMITTED mode
90 takes a new snapshot for each command). If the importing transaction
91 uses SERIALIZABLE isolation level, then the transaction that exported
92 the snapshot must also use that isolation level. Also, a non-read-only
93 serializable transaction cannot import a snapshot from a read-only
94 transaction.
95
97 If SET TRANSACTION is executed without a prior START TRANSACTION or
98 BEGIN, it will appear to have no effect, since the transaction will
99 immediately end.
100
101 It is possible to dispense with SET TRANSACTION by instead specifying
102 the desired transaction_modes in BEGIN or START TRANSACTION. But that
103 option is not available for SET TRANSACTION SNAPSHOT.
104
105 The session default transaction modes can also be set by setting the
106 configuration parameters default_transaction_isolation,
107 default_transaction_read_only, and default_transaction_deferrable. (In
108 fact SET SESSION CHARACTERISTICS is just a verbose equivalent for
109 setting these variables with SET.) This means the defaults can be set
110 in the configuration file, via ALTER DATABASE, etc. Consult Chapter 18,
111 Server Configuration, in the documentation for more information.
112
114 To begin a new transaction with the same snapshot as an already
115 existing transaction, first export the snapshot from the existing
116 transaction. That will return the snapshot identifier, for example:
117
118 BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
119 SELECT pg_export_snapshot();
120 pg_export_snapshot
121 --------------------
122 000003A1-1
123 (1 row)
124
125 Then give the snapshot identifier in a SET TRANSACTION SNAPSHOT command
126 at the beginning of the newly opened transaction:
127
128 BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
129 SET TRANSACTION SNAPSHOT '000003A1-1';
130
132 These commands are defined in the SQL standard, except for the
133 DEFERRABLE transaction mode and the SET TRANSACTION SNAPSHOT form,
134 which are PostgreSQL extensions.
135
136 SERIALIZABLE is the default transaction isolation level in the
137 standard. In PostgreSQL the default is ordinarily READ COMMITTED, but
138 you can change it as mentioned above.
139
140 In the SQL standard, there is one other transaction characteristic that
141 can be set with these commands: the size of the diagnostics area. This
142 concept is specific to embedded SQL, and therefore is not implemented
143 in the PostgreSQL server.
144
145 The SQL standard requires commas between successive transaction_modes,
146 but for historical reasons PostgreSQL allows the commas to be omitted.
147
148
149
150PostgreSQL 9.2.24 2017-11-06 SET TRANSACTION(7)