aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/ref/begin.sgml
blob: 15c1dbdf070068e754bf5236f97137751ca1e33e (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/begin.sgml,v 1.15 2001/12/08 03:24:34 thomas 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>
 <refsynopsisdiv>
  <refsynopsisdivinfo>
   <date>1999-07-20</date>
  </refsynopsisdivinfo>
  <synopsis>  
BEGIN [ WORK | TRANSACTION ]
  </synopsis>
  
  <refsect2 id="R2-SQL-BEGIN-1">
   <refsect2info>
    <date>1999-06-11</date>
   </refsect2info>
   <title>
    Inputs
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term>WORK</term>
      <term>TRANSACTION</term>
      <listitem>
       <para>
	Optional keywords. They have no effect.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
  
  <refsect2 id="R2-SQL-BEGIN-2">
   <refsect2info>
    <date>1999-06-11</date>
   </refsect2info>
   <title>
    Outputs
   </title>

   <para>
    <variablelist>
     <varlistentry>
      <term><computeroutput>
BEGIN
       </computeroutput></term>
      <listitem>
       <para>
	This signifies that a new transaction has been started.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><computeroutput>
NOTICE:  BEGIN: already a transaction in progress
       </computeroutput></term>
      <listitem>
       <para>
	This indicates that a transaction was already in progress.
	The current transaction is not affected.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
 </refsynopsisdiv>
 
 <refsect1 id="R1-SQL-BEGIN-1">
  <refsect1info>
   <date>1999-06-11</date>
  </refsect1info>
  <title>
   Description
  </title>

  <para>
   By default, <productname>PostgreSQL</productname> executes transactions
   in <firstterm>unchained mode</firstterm>
   (also known as <quote>autocommit</quote> in other database
   systems).
   In other words, each user 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).
   <command>BEGIN</command> initiates a user transaction in chained mode,
   i.e., all user statements after <command>BEGIN</command> command will
   be executed in a single transaction until an explicit 
   <xref linkend="sql-commit" endterm="sql-commit-title">,
   <xref linkend="sql-rollback" endterm="sql-rollback-title">,
   or execution abort. Statements in chained mode are executed much faster, 
   because transaction start/commit requires significant CPU and disk 
   activity. Execution of multiple statements inside a transaction
   is also required for consistency when changing several
   related tables.
  </para>

  <para>
   The default transaction isolation level in
   <productname>PostgreSQL</productname>
   is READ COMMITTED, where queries inside the transaction see only changes
   committed before query execution. So, you have to use 
   <command>SET TRANSACTION ISOLATION LEVEL SERIALIZABLE</command>
   just after <command>BEGIN</command> if you need more rigorous transaction isolation. 
   In SERIALIZABLE mode queries will see only changes committed before
   the entire
   transaction began (actually, before execution of the first DML statement
   in a serializable transaction).
  </para>

  <para>
   If the transaction is committed, <productname>PostgreSQL</productname> 
   will ensure either that all updates are done or else that none of
   them are done. Transactions have the standard <acronym>ACID</acronym>
   (atomic, consistent, isolatable, and durable) property.
  </para>
  
  <refsect2 id="R2-SQL-BEGIN-3">
   <refsect2info>
    <date>1999-06-11</date>
   </refsect2info>
   <title>
    Notes
   </title>
   <para>
    Refer to <xref linkend="sql-lock" endterm="sql-lock-title">
    for further information
    about locking tables inside a transaction.
   </para>
   
   <para>
    Use <xref linkend="SQL-COMMIT" endterm="SQL-COMMIT-TITLE">
    or
    <xref linkend="SQL-ROLLBACK" endterm="SQL-ROLLBACK-TITLE">
    to terminate a transaction.
   </para>
  </refsect2>
 </refsect1>

 <refsect1 id="R1-SQL-BEGIN-2">
  <title>
   Usage
  </title>

  <para>
   To begin a user transaction:

  <programlisting>
BEGIN WORK;
  </programlisting>
  </para>
 </refsect1>
 
 <refsect1 id="R1-SQL-BEGIN-3">
  <title>
   Compatibility
  </title>

  <refsect2 id="R2-SQL-BEGIN-4">
     <refsect2info>
    <date>1999-06-11</date>
   </refsect2info>
   <title>
    SQL92
   </title>

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

    <note>
     <para>
      Many relational database systems offer an autocommit feature as a
      convenience.
     </para>
    </note>
   </para>

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

   <para>
    <acronym>SQL92</acronym> also requires SERIALIZABLE to be the default
    transaction isolation level.
   </para>
  </refsect2>
 </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:
-->