SET TRANSACTION 中文man頁面
NAME
SET TRANSACTION - 設置當前事務的特性
SYNOPSIS
SET TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ONLY ] SET SESSION CHARACTERISTICS AS TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ONLY ]
DESCRIPTION 描述
SET TRANSACTION 命令為當前事務設置特性。 它對后面的事務沒有影響。 SET SESSION CHARACTERISTICS 為一個會話中的每個事務設置缺省的隔離級別。 SET TRANSACTION 可以為一個獨立的事務覆蓋上面的設置。
可用的事務特性是事務隔離級別和事務訪問模式(讀/寫或者只讀)。
事務的隔離級別決定一個事務在同時存在其它并行運行的事務時它能夠看到什么數(shù)據(jù)。
- READ COMMITTED
一條語句只能看到在它開始之前的數(shù)據(jù)。這是缺省。- SERIALIZABLE
當前的事務只能看到在這次事務第一條查詢或者修改數(shù)據(jù)的語句執(zhí)行之前的數(shù)據(jù)。- Tip: 提示: 說白了,serializable(可串行化)意味著兩個事務將把數(shù)據(jù)庫保持在同一個狀態(tài), 就好象這兩個事務是嚴格地按照先后順序執(zhí)行地那樣。
事務隔離級別在事務中第一個數(shù)據(jù)修改語句 (SELECT, INSERT, DELETE, UPDATE, FETCH, COPY) 執(zhí)行之后就不能再次設置。 參閱 Chapter 12 ``Concurrency Control'' 獲取有關事務隔離級別和并發(fā)性控制的更多信息。
事務訪問模式?jīng)Q定事務是讀/寫還是只讀。讀/寫是缺省。如果一個 事務是只讀,而且寫入的表不是臨時表,那么下面的 SQL 命令是不允許的:INSERT, UPDATE,DELETE,和 COPY TO; 而所有的 CREATE,ALTER,和 DROP 命令; COMMENT,GRANT,REVOKE, TRUNCATE;和 EXPLAIN ANALYZE 和EXECUTE 都不允許。這是一個高層次的只讀概念,它并不阻止對磁盤的寫入。
NOTES 注意
會話的缺省事務隔離級別也可以用命令
SET default_transaction_isolation = 'value'
以及在配置文件里設置。 參考 Section 16.4 ``Run-time Configuration'' 獲取更多信息。
COMPATIBILITY 兼容性
兩個命令都在 SQL 標準里定義了。SQL 里的缺省事務隔離級別是 SERIALIZABLE; 在 PostgreSQL 里,缺省隔離級別是 READ COMMITED,但是你可以用上面的描述修改它。 PostgreSQL 并沒有提供隔離級別 READ UNCOMMITTED 和 REPEATABLE READ。 因為多版本并發(fā)控制,SERIALIZABLE 級別并非真正的可串行化。參閱 Chapter 12 ``Concurrency Control'' 獲取細節(jié)。
在 SQL 標準里還有另外一種事務特性可以用這些命令設置:診斷范圍的大小。這個概念只用于嵌入的 SQL。
#p#
NAME
SET TRANSACTION - set the characteristics of the current transaction
SYNOPSIS
SET TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ONLY ] SET SESSION CHARACTERISTICS AS TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ONLY ]
DESCRIPTION
The SET TRANSACTION command sets the transaction characteristics of the current transaction. It has no effect on any subsequent transactions. SET SESSION CHARACTERISTICS sets the default transaction characteristics for each transaction of a session. SET TRANSACTION can override it for an individual transaction.
The available transaction characteristics are the transaction isolation level and the transaction access mode (read/write or read-only).
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.
- SERIALIZABLE
- The current transaction can only see rows committed before first query or data-modification statement was executed in this transaction.
- Tip: Intuitively, serializable means that two concurrent transactions will leave the database in the same state as if the two has been executed strictly after one another in either order.
The transaction isolation level cannot be set after the first query or data-modification statement (SELECT, INSERT, DELETE, UPDATE, FETCH, COPY) of a transaction has been executed. See the chapter called ``Concurrency Control'' in the documentation 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, and COPY TO if the table they would write to is not a temporary table; all CREATE, ALTER, and DROP commands; COMMENT, GRANT, REVOKE, TRUNCATE; and EXPLAIN ANALYZE and EXECUTE if the command they would execute is among those listed. This is a high-level notion of read-only that does not prevent writes to disk.
NOTES
The session default transaction isolation level can also be set with the command
SET default_transaction_isolation = 'value'
and in the configuration file. Consult the section called ``Run-time Configuration'' in the documentation for more information.
COMPATIBILITY
Both commands are defined in the SQL standard. SERIALIZABLE is the default transaction isolation level in the standard; in PostgreSQL the default is ordinarily READ COMMITTED, but you can change it as described above. PostgreSQL does not provide the isolation levels READ UNCOMMITTED and REPEATABLE READ. Because of multiversion concurrency control, the SERIALIZABLE level is not truly serializable. See the chapter called ``Concurrency Control'' in the documentation for details.
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 only for use in embedded SQL.