Postgresql 中文操作指南
74.1. Transactions and Identifiers #
可以使用 BEGIN 或 START TRANSACTION 显式创建事务,并可以使用 COMMIT 或 ROLLBACK 结束事务。位于显式事务之外的 SQL 语句将自动使用单语句事务。
Transactions can be created explicitly using BEGIN or START TRANSACTION and ended using COMMIT or ROLLBACK. SQL statements outside of explicit transactions automatically use single-statement transactions.
每个事务都由唯一的 VirtualTransactionId(也称为 virtualXID 或 vxid)标识,该标识由后端 ID(或 backendID)和每个后端本地顺序分配的数字组成,称为 localXID。例如,虚拟事务 ID 4/12532 具有 backendID 为 4,localXID 为 12532。
Every transaction is identified by a unique VirtualTransactionId (also called virtualXID or vxid), which is comprised of a backend ID (or backendID) and a sequentially-assigned number local to each backend, known as localXID. For example, the virtual transaction ID 4/12532 has a backendID of 4 and a localXID of 12532.
非虚拟 TransactionId_s (or _xid),例如 278394,从 PostgreSQL 集群中所有数据库使用的全局计数器中顺序分配给事务。此分配发生在事务首次写入数据库时。这意味着编号较低的事务 ID 在编号较高的事务 ID 之前开始写入。请注意,事务执行其首次数据库写入的顺序可能与事务开始的顺序不同,特别是如果事务以仅执行数据库读取的语句开始。
Non-virtual TransactionId_s (or _xid), e.g., 278394, are assigned sequentially to transactions from a global counter used by all databases within the PostgreSQL cluster. This assignment happens when a transaction first writes to the database. This means lower-numbered xids started writing before higher-numbered xids. Note that the order in which transactions perform their first database write might be different from the order in which the transactions started, particularly if the transaction started with statements that only performed database reads.
内部事务 ID 类型 xid_为 32 位宽,每 40 亿个事物出现 wraps around。在每次折返期间增量一个 32 位纪元。还有一个 64 位类型 _xid8,其中包含此纪元,因此在安装的生命周期内不进行折返;可以通过强制转换将其转换为 xid。 Table 9.80 中的函数返回 xid8 值。Xid 被用作 PostgreSQL 的 MVCC 并发机制和流复制的基础。
The internal transaction ID type xid is 32 bits wide and wraps around every 4 billion transactions. A 32-bit epoch is incremented during each wraparound. There is also a 64-bit type xid8 which includes this epoch and therefore does not wrap around during the life of an installation; it can be converted to xid by casting. The functions in Table 9.80 return xid8 values. Xids are used as the basis for PostgreSQL’s MVCC concurrency mechanism and streaming replication.
当具有(非虚拟)xid 的顶级事务提交时,它被标记为在 pg_xact 目录中提交。如果启用了 track_commit_timestamp,将会在 pg_commit_ts 目录中记录附加信息。
When a top-level transaction with a (non-virtual) xid commits, it is marked as committed in the pg_xact directory. Additional information is recorded in the pg_commit_ts directory if track_commit_timestamp is enabled.
除了 vxid 和 xid 外,已准备好的事务还会被分配全球事务标识符 (GID)。GID 是长达 200 个字节的字符串文本,在其他当前准备好的事务中必须唯一。GID 到 xid 的映射在 pg_prepared_xacts 中显示。
In addition to vxid and xid, prepared transactions are also assigned Global Transaction Identifiers (GID). GIDs are string literals up to 200 bytes long, which must be unique amongst other currently prepared transactions. The mapping of GID to xid is shown in pg_prepared_xacts.