Home Explore Blog CI



postgresql

1st chunk of `doc/src/sgml/ref/begin.sgml`
33891160e196633d622048e0fb0cd800ed338771601667ce0000000100000972
<!--
doc/src/sgml/ref/begin.sgml
PostgreSQL documentation
-->

<refentry id="sql-begin">
 <indexterm zone="sql-begin">
  <primary>BEGIN</primary>
 </indexterm>

 <refmeta>
  <refentrytitle>BEGIN</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>BEGIN</refname>
  <refpurpose>start a transaction block</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
BEGIN [ WORK | TRANSACTION ] [ <replaceable class="parameter">transaction_mode</replaceable> [, ...] ]

<phrase>where <replaceable class="parameter">transaction_mode</replaceable> is one of:</phrase>

    ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
    READ WRITE | READ ONLY
    [ NOT ] DEFERRABLE
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>BEGIN</command> initiates a transaction block, that is,
   all statements after a <command>BEGIN</command> command will be
   executed in a single transaction until an explicit <link
   linkend="sql-commit"><command>COMMIT</command></link> or <link
   linkend="sql-rollback"><command>ROLLBACK</command></link> is given.
   By default (without <command>BEGIN</command>),
   <productname>PostgreSQL</productname> executes
   transactions in <quote>autocommit</quote> mode, that is, each
   statement is executed in its own transaction and a commit is
   implicitly performed at the end of the statement (if execution was
   successful, otherwise a rollback is done).
  </para>

  <para>
   Statements are executed more quickly in a transaction block, because
   transaction start/commit requires significant CPU and disk
   activity. Execution of multiple statements inside a transaction is
   also useful to ensure consistency when making several related changes:
   other sessions will be unable to see the intermediate states
   wherein not all the related updates have been done.
  </para>

  <para>
   If the isolation level, read/write mode, or deferrable mode is specified, the new
   transaction has those characteristics, as if
   <link linkend="sql-set-transaction"><command>SET TRANSACTION</command></link>
   was executed.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><literal>WORK</literal></term>
    <term><literal>TRANSACTION</literal></term>
    <listitem>

Title: BEGIN: Start a Transaction Block
Summary
The SQL command `BEGIN` initiates a transaction block, grouping subsequent statements into a single transaction that requires an explicit `COMMIT` or `ROLLBACK`. PostgreSQL defaults to autocommit mode, where each statement is its own transaction. Using transaction blocks improves performance and ensures data consistency by preventing other sessions from seeing intermediate changes. The command also allows for specifying transaction characteristics like isolation level, read/write mode, and deferrability, similar to using `SET TRANSACTION`.