]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/sql-begin.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / sql-begin.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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 Vsnapshot" /><link rel="prev" href="sql-analyze.html" title="ANALYZE" /><link rel="next" href="sql-call.html" title="CALL" /></head><body id="docContent" class="container-fluid col-10"><div 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 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-call.html" title="CALL">Next</a></td></tr></table><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">
3 BEGIN [ WORK | TRANSACTION ] [ <em class="replaceable"><code>transaction_mode</code></em> [, ...] ]
4
5 <span class="phrase">where <em class="replaceable"><code>transaction_mode</code></em> is one of:</span>
6
7     ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
8     READ WRITE | READ ONLY
9     [ NOT ] DEFERRABLE
10 </pre></div><div class="refsect1" id="id-1.9.3.47.5"><h2>Description</h2><p>
11    <code class="command">BEGIN</code> initiates a transaction block, that is,
12    all statements after a <code class="command">BEGIN</code> command will be
13    executed in a single transaction until an explicit <a class="link" href="sql-commit.html" title="COMMIT"><code class="command">COMMIT</code></a> or <a class="link" href="sql-rollback.html" title="ROLLBACK"><code class="command">ROLLBACK</code></a> is given.
14    By default (without <code class="command">BEGIN</code>),
15    <span class="productname">PostgreSQL</span> executes
16    transactions in <span class="quote">“<span class="quote">autocommit</span>”</span> mode, that is, each
17    statement is executed in its own transaction and a commit is
18    implicitly performed at the end of the statement (if execution was
19    successful, otherwise a rollback is done).
20   </p><p>
21    Statements are executed more quickly in a transaction block, because
22    transaction start/commit requires significant CPU and disk
23    activity. Execution of multiple statements inside a transaction is
24    also useful to ensure consistency when making several related changes:
25    other sessions will be unable to see the intermediate states
26    wherein not all the related updates have been done.
27   </p><p>
28    If the isolation level, read/write mode, or deferrable mode is specified, the new
29    transaction has those characteristics, as if
30    <a class="link" href="sql-set-transaction.html" title="SET TRANSACTION"><code class="command">SET TRANSACTION</code></a>
31    was executed.
32   </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>
33       Optional key words. They have no effect.
34      </p></dd></dl></div><p>
35    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
36    of the other parameters to this statement.
37   </p></div><div class="refsect1" id="id-1.9.3.47.7"><h2>Notes</h2><p>
38    <a class="link" href="sql-start-transaction.html" title="START TRANSACTION"><code class="command">START TRANSACTION</code></a> has the same functionality
39    as <code class="command">BEGIN</code>.
40   </p><p>
41    Use <a class="link" href="sql-commit.html" title="COMMIT"><code class="command">COMMIT</code></a> or
42    <a class="link" href="sql-rollback.html" title="ROLLBACK"><code class="command">ROLLBACK</code></a>
43    to terminate a transaction block.
44   </p><p>
45    Issuing <code class="command">BEGIN</code> when already inside a transaction block will
46    provoke a warning message.  The state of the transaction is not affected.
47    To nest transactions within a transaction block, use savepoints
48    (see <a class="xref" href="sql-savepoint.html" title="SAVEPOINT"><span class="refentrytitle">SAVEPOINT</span></a>).
49   </p><p>
50    For reasons of backwards compatibility, the commas between successive
51    <em class="replaceable"><code>transaction_modes</code></em> can be
52    omitted.
53   </p></div><div class="refsect1" id="id-1.9.3.47.8"><h2>Examples</h2><p>
54    To begin a transaction block:
55
56 </p><pre class="programlisting">
57 BEGIN;
58 </pre></div><div class="refsect1" id="id-1.9.3.47.9"><h2>Compatibility</h2><p>
59    <code class="command">BEGIN</code> is a <span class="productname">PostgreSQL</span>
60    language extension.  It is equivalent to the SQL-standard command
61    <a class="link" href="sql-start-transaction.html" title="START TRANSACTION"><code class="command">START TRANSACTION</code></a>, whose reference page
62    contains additional compatibility information.
63   </p><p>
64    The <code class="literal">DEFERRABLE</code>
65    <em class="replaceable"><code>transaction_mode</code></em>
66    is a <span class="productname">PostgreSQL</span> language extension.
67   </p><p>
68    Incidentally, the <code class="literal">BEGIN</code> key word is used for a
69    different purpose in embedded SQL. You are advised to be careful
70    about the transaction semantics when porting database applications.
71   </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" title="ANALYZE">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-call.html" title="CALL">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" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CALL</td></tr></table></div></body></html>