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

148 行
13KB

  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.9. Errors and Messages</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-transactions.html" title="42.8. Transaction Management" /><link rel="next" href="plpgsql-trigger.html" title="42.10. Trigger Functions" /></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.9. Errors and Messages</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpgsql-transactions.html" title="42.8. Transaction Management">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-trigger.html" title="42.10. Trigger Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPGSQL-ERRORS-AND-MESSAGES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">42.9. Errors and Messages</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="plpgsql-errors-and-messages.html#PLPGSQL-STATEMENTS-RAISE">42.9.1. Reporting Errors and Messages</a></span></dt><dt><span class="sect2"><a href="plpgsql-errors-and-messages.html#PLPGSQL-STATEMENTS-ASSERT">42.9.2. Checking Assertions</a></span></dt></dl></div><div class="sect2" id="PLPGSQL-STATEMENTS-RAISE"><div class="titlepage"><div><div><h3 class="title">42.9.1. Reporting Errors and Messages</h3></div></div></div><a id="id-1.8.8.11.2.2" class="indexterm"></a><a id="id-1.8.8.11.2.3" class="indexterm"></a><p>
  3. Use the <code class="command">RAISE</code> statement to report messages and
  4. raise errors.
  5. </p><pre class="synopsis">
  6. RAISE [<span class="optional"> <em class="replaceable"><code>level</code></em> </span>] '<em class="replaceable"><code>format</code></em>' [<span class="optional">, <em class="replaceable"><code>expression</code></em> [<span class="optional">, ... </span>]</span>] [<span class="optional"> USING <em class="replaceable"><code>option</code></em> = <em class="replaceable"><code>expression</code></em> [<span class="optional">, ... </span>] </span>];
  7. RAISE [<span class="optional"> <em class="replaceable"><code>level</code></em> </span>] <em class="replaceable"><code>condition_name</code></em> [<span class="optional"> USING <em class="replaceable"><code>option</code></em> = <em class="replaceable"><code>expression</code></em> [<span class="optional">, ... </span>] </span>];
  8. RAISE [<span class="optional"> <em class="replaceable"><code>level</code></em> </span>] SQLSTATE '<em class="replaceable"><code>sqlstate</code></em>' [<span class="optional"> USING <em class="replaceable"><code>option</code></em> = <em class="replaceable"><code>expression</code></em> [<span class="optional">, ... </span>] </span>];
  9. RAISE [<span class="optional"> <em class="replaceable"><code>level</code></em> </span>] USING <em class="replaceable"><code>option</code></em> = <em class="replaceable"><code>expression</code></em> [<span class="optional">, ... </span>];
  10. RAISE ;
  11. </pre><p>
  12. The <em class="replaceable"><code>level</code></em> option specifies
  13. the error severity. Allowed levels are <code class="literal">DEBUG</code>,
  14. <code class="literal">LOG</code>, <code class="literal">INFO</code>,
  15. <code class="literal">NOTICE</code>, <code class="literal">WARNING</code>,
  16. and <code class="literal">EXCEPTION</code>, with <code class="literal">EXCEPTION</code>
  17. being the default.
  18. <code class="literal">EXCEPTION</code> raises an error (which normally aborts the
  19. current transaction); the other levels only generate messages of different
  20. priority levels.
  21. Whether messages of a particular priority are reported to the client,
  22. written to the server log, or both is controlled by the
  23. <a class="xref" href="runtime-config-logging.html#GUC-LOG-MIN-MESSAGES">log_min_messages</a> and
  24. <a class="xref" href="runtime-config-client.html#GUC-CLIENT-MIN-MESSAGES">client_min_messages</a> configuration
  25. variables. See <a class="xref" href="runtime-config.html" title="Chapter 19. Server Configuration">Chapter 19</a> for more
  26. information.
  27. </p><p>
  28. After <em class="replaceable"><code>level</code></em> if any,
  29. you can write a <em class="replaceable"><code>format</code></em>
  30. (which must be a simple string literal, not an expression). The
  31. format string specifies the error message text to be reported.
  32. The format string can be followed
  33. by optional argument expressions to be inserted into the message.
  34. Inside the format string, <code class="literal">%</code> is replaced by the
  35. string representation of the next optional argument's value. Write
  36. <code class="literal">%%</code> to emit a literal <code class="literal">%</code>.
  37. The number of arguments must match the number of <code class="literal">%</code>
  38. placeholders in the format string, or an error is raised during
  39. the compilation of the function.
  40. </p><p>
  41. In this example, the value of <code class="literal">v_job_id</code> will replace the
  42. <code class="literal">%</code> in the string:
  43. </p><pre class="programlisting">
  44. RAISE NOTICE 'Calling cs_create_job(%)', v_job_id;
  45. </pre><p>
  46. </p><p>
  47. You can attach additional information to the error report by writing
  48. <code class="literal">USING</code> followed by <em class="replaceable"><code>option</code></em> = <em class="replaceable"><code>expression</code></em> items. Each
  49. <em class="replaceable"><code>expression</code></em> can be any
  50. string-valued expression. The allowed <em class="replaceable"><code>option</code></em> key words are:
  51. </p><div class="variablelist" id="RAISE-USING-OPTIONS"><dl class="variablelist"><dt><span class="term"><code class="literal">MESSAGE</code></span></dt><dd><p>Sets the error message text. This option can't be used in the
  52. form of <code class="command">RAISE</code> that includes a format string
  53. before <code class="literal">USING</code>.</p></dd><dt><span class="term"><code class="literal">DETAIL</code></span></dt><dd><p>Supplies an error detail message.</p></dd><dt><span class="term"><code class="literal">HINT</code></span></dt><dd><p>Supplies a hint message.</p></dd><dt><span class="term"><code class="literal">ERRCODE</code></span></dt><dd><p>Specifies the error code (SQLSTATE) to report, either by condition
  54. name, as shown in <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>, or directly as a
  55. five-character SQLSTATE code.</p></dd><dt><span class="term"><code class="literal">COLUMN</code><br /></span><span class="term"><code class="literal">CONSTRAINT</code><br /></span><span class="term"><code class="literal">DATATYPE</code><br /></span><span class="term"><code class="literal">TABLE</code><br /></span><span class="term"><code class="literal">SCHEMA</code></span></dt><dd><p>Supplies the name of a related object.</p></dd></dl></div><p>
  56. </p><p>
  57. This example will abort the transaction with the given error message
  58. and hint:
  59. </p><pre class="programlisting">
  60. RAISE EXCEPTION 'Nonexistent ID --&gt; %', user_id
  61. USING HINT = 'Please check your user ID';
  62. </pre><p>
  63. </p><p>
  64. These two examples show equivalent ways of setting the SQLSTATE:
  65. </p><pre class="programlisting">
  66. RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation';
  67. RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
  68. </pre><p>
  69. </p><p>
  70. There is a second <code class="command">RAISE</code> syntax in which the main argument
  71. is the condition name or SQLSTATE to be reported, for example:
  72. </p><pre class="programlisting">
  73. RAISE division_by_zero;
  74. RAISE SQLSTATE '22012';
  75. </pre><p>
  76. In this syntax, <code class="literal">USING</code> can be used to supply a custom
  77. error message, detail, or hint. Another way to do the earlier
  78. example is
  79. </p><pre class="programlisting">
  80. RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
  81. </pre><p>
  82. </p><p>
  83. Still another variant is to write <code class="literal">RAISE USING</code> or <code class="literal">RAISE
  84. <em class="replaceable"><code>level</code></em> USING</code> and put
  85. everything else into the <code class="literal">USING</code> list.
  86. </p><p>
  87. The last variant of <code class="command">RAISE</code> has no parameters at all.
  88. This form can only be used inside a <code class="literal">BEGIN</code> block's
  89. <code class="literal">EXCEPTION</code> clause;
  90. it causes the error currently being handled to be re-thrown.
  91. </p><div class="note"><h3 class="title">Note</h3><p>
  92. Before <span class="productname">PostgreSQL</span> 9.1, <code class="command">RAISE</code> without
  93. parameters was interpreted as re-throwing the error from the block
  94. containing the active exception handler. Thus an <code class="literal">EXCEPTION</code>
  95. clause nested within that handler could not catch it, even if the
  96. <code class="command">RAISE</code> was within the nested <code class="literal">EXCEPTION</code> clause's
  97. block. This was deemed surprising as well as being incompatible with
  98. Oracle's PL/SQL.
  99. </p></div><p>
  100. If no condition name nor SQLSTATE is specified in a
  101. <code class="command">RAISE EXCEPTION</code> command, the default is to use
  102. <code class="literal">ERRCODE_RAISE_EXCEPTION</code> (<code class="literal">P0001</code>).
  103. If no message text is specified, the default is to use the condition
  104. name or SQLSTATE as message text.
  105. </p><div class="note"><h3 class="title">Note</h3><p>
  106. When specifying an error code by SQLSTATE code, you are not
  107. limited to the predefined error codes, but can select any
  108. error code consisting of five digits and/or upper-case ASCII
  109. letters, other than <code class="literal">00000</code>. It is recommended that
  110. you avoid throwing error codes that end in three zeroes, because
  111. these are category codes and can only be trapped by trapping
  112. the whole category.
  113. </p></div></div><div class="sect2" id="PLPGSQL-STATEMENTS-ASSERT"><div class="titlepage"><div><div><h3 class="title">42.9.2. Checking Assertions</h3></div></div></div><a id="id-1.8.8.11.3.2" class="indexterm"></a><a id="id-1.8.8.11.3.3" class="indexterm"></a><a id="id-1.8.8.11.3.4" class="indexterm"></a><p>
  114. The <code class="command">ASSERT</code> statement is a convenient shorthand for
  115. inserting debugging checks into <span class="application">PL/pgSQL</span>
  116. functions.
  117. </p><pre class="synopsis">
  118. ASSERT <em class="replaceable"><code>condition</code></em> [<span class="optional"> , <em class="replaceable"><code>message</code></em> </span>];
  119. </pre><p>
  120. The <em class="replaceable"><code>condition</code></em> is a Boolean
  121. expression that is expected to always evaluate to true; if it does,
  122. the <code class="command">ASSERT</code> statement does nothing further. If the
  123. result is false or null, then an <code class="literal">ASSERT_FAILURE</code> exception
  124. is raised. (If an error occurs while evaluating
  125. the <em class="replaceable"><code>condition</code></em>, it is
  126. reported as a normal error.)
  127. </p><p>
  128. If the optional <em class="replaceable"><code>message</code></em> is
  129. provided, it is an expression whose result (if not null) replaces the
  130. default error message text <span class="quote">“<span class="quote">assertion failed</span>”</span>, should
  131. the <em class="replaceable"><code>condition</code></em> fail.
  132. The <em class="replaceable"><code>message</code></em> expression is
  133. not evaluated in the normal case where the assertion succeeds.
  134. </p><p>
  135. Testing of assertions can be enabled or disabled via the configuration
  136. parameter <code class="literal">plpgsql.check_asserts</code>, which takes a Boolean
  137. value; the default is <code class="literal">on</code>. If this parameter
  138. is <code class="literal">off</code> then <code class="command">ASSERT</code> statements do nothing.
  139. </p><p>
  140. Note that <code class="command">ASSERT</code> is meant for detecting program
  141. bugs, not for reporting ordinary error conditions. Use
  142. the <code class="command">RAISE</code> statement, described above, for that.
  143. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpgsql-transactions.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-trigger.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">42.8. Transaction Management </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 42.10. Trigger Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1