Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
plpgsql-transactions.html82 linesDownload Raw Back to 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>43.8. Transaction Management</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="plpgsql-cursors.html" title="43.7. Cursors" /><link rel="next" href="plpgsql-errors-and-messages.html" title="43.9. Errors and Messages" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">43.8. Transaction Management</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpgsql-cursors.html" title="43.7. Cursors">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plpgsql.html" title="Chapter 43. PL/pgSQL — SQL Procedural Language">Up</a></td><th width="60%" align="center">Chapter 43. <span class="application">PL/pgSQL</span> — <acronym class="acronym">SQL</acronym> Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plpgsql-errors-and-messages.html" title="43.9. Errors and Messages">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPGSQL-TRANSACTIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">43.8. Transaction Management <a href="#PLPGSQL-TRANSACTIONS" class="id_link">#</a></h2></div></div></div><p>3    In procedures invoked by the <code class="command">CALL</code> command4    as well as in anonymous code blocks (<code class="command">DO</code> command),5    it is possible to end transactions using the6    commands <code class="command">COMMIT</code> and <code class="command">ROLLBACK</code>.  A new7    transaction is started automatically after a transaction is ended using8    these commands, so there is no separate <code class="command">START9    TRANSACTION</code> command.  (Note that <code class="command">BEGIN</code> and10    <code class="command">END</code> have different meanings in PL/pgSQL.)11   </p><p>12    Here is a simple example:13</p><pre class="programlisting">14CREATE PROCEDURE transaction_test1()15LANGUAGE plpgsql16AS $$17BEGIN18    FOR i IN 0..9 LOOP19        INSERT INTO test1 (a) VALUES (i);20        IF i % 2 = 0 THEN21            COMMIT;22        ELSE23            ROLLBACK;24        END IF;25    END LOOP;26END;27$$;28 29CALL transaction_test1();30</pre><p>31   </p><a id="id-1.8.8.10.4" class="indexterm"></a><p id="PLPGSQL-TRANSACTION-CHAIN">32    A new transaction starts out with default transaction characteristics such33    as transaction isolation level.  In cases where transactions are committed34    in a loop, it might be desirable to start new transactions automatically35    with the same characteristics as the previous one.  The commands36    <code class="command">COMMIT AND CHAIN</code> and <code class="command">ROLLBACK AND37    CHAIN</code> accomplish this.38   </p><p>39    Transaction control is only possible in <code class="command">CALL</code> or40    <code class="command">DO</code> invocations from the top level or nested41    <code class="command">CALL</code> or <code class="command">DO</code> invocations without any42    other intervening command.  For example, if the call stack is43    <code class="command">CALL proc1()</code> → <code class="command">CALL proc2()</code>44    → <code class="command">CALL proc3()</code>, then the second and third45    procedures can perform transaction control actions.  But if the call stack46    is <code class="command">CALL proc1()</code> → <code class="command">SELECT47    func2()</code> → <code class="command">CALL proc3()</code>, then the last48    procedure cannot do transaction control, because of the49    <code class="command">SELECT</code> in between.50   </p><p>51    Special considerations apply to cursor loops.  Consider this example:52</p><pre class="programlisting">53CREATE PROCEDURE transaction_test2()54LANGUAGE plpgsql55AS $$56DECLARE57    r RECORD;58BEGIN59    FOR r IN SELECT * FROM test2 ORDER BY x LOOP60        INSERT INTO test1 (a) VALUES (r.x);61        COMMIT;62    END LOOP;63END;64$$;65 66CALL transaction_test2();67</pre><p>68    Normally, cursors are automatically closed at transaction commit.69    However, a cursor created as part of a loop like this is automatically70    converted to a holdable cursor by the first <code class="command">COMMIT</code> or71    <code class="command">ROLLBACK</code>.  That means that the cursor is fully72    evaluated at the first <code class="command">COMMIT</code> or73    <code class="command">ROLLBACK</code> rather than row by row.  The cursor is still74    removed automatically after the loop, so this is mostly invisible to the75    user.76   </p><p>77    Transaction commands are not allowed in cursor loops driven by commands78    that are not read-only (for example <code class="command">UPDATE79    ... RETURNING</code>).80   </p><p>81    A transaction cannot be ended inside a block with exception handlers.82   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpgsql-cursors.html" title="43.7. Cursors">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpgsql.html" title="Chapter 43. PL/pgSQL — SQL Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpgsql-errors-and-messages.html" title="43.9. Errors and Messages">Next</a></td></tr><tr><td width="40%" align="left" valign="top">43.7. Cursors </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 43.9. Errors and Messages</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai