3 PostgreSQL documentation
6 <refentry id=
"SQL-BEGIN">
8 <refentrytitle id=
"SQL-BEGIN-TITLE">BEGIN
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>BEGIN
</refname>
15 <refpurpose>start a transaction block
</refpurpose>
18 <indexterm zone=
"sql-begin">
19 <primary>BEGIN
</primary>
24 BEGIN [ WORK | TRANSACTION ] [
<replaceable class=
"parameter">transaction_mode
</replaceable> [, ...] ]
26 where
<replaceable class=
"parameter">transaction_mode
</replaceable> is one of:
28 ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
29 READ WRITE | READ ONLY
34 <title>Description
</title>
37 <command>BEGIN
</command> initiates a transaction block, that is,
38 all statements after a
<command>BEGIN
</command> command will be
39 executed in a single transaction until an explicit
<xref
40 linkend=
"sql-commit" endterm=
"sql-commit-title"> or
<xref
41 linkend=
"sql-rollback" endterm=
"sql-rollback-title"> is given.
42 By default (without
<command>BEGIN
</command>),
43 <productname>PostgreSQL
</productname> executes
44 transactions in
<quote>autocommit
</quote> mode, that is, each
45 statement is executed in its own transaction and a commit is
46 implicitly performed at the end of the statement (if execution was
47 successful, otherwise a rollback is done).
51 Statements are executed more quickly in a transaction block, because
52 transaction start/commit requires significant CPU and disk
53 activity. Execution of multiple statements inside a transaction is
54 also useful to ensure consistency when making several related changes:
55 other sessions will be unable to see the intermediate states
56 wherein not all the related updates have been done.
60 If the isolation level or read/write mode is specified, the new
61 transaction has those characteristics, as if
62 <xref linkend=
"sql-set-transaction" endterm=
"sql-set-transaction-title">
68 <title>Parameters
</title>
72 <term><literal>WORK
</literal></term>
73 <term><literal>TRANSACTION
</literal></term>
76 Optional key words. They have no effect.
83 Refer to
<xref linkend=
"sql-set-transaction"
84 endterm=
"sql-set-transaction-title"> for information on the meaning
85 of the other parameters to this statement.
93 <xref linkend=
"sql-start-transaction"
94 endterm=
"sql-start-transaction-title"> has the same functionality
99 Use
<xref linkend=
"SQL-COMMIT" endterm=
"SQL-COMMIT-TITLE"> or
100 <xref linkend=
"SQL-ROLLBACK" endterm=
"SQL-ROLLBACK-TITLE">
101 to terminate a transaction block.
105 Issuing
<command>BEGIN<
/> when already inside a transaction block will
106 provoke a warning message. The state of the transaction is not affected.
107 To nest transactions within a transaction block, use savepoints
108 (see
<xref linkend=
"sql-savepoint" endterm=
"sql-savepoint-title">).
112 For reasons of backwards compatibility, the commas between successive
113 <replaceable class=
"parameter">transaction_modes
</replaceable> can be
119 <title>Examples
</title>
122 To begin a transaction block:
131 <title>Compatibility
</title>
134 <command>BEGIN
</command> is a
<productname>PostgreSQL
</productname>
135 language extension. It is equivalent to the SQL-standard command
136 <xref linkend=
"sql-start-transaction"
137 endterm=
"sql-start-transaction-title">, whose reference page
138 contains additional compatibility information.
142 Incidentally, the
<literal>BEGIN
</literal> key word is used for a
143 different purpose in embedded SQL. You are advised to be careful
144 about the transaction semantics when porting database applications.
149 <title>See Also
</title>
151 <simplelist type=
"inline">
152 <member><xref linkend=
"sql-commit" endterm=
"sql-commit-title"></member>
153 <member><xref linkend=
"sql-rollback" endterm=
"sql-rollback-title"></member>
154 <member><xref linkend=
"sql-start-transaction" endterm=
"sql-start-transaction-title"></member>
155 <member><xref linkend=
"sql-savepoint" endterm=
"sql-savepoint-title"></member>