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

149 行
15KB

  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>9.16. Sequence Manipulation Functions</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="functions-json.html" title="9.15. JSON Functions and Operators" /><link rel="next" href="functions-conditional.html" title="9.17. Conditional Expressions" /></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">9.16. Sequence Manipulation Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-json.html" title="9.15. JSON Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</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="functions-conditional.html" title="9.17. Conditional Expressions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-SEQUENCE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.16. Sequence Manipulation Functions</h2></div></div></div><a id="id-1.5.8.21.2" class="indexterm"></a><a id="id-1.5.8.21.3" class="indexterm"></a><a id="id-1.5.8.21.4" class="indexterm"></a><a id="id-1.5.8.21.5" class="indexterm"></a><a id="id-1.5.8.21.6" class="indexterm"></a><p>
  3. This section describes functions for operating on <em class="firstterm">sequence
  4. objects</em>, also called sequence generators or just sequences.
  5. Sequence objects are special single-row tables created with <a class="xref" href="sql-createsequence.html" title="CREATE SEQUENCE"><span class="refentrytitle">CREATE SEQUENCE</span></a>.
  6. Sequence objects are commonly used to generate unique identifiers
  7. for rows of a table. The sequence functions, listed in <a class="xref" href="functions-sequence.html#FUNCTIONS-SEQUENCE-TABLE" title="Table 9.50. Sequence Functions">Table 9.50</a>, provide simple, multiuser-safe
  8. methods for obtaining successive sequence values from sequence
  9. objects.
  10. </p><div class="table" id="FUNCTIONS-SEQUENCE-TABLE"><p class="title"><strong>Table 9.50. Sequence Functions</strong></p><div class="table-contents"><table class="table" summary="Sequence Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Return Type</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal"><code class="function">currval(<code class="type">regclass</code>)</code></code></td><td><code class="type">bigint</code></td><td>Return value most recently obtained with
  11. <code class="function">nextval</code> for specified sequence</td></tr><tr><td><code class="literal"><code class="function">lastval()</code></code></td><td><code class="type">bigint</code></td><td>Return value most recently obtained with
  12. <code class="function">nextval</code> for any sequence</td></tr><tr><td><code class="literal"><code class="function">nextval(<code class="type">regclass</code>)</code></code></td><td><code class="type">bigint</code></td><td>Advance sequence and return new value</td></tr><tr><td><code class="literal"><code class="function">setval(<code class="type">regclass</code>, <code class="type">bigint</code>)</code></code></td><td><code class="type">bigint</code></td><td>Set sequence's current value</td></tr><tr><td><code class="literal"><code class="function">setval(<code class="type">regclass</code>, <code class="type">bigint</code>, <code class="type">boolean</code>)</code></code></td><td><code class="type">bigint</code></td><td>Set sequence's current value and <code class="literal">is_called</code> flag</td></tr></tbody></table></div></div><br class="table-break" /><p>
  13. The sequence to be operated on by a sequence function is specified by
  14. a <code class="type">regclass</code> argument, which is simply the OID of the sequence in the
  15. <code class="structname">pg_class</code> system catalog. You do not have to look up the
  16. OID by hand, however, since the <code class="type">regclass</code> data type's input
  17. converter will do the work for you. Just write the sequence name enclosed
  18. in single quotes so that it looks like a literal constant. For
  19. compatibility with the handling of ordinary
  20. <acronym class="acronym">SQL</acronym> names, the string will be converted to lower case
  21. unless it contains double quotes around the sequence name. Thus:
  22. </p><pre class="programlisting">
  23. nextval('foo') <em class="lineannotation"><span class="lineannotation">operates on sequence <code class="literal">foo</code></span></em>
  24. nextval('FOO') <em class="lineannotation"><span class="lineannotation">operates on sequence <code class="literal">foo</code></span></em>
  25. nextval('"Foo"') <em class="lineannotation"><span class="lineannotation">operates on sequence <code class="literal">Foo</code></span></em>
  26. </pre><p>
  27. The sequence name can be schema-qualified if necessary:
  28. </p><pre class="programlisting">
  29. nextval('myschema.foo') <em class="lineannotation"><span class="lineannotation">operates on <code class="literal">myschema.foo</code></span></em>
  30. nextval('"myschema".foo') <em class="lineannotation"><span class="lineannotation">same as above</span></em>
  31. nextval('foo') <em class="lineannotation"><span class="lineannotation">searches search path for <code class="literal">foo</code></span></em>
  32. </pre><p>
  33. See <a class="xref" href="datatype-oid.html" title="8.19. Object Identifier Types">Section 8.19</a> for more information about
  34. <code class="type">regclass</code>.
  35. </p><div class="note"><h3 class="title">Note</h3><p>
  36. Before <span class="productname">PostgreSQL</span> 8.1, the arguments of the
  37. sequence functions were of type <code class="type">text</code>, not <code class="type">regclass</code>, and
  38. the above-described conversion from a text string to an OID value would
  39. happen at run time during each call. For backward compatibility, this
  40. facility still exists, but internally it is now handled as an implicit
  41. coercion from <code class="type">text</code> to <code class="type">regclass</code> before the function is
  42. invoked.
  43. </p><p>
  44. When you write the argument of a sequence function as an unadorned
  45. literal string, it becomes a constant of type <code class="type">regclass</code>.
  46. Since this is really just an OID, it will track the originally
  47. identified sequence despite later renaming, schema reassignment,
  48. etc. This <span class="quote">“<span class="quote">early binding</span>”</span> behavior is usually desirable for
  49. sequence references in column defaults and views. But sometimes you might
  50. want <span class="quote">“<span class="quote">late binding</span>”</span> where the sequence reference is resolved
  51. at run time. To get late-binding behavior, force the constant to be
  52. stored as a <code class="type">text</code> constant instead of <code class="type">regclass</code>:
  53. </p><pre class="programlisting">
  54. nextval('foo'::text) <em class="lineannotation"><span class="lineannotation"><code class="literal">foo</code> is looked up at runtime</span></em>
  55. </pre><p>
  56. Note that late binding was the only behavior supported in
  57. <span class="productname">PostgreSQL</span> releases before 8.1, so you
  58. might need to do this to preserve the semantics of old applications.
  59. </p><p>
  60. Of course, the argument of a sequence function can be an expression
  61. as well as a constant. If it is a text expression then the implicit
  62. coercion will result in a run-time lookup.
  63. </p></div><p>
  64. The available sequence functions are:
  65. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">nextval</code></span></dt><dd><p>
  66. Advance the sequence object to its next value and return that
  67. value. This is done atomically: even if multiple sessions
  68. execute <code class="function">nextval</code> concurrently, each will safely receive
  69. a distinct sequence value.
  70. </p><p>
  71. If a sequence object has been created with default parameters,
  72. successive <code class="function">nextval</code> calls will return successive
  73. values beginning with 1. Other behaviors can be obtained by using
  74. special parameters in the <a class="xref" href="sql-createsequence.html" title="CREATE SEQUENCE"><span class="refentrytitle">CREATE SEQUENCE</span></a> command;
  75. see its command reference page for more information.
  76. </p><div class="important"><h3 class="title">Important</h3><p>
  77. To avoid blocking concurrent transactions that obtain numbers from
  78. the same sequence, a <code class="function">nextval</code> operation is never
  79. rolled back; that is, once a value has been fetched it is considered
  80. used and will not be returned again. This is true even if the
  81. surrounding transaction later aborts, or if the calling query ends
  82. up not using the value. For example an <code class="command">INSERT</code> with
  83. an <code class="literal">ON CONFLICT</code> clause will compute the to-be-inserted
  84. tuple, including doing any required <code class="function">nextval</code>
  85. calls, before detecting any conflict that would cause it to follow
  86. the <code class="literal">ON CONFLICT</code> rule instead. Such cases will leave
  87. unused <span class="quote">“<span class="quote">holes</span>”</span> in the sequence of assigned values.
  88. Thus, <span class="productname">PostgreSQL</span> sequence objects <span class="emphasis"><em>cannot
  89. be used to obtain <span class="quote">“<span class="quote">gapless</span>”</span> sequences</em></span>.
  90. </p></div><p>
  91. This function requires <code class="literal">USAGE</code>
  92. or <code class="literal">UPDATE</code> privilege on the sequence.
  93. </p></dd><dt><span class="term"><code class="function">currval</code></span></dt><dd><p>
  94. Return the value most recently obtained by <code class="function">nextval</code>
  95. for this sequence in the current session. (An error is
  96. reported if <code class="function">nextval</code> has never been called for this
  97. sequence in this session.) Because this is returning
  98. a session-local value, it gives a predictable answer whether or not
  99. other sessions have executed <code class="function">nextval</code> since the
  100. current session did.
  101. </p><p>
  102. This function requires <code class="literal">USAGE</code>
  103. or <code class="literal">SELECT</code> privilege on the sequence.
  104. </p></dd><dt><span class="term"><code class="function">lastval</code></span></dt><dd><p>
  105. Return the value most recently returned by
  106. <code class="function">nextval</code> in the current session. This function is
  107. identical to <code class="function">currval</code>, except that instead
  108. of taking the sequence name as an argument it refers to whichever
  109. sequence <code class="function">nextval</code> was most recently applied to
  110. in the current session. It is an error to call
  111. <code class="function">lastval</code> if <code class="function">nextval</code>
  112. has not yet been called in the current session.
  113. </p><p>
  114. This function requires <code class="literal">USAGE</code>
  115. or <code class="literal">SELECT</code> privilege on the last used sequence.
  116. </p></dd><dt><span class="term"><code class="function">setval</code></span></dt><dd><p>
  117. Reset the sequence object's counter value. The two-parameter
  118. form sets the sequence's <code class="literal">last_value</code> field to the
  119. specified value and sets its <code class="literal">is_called</code> field to
  120. <code class="literal">true</code>, meaning that the next
  121. <code class="function">nextval</code> will advance the sequence before
  122. returning a value. The value reported by <code class="function">currval</code> is
  123. also set to the specified value. In the three-parameter form,
  124. <code class="literal">is_called</code> can be set to either <code class="literal">true</code>
  125. or <code class="literal">false</code>. <code class="literal">true</code> has the same effect as
  126. the two-parameter form. If it is set to <code class="literal">false</code>, the
  127. next <code class="function">nextval</code> will return exactly the specified
  128. value, and sequence advancement commences with the following
  129. <code class="function">nextval</code>. Furthermore, the value reported by
  130. <code class="function">currval</code> is not changed in this case. For example,
  131. </p><pre class="screen">
  132. SELECT setval('foo', 42); <em class="lineannotation"><span class="lineannotation">Next <code class="function">nextval</code> will return 43</span></em>
  133. SELECT setval('foo', 42, true); <em class="lineannotation"><span class="lineannotation">Same as above</span></em>
  134. SELECT setval('foo', 42, false); <em class="lineannotation"><span class="lineannotation">Next <code class="function">nextval</code> will return 42</span></em>
  135. </pre><p>
  136. The result returned by <code class="function">setval</code> is just the value of its
  137. second argument.
  138. </p><div class="important"><h3 class="title">Important</h3><p>
  139. Because sequences are non-transactional, changes made by
  140. <code class="function">setval</code> are not undone if the transaction rolls
  141. back.
  142. </p></div><p>
  143. This function requires <code class="literal">UPDATE</code> privilege on the
  144. sequence.
  145. </p></dd></dl></div><p>
  146. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-json.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-conditional.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.15. JSON Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.17. Conditional Expressions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1