gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

107 lignes
9.7KB

  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>37.7. Function Volatility Categories</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="xfunc-overload.html" title="37.6. Function Overloading" /><link rel="next" href="xfunc-pl.html" title="37.8. Procedural Language 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">37.7. Function Volatility Categories</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="xfunc-overload.html" title="37.6. Function Overloading">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="extend.html" title="Chapter 37. Extending SQL">Up</a></td><th width="60%" align="center">Chapter 37. Extending <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym></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="xfunc-pl.html" title="37.8. Procedural Language Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="XFUNC-VOLATILITY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">37.7. Function Volatility Categories</h2></div></div></div><a id="id-1.8.3.10.2" class="indexterm"></a><a id="id-1.8.3.10.3" class="indexterm"></a><a id="id-1.8.3.10.4" class="indexterm"></a><a id="id-1.8.3.10.5" class="indexterm"></a><p>
  3. Every function has a <em class="firstterm">volatility</em> classification, with
  4. the possibilities being <code class="literal">VOLATILE</code>, <code class="literal">STABLE</code>, or
  5. <code class="literal">IMMUTABLE</code>. <code class="literal">VOLATILE</code> is the default if the
  6. <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>
  7. command does not specify a category. The volatility category is a
  8. promise to the optimizer about the behavior of the function:
  9. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  10. A <code class="literal">VOLATILE</code> function can do anything, including modifying
  11. the database. It can return different results on successive calls with
  12. the same arguments. The optimizer makes no assumptions about the
  13. behavior of such functions. A query using a volatile function will
  14. re-evaluate the function at every row where its value is needed.
  15. </p></li><li class="listitem"><p>
  16. A <code class="literal">STABLE</code> function cannot modify the database and is
  17. guaranteed to return the same results given the same arguments
  18. for all rows within a single statement. This category allows the
  19. optimizer to optimize multiple calls of the function to a single
  20. call. In particular, it is safe to use an expression containing
  21. such a function in an index scan condition. (Since an index scan
  22. will evaluate the comparison value only once, not once at each
  23. row, it is not valid to use a <code class="literal">VOLATILE</code> function in an
  24. index scan condition.)
  25. </p></li><li class="listitem"><p>
  26. An <code class="literal">IMMUTABLE</code> function cannot modify the database and is
  27. guaranteed to return the same results given the same arguments forever.
  28. This category allows the optimizer to pre-evaluate the function when
  29. a query calls it with constant arguments. For example, a query like
  30. <code class="literal">SELECT ... WHERE x = 2 + 2</code> can be simplified on sight to
  31. <code class="literal">SELECT ... WHERE x = 4</code>, because the function underlying
  32. the integer addition operator is marked <code class="literal">IMMUTABLE</code>.
  33. </p></li></ul></div><p>
  34. </p><p>
  35. For best optimization results, you should label your functions with the
  36. strictest volatility category that is valid for them.
  37. </p><p>
  38. Any function with side-effects <span class="emphasis"><em>must</em></span> be labeled
  39. <code class="literal">VOLATILE</code>, so that calls to it cannot be optimized away.
  40. Even a function with no side-effects needs to be labeled
  41. <code class="literal">VOLATILE</code> if its value can change within a single query;
  42. some examples are <code class="literal">random()</code>, <code class="literal">currval()</code>,
  43. <code class="literal">timeofday()</code>.
  44. </p><p>
  45. Another important example is that the <code class="function">current_timestamp</code>
  46. family of functions qualify as <code class="literal">STABLE</code>, since their values do
  47. not change within a transaction.
  48. </p><p>
  49. There is relatively little difference between <code class="literal">STABLE</code> and
  50. <code class="literal">IMMUTABLE</code> categories when considering simple interactive
  51. queries that are planned and immediately executed: it doesn't matter
  52. a lot whether a function is executed once during planning or once during
  53. query execution startup. But there is a big difference if the plan is
  54. saved and reused later. Labeling a function <code class="literal">IMMUTABLE</code> when
  55. it really isn't might allow it to be prematurely folded to a constant during
  56. planning, resulting in a stale value being re-used during subsequent uses
  57. of the plan. This is a hazard when using prepared statements or when
  58. using function languages that cache plans (such as
  59. <span class="application">PL/pgSQL</span>).
  60. </p><p>
  61. For functions written in SQL or in any of the standard procedural
  62. languages, there is a second important property determined by the
  63. volatility category, namely the visibility of any data changes that have
  64. been made by the SQL command that is calling the function. A
  65. <code class="literal">VOLATILE</code> function will see such changes, a <code class="literal">STABLE</code>
  66. or <code class="literal">IMMUTABLE</code> function will not. This behavior is implemented
  67. using the snapshotting behavior of MVCC (see <a class="xref" href="mvcc.html" title="Chapter 13. Concurrency Control">Chapter 13</a>):
  68. <code class="literal">STABLE</code> and <code class="literal">IMMUTABLE</code> functions use a snapshot
  69. established as of the start of the calling query, whereas
  70. <code class="literal">VOLATILE</code> functions obtain a fresh snapshot at the start of
  71. each query they execute.
  72. </p><div class="note"><h3 class="title">Note</h3><p>
  73. Functions written in C can manage snapshots however they want, but it's
  74. usually a good idea to make C functions work this way too.
  75. </p></div><p>
  76. Because of this snapshotting behavior,
  77. a function containing only <code class="command">SELECT</code> commands can safely be
  78. marked <code class="literal">STABLE</code>, even if it selects from tables that might be
  79. undergoing modifications by concurrent queries.
  80. <span class="productname">PostgreSQL</span> will execute all commands of a
  81. <code class="literal">STABLE</code> function using the snapshot established for the
  82. calling query, and so it will see a fixed view of the database throughout
  83. that query.
  84. </p><p>
  85. The same snapshotting behavior is used for <code class="command">SELECT</code> commands
  86. within <code class="literal">IMMUTABLE</code> functions. It is generally unwise to select
  87. from database tables within an <code class="literal">IMMUTABLE</code> function at all,
  88. since the immutability will be broken if the table contents ever change.
  89. However, <span class="productname">PostgreSQL</span> does not enforce that you
  90. do not do that.
  91. </p><p>
  92. A common error is to label a function <code class="literal">IMMUTABLE</code> when its
  93. results depend on a configuration parameter. For example, a function
  94. that manipulates timestamps might well have results that depend on the
  95. <a class="xref" href="runtime-config-client.html#GUC-TIMEZONE">TimeZone</a> setting. For safety, such functions should
  96. be labeled <code class="literal">STABLE</code> instead.
  97. </p><div class="note"><h3 class="title">Note</h3><p>
  98. <span class="productname">PostgreSQL</span> requires that <code class="literal">STABLE</code>
  99. and <code class="literal">IMMUTABLE</code> functions contain no SQL commands other
  100. than <code class="command">SELECT</code> to prevent data modification.
  101. (This is not a completely bulletproof test, since such functions could
  102. still call <code class="literal">VOLATILE</code> functions that modify the database.
  103. If you do that, you will find that the <code class="literal">STABLE</code> or
  104. <code class="literal">IMMUTABLE</code> function does not notice the database changes
  105. applied by the called function, since they are hidden from its snapshot.)
  106. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="xfunc-overload.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extend.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="xfunc-pl.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">37.6. Function Overloading </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 37.8. Procedural Language Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1