バージョンごとのドキュメント一覧

74.3. サブトランザクション #

<title>Subtransactions</title>

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 <firstterm>subxact</firstterm>. サブトランザクションはトランザクション内で開始され、大きなトランザクションを小さな単位に分割できます。 サブトランザクションは、親トランザクションに影響を与えることなくコミットまたは中断できるため、親トランザクションを継続できます。 これにより、アプリケーションの開発パターンによくあるエラーの処理が容易になります。 この用語は、しばしばsubxactと省略されます。

Subtransactions can be started explicitly using the <command>SAVEPOINT</command> command, but can also be started in other ways, such as PL/pgSQL's <command>EXCEPTION</command> 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. サブトランザクションはSAVEPOINTコマンドを使用して明示的に開始できますが、PL/pgSQLのEXCEPTION句などの他の方法でも開始できます。 PL/PythonとPL/TCLも明示的なサブトランザクションをサポートしています。 サブトランザクションは他のサブトランザクションから開始することもできます。 トップレベルのトランザクションとその子トランザクションは階層またはツリーを形成するため、メイントランザクションをトップレベルのトランザクションと呼びます。

If a subtransaction is assigned a non-virtual transaction ID, its transaction ID is referred to as a <quote>subxid</quote>. 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. サブトランザクションに仮想トランザクションID以外のIDが割り当てられた場合、そのトランザクションIDはsubxidと呼ばれます。 読取専用のサブトランザクションにはsubxidは割り当てられませんが、書込みを試みると割り当てられます。 これにより、トップレベルのトランザクションを含むすべての親サブトランザクションに、非仮想トランザクションIDが割り当てられます。 親xidは常に子subxidのいずれよりも小さいことが保証されます。

The immediate parent xid of each subxid is recorded in the <filename>pg_subtrans</filename> 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の直接の親xidはpg_subtransディレクトリに記録されます。 トップレベルのxidには親がないのでエントリは記録されません。 読み取り専用のサブトランザクションにもエントリは記録されません。

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がコミットされたものとみなされます。 サブトランザクションが中断された場合、その子サブトランザクションすべても中断されたものとみなされます。

When a top-level transaction with an xid commits, all of its subcommitted child subtransactions are also persistently recorded as committed in the <filename>pg_xact</filename> directory. If the top-level transaction aborts, all its subtransactions are also aborted, even if they were subcommitted. xidを持つトップレベルトランザクションがコミットすると、そのサブトランザクションの子サブトランザクションもすべてpg_xactディレクトリに永続的にコミット済みとして記録されます。 トップレベルトランザクションが中断された場合、そのサブトランザクションは、たとえサブトランザクションがコミットされていたとしても中断されます。

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 <filename>pg_subtrans</filename>. トランザクションがオープンしたサブトランザクション数が多いほど(ロールバックや解放されない)、トランザクション制御のオーバーヘッドが大きくなります。 各バックエンドで最大64個のオープンしたサブトランザクションが共有メモリにキャッシュされます。 その後、pg_subtrans内のサブxidエントリの追加検索により、ストレージI/Oオーバーヘッドが大幅に増加します。