forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Lu, Huayi <luhuayi@huawei.com> Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
128 lines
14 KiB
HTML
128 lines
14 KiB
HTML
<a name="EN-US_TOPIC_0000001188110524"></a><a name="EN-US_TOPIC_0000001188110524"></a>
|
|
|
|
<h1 class="topictitle1">SEQUENCE Functions</h1>
|
|
<div id="body1495779428189"><p id="EN-US_TOPIC_0000001188110524__a363f613923e340c6bab7d782ca87fbcf">The sequence functions provide a simple method to ensure security of multiple users for users to obtain sequence values from sequence objects.</p>
|
|
<div class="note" id="EN-US_TOPIC_0000001188110524__note78071427191917"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001188110524__p11734142116243">The hybrid data warehouse (standalone) does not support <strong id="EN-US_TOPIC_0000001188110524__b14650185019214">SEQUENCE</strong> and related functions.</p>
|
|
</div></div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188110524__section2064175413259"><h4 class="sectiontitle">nextval(regclass)</h4><p id="EN-US_TOPIC_0000001188110524__p4279155672512">Specifies an increasing sequence and returns a new value.</p>
|
|
<div class="note" id="EN-US_TOPIC_0000001188110524__note93084124714"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><ul id="EN-US_TOPIC_0000001188110524__ul731114104714"><li id="EN-US_TOPIC_0000001188110524__li133124154716">To avoid blocking of concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used, even if the transaction that did the nextval later aborts. This means that aborted transactions may leave unused "holes" in the sequence of assigned values. Therefore, sequences in <span id="EN-US_TOPIC_0000001188110524__text456520506">GaussDB(DWS)</span> cannot be used to obtain sequence without gaps.</li><li id="EN-US_TOPIC_0000001188110524__li73112413472">If the nextval function is pushed to DNs, each DN will automatically connect to the GTM and requests the next value. For example, in the <strong id="EN-US_TOPIC_0000001188110524__b842352706204242">insert into t1 select xxx</strong> statement, a column in table <strong id="EN-US_TOPIC_0000001188110524__b84235270620433">t1</strong> needs to invoke the nextval function. If maximum number of connections on the GTM is 8192, this type of pushed statements occupies too many GTM connections. Therefore, the number of concurrent connections for these statements is limited to 7000 divided by the number of cluster DNs. The other 1192 connections are reserved for other statements.</li></ul>
|
|
</div></div>
|
|
<p id="EN-US_TOPIC_0000001188110524__p11018168471">Return type: bigint</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p8101165474">The <strong id="EN-US_TOPIC_0000001188110524__b1410193012719">nextval</strong> function can be invoked in either of the following ways: (In example 2, the Oracle syntax is supported. Currently, the sequence name cannot contain a dot.)</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p110141611474">Example 1:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen14121325134712"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">nextval</span><span class="p">(</span><span class="s1">'seqDemo'</span><span class="p">);</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="n">nextval</span>
|
|
<span class="c1">---------</span>
|
|
<span class="w"> </span><span class="mi">2</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188110524__p1479182324720">Example 2:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen152301839134714"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">seqDemo</span><span class="p">.</span><span class="n">nextval</span><span class="p">;</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="n">nextval</span>
|
|
<span class="c1">---------</span>
|
|
<span class="w"> </span><span class="mi">2</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188110524__section3956656134710"><h4 class="sectiontitle">currval(regclass)</h4><p id="EN-US_TOPIC_0000001188110524__p177662224810">Returns the last value of <strong id="EN-US_TOPIC_0000001188110524__b393411614205">nextval</strong> for a specified sequence in the current session. If <strong id="EN-US_TOPIC_0000001188110524__b196510619500">nextval</strong> has not been invoked for the specified sequence in the current session, an error is reported when <strong id="EN-US_TOPIC_0000001188110524__b5565173802111">currval</strong> is invoked. By default, <strong id="EN-US_TOPIC_0000001188110524__b1192556235">currval</strong> is disabled. To enable it, set <strong id="EN-US_TOPIC_0000001188110524__b18349122452315">enable_beta_features</strong> to <strong id="EN-US_TOPIC_0000001188110524__b6931151864117">true</strong>. After <strong id="EN-US_TOPIC_0000001188110524__b62701450162310">currval</strong> is enabled, <strong id="EN-US_TOPIC_0000001188110524__b10401539185414">nextval</strong> will not be pushed down.</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p137666274816">Return type: bigint</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p67669211486">The <strong id="EN-US_TOPIC_0000001188110524__b7534439172819">currval</strong> function can be invoked in either of the following ways: (In example 2, the Oracle syntax is supported. Currently, the sequence name cannot contain a dot.)</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p1276682154812">Example 1:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen13946131118486"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">currval</span><span class="p">(</span><span class="s1">'seq1'</span><span class="p">);</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="n">currval</span>
|
|
<span class="c1">---------</span>
|
|
<span class="w"> </span><span class="mi">2</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001188110524__p89461111114820">Example 2:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen794641118485"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">seq1</span><span class="p">.</span><span class="n">currval</span><span class="w"> </span><span class="n">seq1</span><span class="p">;</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="n">currval</span>
|
|
<span class="c1">---------</span>
|
|
<span class="w"> </span><span class="mi">2</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188110524__section972311333487"><h4 class="sectiontitle">lastval()</h4><p id="EN-US_TOPIC_0000001188110524__p1681144114812">Returns the last value of <strong id="EN-US_TOPIC_0000001188110524__b15113181013442">nextval</strong> in the current session. This function is equivalent to <strong id="EN-US_TOPIC_0000001188110524__b1869514325444">currval</strong>, but <strong id="EN-US_TOPIC_0000001188110524__b16721648205213">lastval</strong> does not have a parameter. If <strong id="EN-US_TOPIC_0000001188110524__b133893449">nextval</strong> has not been invoked in the current session, an error is reported when <strong id="EN-US_TOPIC_0000001188110524__b994111315501">lastval</strong> is invoked.</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p10811641164814">By default, <strong id="EN-US_TOPIC_0000001188110524__b1667814301277">lastval</strong> is disabled. To enable it, set <strong id="EN-US_TOPIC_0000001188110524__b0945577241">enable_beta_features</strong> or <strong id="EN-US_TOPIC_0000001188110524__b17931418204111">lastval_supported</strong> to <strong id="EN-US_TOPIC_0000001188110524__b673551011">true</strong>. After <strong id="EN-US_TOPIC_0000001188110524__b1731734193010">lastval</strong> is enabled, <strong id="EN-US_TOPIC_0000001188110524__b1920581822511">nextval</strong> will not be pushed down.</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p18118414482">Return type: bigint</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p881144120486">For example:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen1116884764812"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">lastval</span><span class="p">();</span><span class="w"> </span>
|
|
<span class="w"> </span><span class="n">lastval</span>
|
|
<span class="c1">---------</span>
|
|
<span class="w"> </span><span class="mi">2</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188110524__section8716194284918"><h4 class="sectiontitle">setval(regclass, bigint)</h4><p id="EN-US_TOPIC_0000001188110524__p19736181284916">Sets the current value of a sequence.</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p27362124493">Return type: bigint</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p668764984919">For example:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen39321657184917"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">setval</span><span class="p">(</span><span class="s1">'seqDemo'</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="n">setval</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">1</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001188110524__section195142179507"><h4 class="sectiontitle">setval(regclass, bigint, <span id="EN-US_TOPIC_0000001188110524__text1514181713505">boolean</span>)</h4><p id="EN-US_TOPIC_0000001188110524__p1813583025018">Sets the current value of a sequence and the is_called sign.</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p151351030155011">Return type: bigint</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p113593095011">For example:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001188110524__screen245223725012"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">setval</span><span class="p">(</span><span class="s1">'seqDemo'</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="k">true</span><span class="p">);</span>
|
|
<span class="w"> </span><span class="n">setval</span>
|
|
<span class="c1">--------</span>
|
|
<span class="w"> </span><span class="mi">1</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<div class="note" id="EN-US_TOPIC_0000001188110524__note119224719509"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001188110524__p7192124775017">The current session and GTM will take effect immediately after <strong id="EN-US_TOPIC_0000001188110524__b842352706115433">setval</strong> is performed. If other sessions have buffered sequence values, <strong id="EN-US_TOPIC_0000001188110524__b71271922452">setval</strong> will take effect only after the values are used up. Therefore, to prevent sequence value conflicts, you are advised to use <strong id="EN-US_TOPIC_0000001188110524__b040245753912">setval</strong> with caution.</p>
|
|
<p id="EN-US_TOPIC_0000001188110524__p1419264713508">Because the sequence is non-transactional, changes made by <strong id="EN-US_TOPIC_0000001188110524__b84235270620373">setval</strong> will not be canceled when a transaction rolled back.</p>
|
|
</div></div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0027.html">Functions and Operators</a></div>
|
|
</div>
|
|
</div>
|
|
|