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