Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
SET TRANSACTION
Prev UpI. SQL CommandsHome Next

SET TRANSACTION

SET TRANSACTION — set the characteristics of the current transaction

Synopsis

SET TRANSACTIONtransaction_mode [, ...]SET TRANSACTION SNAPSHOTsnapshot_idSET SESSION CHARACTERISTICS AS TRANSACTIONtransaction_mode [, ...]wheretransaction_mode is one of:    ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }    READ WRITE | READ ONLY    [ NOT ] DEFERRABLE

Description

TheSET TRANSACTION command sets the characteristics of the current transaction. It has no effect on any subsequent transactions.SET SESSION CHARACTERISTICS sets the default transaction characteristics for subsequent transactions of a session. These defaults can be overridden bySET TRANSACTION for an individual transaction.

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.

The isolation level of a transaction determines what data the transaction can see when other transactions are running concurrently:

READ COMMITTED

A statement can only see rows committed before it began. This is the default.

REPEATABLE READ

All statements of the current transaction can only see rows committed before the first query or data-modification statement was executed in this transaction.

SERIALIZABLE

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 aserialization_failure error.

The SQL standard defines one additional level,READ UNCOMMITTED. InPostgres ProREAD UNCOMMITTED is treated asREAD COMMITTED.

The transaction isolation level cannot be changed after the first query or data-modification statement (SELECT,INSERT,DELETE,UPDATE,FETCH, orCOPY) of a transaction has been executed. SeeChapter 13 for more information about transaction isolation and concurrency control.

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:INSERT,UPDATE,DELETE, andCOPY FROM if the table they would write to is not a temporary table; allCREATE,ALTER, andDROP commands;COMMENT,GRANT,REVOKE,TRUNCATE; andEXPLAIN ANALYZE andEXECUTE 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.

TheDEFERRABLE transaction property has no effect unless the transaction is alsoSERIALIZABLE andREAD 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 aSERIALIZABLE 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.

TheSET TRANSACTION SNAPSHOT command allows a new transaction to run with the samesnapshot as an existing transaction. The pre-existing transaction must have exported its snapshot with thepg_export_snapshot function (seeSection 9.26.5). That function returns a snapshot identifier, which must be given toSET TRANSACTION SNAPSHOT to specify which snapshot is to be imported. The identifier must be written as a string literal in this command, for example'000003A1-1'.SET TRANSACTION SNAPSHOT can only be executed at the start of a transaction, before the first query or data-modification statement (SELECT,INSERT,DELETE,UPDATE,FETCH, orCOPY) of the transaction. Furthermore, the transaction must already be set toSERIALIZABLE orREPEATABLE READ isolation level (otherwise, the snapshot would be discarded immediately, sinceREAD COMMITTED mode takes a new snapshot for each command). If the importing transaction usesSERIALIZABLE 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.

Notes

IfSET TRANSACTION is executed without a priorSTART TRANSACTION orBEGIN, it emits a warning and otherwise has no effect.

It is possible to dispense withSET TRANSACTION by instead specifying the desiredtransaction_modes inBEGIN orSTART TRANSACTION. But that option is not available forSET TRANSACTION SNAPSHOT.

The session default transaction modes can also be set or examined via the configuration parametersdefault_transaction_isolation,default_transaction_read_only, anddefault_transaction_deferrable. (In factSET SESSION CHARACTERISTICS is just a verbose equivalent for setting these variables withSET.) This means the defaults can be set in the configuration file, viaALTER DATABASE, etc. ConsultChapter 18 for more information.

The current transaction's modes can similarly be set or examined via the configuration parameterstransaction_isolation,transaction_read_only, andtransaction_deferrable. Setting one of these parameters acts the same as the correspondingSET TRANSACTION 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.

Examples

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:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;SELECT pg_export_snapshot(); pg_export_snapshot-------------------- 000003A1-1(1 row)

Then give the snapshot identifier in aSET TRANSACTION SNAPSHOT command at the beginning of the newly opened transaction:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;SET TRANSACTION SNAPSHOT '000003A1-1';

Prev Home Next
SET SESSION AUTHORIZATION Up SHOW
pdfepub
Go to Postgres Pro Standard 9.6
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp