1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2007-05-15 21:43:51 +02:00
|
|
|
$PostgreSQL: pgsql/doc/src/sgml/ref/begin.sgml,v 1.36 2007/05/15 19:43:51 neilc Exp $
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
<refentry id="SQL-BEGIN">
|
1999-06-14 09:37:05 +02:00
|
|
|
<refmeta>
|
2002-04-21 21:02:39 +02:00
|
|
|
<refentrytitle id="SQL-BEGIN-TITLE">BEGIN</refentrytitle>
|
1999-06-14 09:37:05 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-04-15 15:25:08 +02:00
|
|
|
|
1999-06-14 09:37:05 +02:00
|
|
|
<refnamediv>
|
2003-04-15 15:25:08 +02:00
|
|
|
<refname>BEGIN</refname>
|
|
|
|
<refpurpose>start a transaction block</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
2003-08-31 19:32:24 +02:00
|
|
|
<indexterm zone="sql-begin">
|
|
|
|
<primary>BEGIN</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
1999-06-14 09:37:05 +02:00
|
|
|
<refsynopsisdiv>
|
2003-04-15 15:25:08 +02:00
|
|
|
<synopsis>
|
2004-08-12 23:00:34 +02:00
|
|
|
BEGIN [ WORK | TRANSACTION ] [ <replaceable class="parameter">transaction_mode</replaceable> [, ...] ]
|
|
|
|
|
|
|
|
where <replaceable class="parameter">transaction_mode</replaceable> is one of:
|
|
|
|
|
|
|
|
ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
|
|
|
|
READ WRITE | READ ONLY
|
2003-04-15 15:25:08 +02:00
|
|
|
</synopsis>
|
1999-06-14 09:37:05 +02:00
|
|
|
</refsynopsisdiv>
|
1998-05-13 07:34:00 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
1999-06-14 09:37:05 +02:00
|
|
|
|
1998-05-13 07:34:00 +02:00
|
|
|
<para>
|
2003-04-15 15:25:08 +02:00
|
|
|
<command>BEGIN</command> initiates a transaction block, that is,
|
2004-08-01 19:32:22 +02:00
|
|
|
all statements after a <command>BEGIN</command> command will be
|
2003-04-15 15:25:08 +02:00
|
|
|
executed in a single transaction until an explicit <xref
|
|
|
|
linkend="sql-commit" endterm="sql-commit-title"> or <xref
|
2003-09-09 20:28:53 +02:00
|
|
|
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
|
2003-04-15 15:25:08 +02:00
|
|
|
transaction start/commit requires significant CPU and disk
|
|
|
|
activity. Execution of multiple statements inside a transaction is
|
2003-09-09 20:28:53 +02:00
|
|
|
also useful to ensure consistency when making several related changes:
|
|
|
|
other sessions will be unable to see the intermediate states
|
2003-04-15 15:25:08 +02:00
|
|
|
wherein not all the related updates have been done.
|
1999-06-11 07:19:53 +02:00
|
|
|
</para>
|
2004-01-10 03:21:08 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
If the isolation level or read/write mode is specified, the new
|
|
|
|
transaction has those characteristics, as if
|
|
|
|
<xref linkend="sql-set-transaction" endterm="sql-set-transaction-title">
|
|
|
|
was executed.
|
|
|
|
</para>
|
2003-04-15 15:25:08 +02:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
1999-06-14 09:37:05 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<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>
|
2004-01-10 03:21:08 +01:00
|
|
|
|
|
|
|
<para>
|
2004-01-11 06:46:58 +01:00
|
|
|
Refer to <xref linkend="sql-set-transaction"
|
|
|
|
endterm="sql-set-transaction-title"> for information on the meaning
|
|
|
|
of the other parameters to this statement.
|
2004-01-10 03:21:08 +01:00
|
|
|
</para>
|
2003-04-15 15:25:08 +02:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
1999-06-14 09:37:05 +02:00
|
|
|
|
1999-06-11 07:19:53 +02:00
|
|
|
<para>
|
2003-04-15 15:25:08 +02:00
|
|
|
<xref linkend="sql-start-transaction"
|
|
|
|
endterm="sql-start-transaction-title"> has the same functionality
|
|
|
|
as <command>BEGIN</>.
|
1998-05-13 07:34:00 +02:00
|
|
|
</para>
|
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<para>
|
|
|
|
Use <xref linkend="SQL-COMMIT" endterm="SQL-COMMIT-TITLE"> or
|
|
|
|
<xref linkend="SQL-ROLLBACK" endterm="SQL-ROLLBACK-TITLE">
|
2003-09-09 20:28:53 +02:00
|
|
|
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.
|
2004-08-08 03:48:31 +02:00
|
|
|
To nest transactions within a transaction block, use savepoints
|
2004-08-12 23:00:34 +02:00
|
|
|
(see <xref linkend="sql-savepoint" endterm="sql-savepoint-title">).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For reasons of backwards compatibility, the commas between successive
|
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
|
|
|
<replaceable class="parameter">transaction_modes</replaceable> can be
|
2004-08-12 23:00:34 +02:00
|
|
|
omitted.
|
2003-04-15 15:25:08 +02:00
|
|
|
</para>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refsect1>
|
1999-06-14 09:37:05 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
1998-09-16 16:43:12 +02:00
|
|
|
|
1999-06-14 09:37:05 +02:00
|
|
|
<para>
|
2003-04-15 15:25:08 +02:00
|
|
|
To begin a transaction block:
|
1999-06-14 09:37:05 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<programlisting>
|
|
|
|
BEGIN;
|
|
|
|
</programlisting>
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1999-06-14 09:37:05 +02:00
|
|
|
</refsect1>
|
1998-05-13 07:34:00 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
1999-07-22 17:09:15 +02:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<para>
|
|
|
|
<command>BEGIN</command> is a <productname>PostgreSQL</productname>
|
2004-08-12 23:00:34 +02:00
|
|
|
language extension. It is equivalent to the SQL-standard command
|
|
|
|
<xref linkend="sql-start-transaction"
|
2007-05-15 21:43:51 +02:00
|
|
|
endterm="sql-start-transaction-title">, whose reference page
|
|
|
|
contains additional compatibility information.
|
2003-04-15 15:25:08 +02:00
|
|
|
</para>
|
2000-01-29 17:58:54 +01:00
|
|
|
|
2003-04-15 15:25:08 +02:00
|
|
|
<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>
|
1998-05-13 07:34:00 +02:00
|
|
|
</refsect1>
|
2003-09-09 20:28:53 +02:00
|
|
|
|
|
|
|
<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>
|
2004-01-11 10:24:17 +01:00
|
|
|
<member><xref linkend="sql-start-transaction" endterm="sql-start-transaction-title"></member>
|
2004-08-01 19:32:22 +02:00
|
|
|
<member><xref linkend="sql-savepoint" endterm="sql-savepoint-title"></member>
|
2003-09-09 20:28:53 +02:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
1999-06-14 09:37:05 +02:00
|
|
|
</refentry>
|