2 67.3. Subtransactions #
4 Subtransactions are started inside transactions, allowing large
5 transactions to be broken into smaller units. Subtransactions can
6 commit or abort without affecting their parent transactions, allowing
7 parent transactions to continue. This allows errors to be handled more
8 easily, which is a common application development pattern. The word
9 subtransaction is often abbreviated as subxact.
11 Subtransactions can be started explicitly using the SAVEPOINT command,
12 but can also be started in other ways, such as PL/pgSQL's EXCEPTION
13 clause. PL/Python and PL/Tcl also support explicit subtransactions.
14 Subtransactions can also be started from other subtransactions. The
15 top-level transaction and its child subtransactions form a hierarchy or
16 tree, which is why we refer to the main transaction as the top-level
19 If a subtransaction is assigned a non-virtual transaction ID, its
20 transaction ID is referred to as a “subxid”. Read-only subtransactions
21 are not assigned subxids, but once they attempt to write, they will be
22 assigned one. This also causes all of a subxid's parents, up to and
23 including the top-level transaction, to be assigned non-virtual
24 transaction ids. We ensure that a parent xid is always lower than any
27 The immediate parent xid of each subxid is recorded in the pg_subtrans
28 directory. No entry is made for top-level xids since they do not have a
29 parent, nor is an entry made for read-only subtransactions.
31 When a subtransaction commits, all of its committed child
32 subtransactions with subxids will also be considered subcommitted in
33 that transaction. When a subtransaction aborts, all of its child
34 subtransactions will also be considered aborted.
36 When a top-level transaction with an xid commits, all of its
37 subcommitted child subtransactions are also persistently recorded as
38 committed in the pg_xact subdirectory. If the top-level transaction
39 aborts, all its subtransactions are also aborted, even if they were
42 The more subtransactions each transaction keeps open (not rolled back
43 or released), the greater the transaction management overhead. Up to 64
44 open subxids are cached in shared memory for each backend; after that
45 point, the storage I/O overhead increases significantly due to
46 additional lookups of subxid entries in pg_subtrans.