Postgresql 中文操作指南

74.3. Subtransactions #

子事务在事务中启动,允许将大型事务分解成更小的单元。子事务可以提交或中止而不影响其父事务,从而允许父事务继续。这样可以更轻松地处理错误,这是常见应用程序开发模式。子事务一词通常缩写为 subxact

Subtransactions are started inside transactions, allowing large transactions to be broken into smaller units. Subtransactions can commit or abort without affecting their parent transactions, allowing parent transactions to continue. This allows errors to be handled more easily, which is a common application development pattern. The word subtransaction is often abbreviated as subxact.

可以使用 SAVEPOINT 命令显式启动子事务,但也可以通过其他方式启动,例如 PL/pgSQL 的 EXCEPTION 子句。PL/Python 和 PL/Tcl 也支持显式子事务。还可以从其他子事务启动子事务。顶级事务及其子事务形成一个层次结构或树,这就是我们称主要事务为顶级事务的原因。

Subtransactions can be started explicitly using the SAVEPOINT command, but can also be started in other ways, such as PL/pgSQL’s EXCEPTION clause. PL/Python and PL/Tcl also support explicit subtransactions. Subtransactions can also be started from other subtransactions. The top-level transaction and its child subtransactions form a hierarchy or tree, which is why we refer to the main transaction as the top-level transaction.

如果向子事务分配了非虚拟事务 ID,则其事务 ID 被称为“subxid”。只读子事务不分配 subxid,但一旦它们尝试写入,就会分配一个 subxid。这也将导致 subxid 的所有父级(包括顶级事务)被分配非虚拟事务 id。我们确保父 xid 始终低于其任何子 subxid。

If a subtransaction is assigned a non-virtual transaction ID, its transaction ID is referred to as a “subxid”. Read-only subtransactions are not assigned subxids, but once they attempt to write, they will be assigned one. This also causes all of a subxid’s parents, up to and including the top-level transaction, to be assigned non-virtual transaction ids. We ensure that a parent xid is always lower than any of its child subxids.

每个 subxid 的直接父 xid 都记录在 pg_subtrans 目录中。不会为顶级 xid 创建条目,因为它们没有父级,也不会为只读子事务创建条目。

The immediate parent xid of each subxid is recorded in the pg_subtrans directory. No entry is made for top-level xids since they do not have a parent, nor is an entry made for read-only subtransactions.

当子事务提交时,其所有已提交的具有 subxid 的子子事务也将被认为已在此事务中提交。当一个子事务中止时,其所有子事务也将被视为已中止。

When a subtransaction commits, all of its committed child subtransactions with subxids will also be considered subcommitted in that transaction. When a subtransaction aborts, all of its child subtransactions will also be considered aborted.

具有 xid 的顶级事务提交时,其所有已提交的子子事务也会在 pg_xact 子目录中被永久记录为已提交。如果顶级事务中止,则其所有子事务也会中止,即使它们已提交。

When a top-level transaction with an xid commits, all of its subcommitted child subtransactions are also persistently recorded as committed in the pg_xact subdirectory. If the top-level transaction aborts, all its subtransactions are also aborted, even if they were subcommitted.

每个事务保持打开状态(未回滚或释放)的子事务越多,事务管理开销就越大。每个后端最多可以缓存 64 个开放的 subxid;在超出该点后,由于在 pg_subtrans 中额外查找 subxid 条目,存储 I/O 开销将显著增加。

The more subtransactions each transaction keeps open (not rolled back or released), the greater the transaction management overhead. Up to 64 open subxids are cached in shared memory for each backend; after that point, the storage I/O overhead increases significantly due to additional lookups of subxid entries in pg_subtrans.