Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/xact.sgml`
0a9d69c284a8c50a1b1430c381bb95a2e236e1cf1b4517c20000000100000b08
 addition to <literal>vxid</literal> and <literal>xid</literal>,
   prepared transactions are also assigned Global Transaction
   Identifiers (<acronym>GID</acronym>). 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 <link
   linkend="view-pg-prepared-xacts"><structname>pg_prepared_xacts</structname></link>.
  </para>
 </sect1>

 <sect1 id="xact-locking">

  <title>Transactions and Locking</title>

  <para>
   The transaction IDs of currently executing transactions are shown in
   <link linkend="view-pg-locks"><structname>pg_locks</structname></link>
   in columns <structfield>virtualxid</structfield> and
   <structfield>transactionid</structfield>.  Read-only transactions
   will have <structfield>virtualxid</structfield>s but NULL
   <structfield>transactionid</structfield>s, while both columns will be
   set in read-write transactions.
  </para>

  <para>
   Some lock types wait on <structfield>virtualxid</structfield>,
   while other types wait on <structfield>transactionid</structfield>.
   Row-level read and write locks are recorded directly in the locked
   rows and can be inspected using the <xref linkend="pgrowlocks"/>
   extension.  Row-level read locks might also require the assignment
   of multixact IDs (<literal>mxid</literal>;  see <xref
   linkend="vacuum-for-multixact-wraparound"/>).
  </para>
 </sect1>

 <sect1 id="subxacts">

  <title>Subtransactions</title>

  <para>
   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>.
  </para>

  <para>
   Subtransactions can be started explicitly using the
   <command>SAVEPOINT</command> command, but can also be started in
   other ways, such as PL/pgSQL's <literal>EXCEPTION</literal> 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.
  </para>

  <para>
   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,

Title: Transactions and Locking in PostgreSQL
Summary
This section discusses transactions and locking in PostgreSQL, including the use of global transaction identifiers, lock types, and subtransactions, which allow large transactions to be broken into smaller units and enable easier error handling.