gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

82 行
5.8KB

  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>42.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 V1.79.1" /><link rel="prev" href="plpgsql-cursors.html" title="42.7. Cursors" /><link rel="next" href="plpgsql-errors-and-messages.html" title="42.9. Errors and Messages" /></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">42.8. Transaction Management</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpgsql-cursors.html" title="42.7. Cursors">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plpgsql.html" title="Chapter 42. PL/pgSQL - SQL Procedural Language">Up</a></td><th width="60%" align="center">Chapter 42. <span xmlns="http://www.w3.org/1999/xhtml" class="application">PL/pgSQL</span> - <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> Procedural Language</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="plpgsql-errors-and-messages.html" title="42.9. Errors and Messages">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPGSQL-TRANSACTIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">42.8. Transaction Management</h2></div></div></div><p>
  3. In procedures invoked by the <code class="command">CALL</code> command
  4. as well as in anonymous code blocks (<code class="command">DO</code> command),
  5. it is possible to end transactions using the
  6. commands <code class="command">COMMIT</code> and <code class="command">ROLLBACK</code>. A new
  7. transaction is started automatically after a transaction is ended using
  8. these commands, so there is no separate <code class="command">START
  9. TRANSACTION</code> command. (Note that <code class="command">BEGIN</code> and
  10. <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">
  14. CREATE PROCEDURE transaction_test1()
  15. LANGUAGE plpgsql
  16. AS $$
  17. BEGIN
  18. FOR i IN 0..9 LOOP
  19. INSERT INTO test1 (a) VALUES (i);
  20. IF i % 2 = 0 THEN
  21. COMMIT;
  22. ELSE
  23. ROLLBACK;
  24. END IF;
  25. END LOOP;
  26. END
  27. $$;
  28. CALL transaction_test1();
  29. </pre><p>
  30. </p><a id="id-1.8.8.10.4" class="indexterm"></a><p id="PLPGSQL-TRANSACTION-CHAIN">
  31. A new transaction starts out with default transaction characteristics such
  32. as transaction isolation level. In cases where transactions are committed
  33. in a loop, it might be desirable to start new transactions automatically
  34. with the same characteristics as the previous one. The commands
  35. <code class="command">COMMIT AND CHAIN</code> and <code class="command">ROLLBACK AND
  36. CHAIN</code> accomplish this.
  37. </p><p>
  38. Transaction control is only possible in <code class="command">CALL</code> or
  39. <code class="command">DO</code> invocations from the top level or nested
  40. <code class="command">CALL</code> or <code class="command">DO</code> invocations without any
  41. other intervening command. For example, if the call stack is
  42. <code class="command">CALL proc1()</code> → <code class="command">CALL proc2()</code>
  43. → <code class="command">CALL proc3()</code>, then the second and third
  44. procedures can perform transaction control actions. But if the call stack
  45. is <code class="command">CALL proc1()</code> → <code class="command">SELECT
  46. func2()</code> → <code class="command">CALL proc3()</code>, then the last
  47. procedure cannot do transaction control, because of the
  48. <code class="command">SELECT</code> in between.
  49. </p><p>
  50. Special considerations apply to cursor loops. Consider this example:
  51. </p><pre class="programlisting">
  52. CREATE PROCEDURE transaction_test2()
  53. LANGUAGE plpgsql
  54. AS $$
  55. DECLARE
  56. r RECORD;
  57. BEGIN
  58. FOR r IN SELECT * FROM test2 ORDER BY x LOOP
  59. INSERT INTO test1 (a) VALUES (r.x);
  60. COMMIT;
  61. END LOOP;
  62. END;
  63. $$;
  64. CALL transaction_test2();
  65. </pre><p>
  66. Normally, cursors are automatically closed at transaction commit.
  67. However, a cursor created as part of a loop like this is automatically
  68. converted to a holdable cursor by the first <code class="command">COMMIT</code> or
  69. <code class="command">ROLLBACK</code>. That means that the cursor is fully
  70. evaluated at the first <code class="command">COMMIT</code> or
  71. <code class="command">ROLLBACK</code> rather than row by row. The cursor is still
  72. removed automatically after the loop, so this is mostly invisible to the
  73. user.
  74. </p><p>
  75. Transaction commands are not allowed in cursor loops driven by commands
  76. that are not read-only (for example <code class="command">UPDATE
  77. ... RETURNING</code>).
  78. </p><p>
  79. A transaction cannot be ended inside a block with exception handlers.
  80. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpgsql.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpgsql-errors-and-messages.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">42.7. Cursors </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 42.9. Errors and Messages</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1