aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/ref/begin.sgml
blob: 5acab231b3c2847264b9485e27fc0449f1a0d19d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/begin.sgml,v 1.26 2003/09/09 18:28:52 tgl Exp $
PostgreSQL documentation
-->

<refentry id="SQL-BEGIN">
 <refmeta>
  <refentrytitle id="SQL-BEGIN-TITLE">BEGIN</refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

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

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

 <refsynopsisdiv>
<synopsis>
BEGIN [ WORK | TRANSACTION ]
</synopsis>
 </refsynopsisdiv>
 
 <refsect1>
  <title>Description</title>

  <para>
   <command>BEGIN</command> initiates a transaction block, that is,
   all statements after <command>BEGIN</command> command will be
   executed in a single transaction until an explicit <xref
   linkend="sql-commit" endterm="sql-commit-title"> or <xref
   linkend="sql-rollback" endterm="sql-rollback-title"> 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>
 </refsect1>
  
 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><literal>WORK</literal></term>
    <term><literal>TRANSACTION</literal></term>
    <listitem>
     <para>
      Optional key words. They have no effect.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>
  
 <refsect1>
  <title>Notes</title>

  <para>
   <xref linkend="sql-start-transaction"
   endterm="sql-start-transaction-title"> has the same functionality
   as <command>BEGIN</>.
  </para>
   
  <para>
   Use <xref linkend="SQL-COMMIT" endterm="SQL-COMMIT-TITLE"> or
   <xref linkend="SQL-ROLLBACK" endterm="SQL-ROLLBACK-TITLE">
   to terminate a transaction block.
  </para>

  <para>
   Issuing <command>BEGIN</> when already inside a transaction block will
   provoke a warning message.  The state of the transaction is not affected.
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   To begin a transaction block:

<programlisting>
BEGIN;
</programlisting>
  </para>
 </refsect1>
 
 <refsect1>
  <title>Compatibility</title>

  <para>
   <command>BEGIN</command> is a <productname>PostgreSQL</productname>
   language extension.  There is no explicit <command>BEGIN</command>
   command in the SQL standard; transaction initiation is
   always implicit and it terminates either with a
   <command>COMMIT</command> or <command>ROLLBACK</command> statement.
  </para>

  <para>
   Other relational database systems may offer an autocommit feature
   as a convenience.
  </para>

  <para>
   Incidentally, the <literal>BEGIN</literal> key word is used for a
   different purpose in embedded SQL. You are advised to be careful
   about the transaction semantics when porting database applications.
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-commit" endterm="sql-commit-title"></member>
   <member><xref linkend="sql-rollback" endterm="sql-rollback-title"></member>
  </simplelist>
 </refsect1>
</refentry>
 
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->