|
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>BEGIN</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-analyze.html" title="ANALYZE" /><link rel="next" href="sql-call.html" title="CALL" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">BEGIN</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-analyze.html" title="ANALYZE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-call.html" title="CALL">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-BEGIN"><div class="titlepage"></div><a id="id-1.9.3.47.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">BEGIN</span></h2><p>BEGIN — start a transaction block</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
- BEGIN [ WORK | TRANSACTION ] [ <em class="replaceable"><code>transaction_mode</code></em> [, ...] ]
-
- <span class="phrase">where <em class="replaceable"><code>transaction_mode</code></em> is one of:</span>
-
- ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
- READ WRITE | READ ONLY
- [ NOT ] DEFERRABLE
- </pre></div><div class="refsect1" id="id-1.9.3.47.5"><h2>Description</h2><p>
- <code class="command">BEGIN</code> initiates a transaction block, that is,
- all statements after a <code class="command">BEGIN</code> command will be
- executed in a single transaction until an explicit <a class="xref" href="sql-commit.html" title="COMMIT"><span class="refentrytitle">COMMIT</span></a> or <a class="xref" href="sql-rollback.html" title="ROLLBACK"><span class="refentrytitle">ROLLBACK</span></a> is given.
- By default (without <code class="command">BEGIN</code>),
- <span class="productname">PostgreSQL</span> executes
- transactions in <span class="quote">“<span class="quote">autocommit</span>”</span> 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).
- </p><p>
- 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.
- </p><p>
- If the isolation level, read/write mode, or deferrable mode is specified, the new
- transaction has those characteristics, as if
- <a class="xref" href="sql-set-transaction.html" title="SET TRANSACTION"><span class="refentrytitle">SET TRANSACTION</span></a>
- was executed.
- </p></div><div class="refsect1" id="id-1.9.3.47.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">WORK</code><br /></span><span class="term"><code class="literal">TRANSACTION</code></span></dt><dd><p>
- Optional key words. They have no effect.
- </p></dd></dl></div><p>
- Refer to <a class="xref" href="sql-set-transaction.html" title="SET TRANSACTION"><span class="refentrytitle">SET TRANSACTION</span></a> for information on the meaning
- of the other parameters to this statement.
- </p></div><div class="refsect1" id="id-1.9.3.47.7"><h2>Notes</h2><p>
- <a class="xref" href="sql-start-transaction.html" title="START TRANSACTION"><span class="refentrytitle">START TRANSACTION</span></a> has the same functionality
- as <code class="command">BEGIN</code>.
- </p><p>
- Use <a class="xref" href="sql-commit.html" title="COMMIT"><span class="refentrytitle">COMMIT</span></a> or
- <a class="xref" href="sql-rollback.html" title="ROLLBACK"><span class="refentrytitle">ROLLBACK</span></a>
- to terminate a transaction block.
- </p><p>
- Issuing <code class="command">BEGIN</code> when already inside a transaction block will
- provoke a warning message. The state of the transaction is not affected.
- To nest transactions within a transaction block, use savepoints
- (see <a class="xref" href="sql-savepoint.html" title="SAVEPOINT"><span class="refentrytitle">SAVEPOINT</span></a>).
- </p><p>
- For reasons of backwards compatibility, the commas between successive
- <em class="replaceable"><code>transaction_modes</code></em> can be
- omitted.
- </p></div><div class="refsect1" id="id-1.9.3.47.8"><h2>Examples</h2><p>
- To begin a transaction block:
-
- </p><pre class="programlisting">
- BEGIN;
- </pre></div><div class="refsect1" id="id-1.9.3.47.9"><h2>Compatibility</h2><p>
- <code class="command">BEGIN</code> is a <span class="productname">PostgreSQL</span>
- language extension. It is equivalent to the SQL-standard command
- <a class="xref" href="sql-start-transaction.html" title="START TRANSACTION"><span class="refentrytitle">START TRANSACTION</span></a>, whose reference page
- contains additional compatibility information.
- </p><p>
- The <code class="literal">DEFERRABLE</code>
- <em class="replaceable"><code>transaction_mode</code></em>
- is a <span class="productname">PostgreSQL</span> language extension.
- </p><p>
- Incidentally, the <code class="literal">BEGIN</code> 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.
- </p></div><div class="refsect1" id="id-1.9.3.47.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-commit.html" title="COMMIT"><span class="refentrytitle">COMMIT</span></a>, <a class="xref" href="sql-rollback.html" title="ROLLBACK"><span class="refentrytitle">ROLLBACK</span></a>, <a class="xref" href="sql-start-transaction.html" title="START TRANSACTION"><span class="refentrytitle">START TRANSACTION</span></a>, <a class="xref" href="sql-savepoint.html" title="SAVEPOINT"><span class="refentrytitle">SAVEPOINT</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-analyze.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-call.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ANALYZE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CALL</td></tr></table></div></body></html>
|