1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>67.3. Subtransactions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="xact-locking.html" title="67.2. Transactions and Locking" /><link rel="next" href="two-phase.html" title="67.4. Two-Phase Transactions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">67.3. Subtransactions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="xact-locking.html" title="67.2. Transactions and Locking">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="transactions.html" title="Chapter 67. Transaction Processing">Up</a></td><th width="60%" align="center">Chapter 67. Transaction Processing</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="two-phase.html" title="67.4. Two-Phase Transactions">Next</a></td></tr></table><hr /></div><div class="sect1" id="SUBXACTS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">67.3. Subtransactions <a href="#SUBXACTS" class="id_link">#</a></h2></div></div></div><p>
3 Subtransactions are started inside transactions, allowing large
4 transactions to be broken into smaller units. Subtransactions can
5 commit or abort without affecting their parent transactions, allowing
6 parent transactions to continue. This allows errors to be handled
7 more easily, which is a common application development pattern.
8 The word subtransaction is often abbreviated as
9 <em class="firstterm">subxact</em>.
11 Subtransactions can be started explicitly using the
12 <code class="command">SAVEPOINT</code> command, but can also be started in
13 other ways, such as PL/pgSQL's <code class="literal">EXCEPTION</code> clause.
14 PL/Python and PL/Tcl also support explicit subtransactions.
15 Subtransactions can also be started from other subtransactions.
16 The top-level transaction and its child subtransactions form a
17 hierarchy or tree, which is why we refer to the main transaction as
18 the top-level transaction.
20 If a subtransaction is assigned a non-virtual transaction ID,
21 its transaction ID is referred to as a <span class="quote">“<span class="quote">subxid</span>”</span>.
22 Read-only subtransactions are not assigned subxids, but once they
23 attempt to write, they will be assigned one. This also causes all of
24 a subxid's parents, up to and including the top-level transaction,
25 to be assigned non-virtual transaction ids. We ensure that a parent
26 xid is always lower than any of its child subxids.
28 The immediate parent xid of each subxid is recorded in the
29 <code class="filename">pg_subtrans</code> directory. No entry is made for
30 top-level xids since they do not have a parent, nor is an entry made
31 for read-only subtransactions.
33 When a subtransaction commits, all of its committed child
34 subtransactions with subxids will also be considered subcommitted
35 in that transaction. When a subtransaction aborts, all of its child
36 subtransactions will also be considered aborted.
38 When a top-level transaction with an xid commits, all of its
39 subcommitted child subtransactions are also persistently recorded
40 as committed in the <code class="filename">pg_xact</code> subdirectory. If the
41 top-level transaction aborts, all its subtransactions are also aborted,
42 even if they were subcommitted.
44 The more subtransactions each transaction keeps open (not
45 rolled back or released), the greater the transaction management
46 overhead. Up to 64 open subxids are cached in shared memory for
47 each backend; after that point, the storage I/O overhead increases
48 significantly due to additional lookups of subxid entries in
49 <code class="filename">pg_subtrans</code>.
50 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="xact-locking.html" title="67.2. Transactions and Locking">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="transactions.html" title="Chapter 67. Transaction Processing">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="two-phase.html" title="67.4. Two-Phase Transactions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">67.2. Transactions and Locking </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 67.4. Two-Phase Transactions</td></tr></table></div></body></html>