Server IP : 103.119.228.120 / Your IP : 3.139.108.99 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/share/doc/postgresql-9.2.24/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Sequence Manipulation Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.2.24 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Functions and Operators" HREF="functions.html"><LINK REL="PREVIOUS" TITLE="JSON Functions" HREF="functions-json.html"><LINK REL="NEXT" TITLE="Conditional Expressions" HREF="functions-conditional.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2017-11-06T22:43:11"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="JSON Functions" HREF="functions-json.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="functions.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 9. Functions and Operators</TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="Conditional Expressions" HREF="functions-conditional.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="FUNCTIONS-SEQUENCE" >9.16. Sequence Manipulation Functions</A ></H1 ><P > This section describes functions for operating on <I CLASS="FIRSTTERM" >sequence objects</I >, also called sequence generators or just sequences. Sequence objects are special single-row tables created with <A HREF="sql-createsequence.html" >CREATE SEQUENCE</A >. Sequence objects are commonly used to generate unique identifiers for rows of a table. The sequence functions, listed in <A HREF="functions-sequence.html#FUNCTIONS-SEQUENCE-TABLE" >Table 9-41</A >, provide simple, multiuser-safe methods for obtaining successive sequence values from sequence objects. </P ><DIV CLASS="TABLE" ><A NAME="FUNCTIONS-SEQUENCE-TABLE" ></A ><P ><B >Table 9-41. Sequence Functions</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Function</TH ><TH >Return Type</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="LITERAL" ><CODE CLASS="FUNCTION" >currval(<TT CLASS="TYPE" >regclass</TT >)</CODE ></TT ></TD ><TD ><TT CLASS="TYPE" >bigint</TT ></TD ><TD >Return value most recently obtained with <CODE CLASS="FUNCTION" >nextval</CODE > for specified sequence</TD ></TR ><TR ><TD ><TT CLASS="LITERAL" ><CODE CLASS="FUNCTION" >lastval()</CODE ></TT ></TD ><TD ><TT CLASS="TYPE" >bigint</TT ></TD ><TD >Return value most recently obtained with <CODE CLASS="FUNCTION" >nextval</CODE > for any sequence</TD ></TR ><TR ><TD ><TT CLASS="LITERAL" ><CODE CLASS="FUNCTION" >nextval(<TT CLASS="TYPE" >regclass</TT >)</CODE ></TT ></TD ><TD ><TT CLASS="TYPE" >bigint</TT ></TD ><TD >Advance sequence and return new value</TD ></TR ><TR ><TD ><TT CLASS="LITERAL" ><CODE CLASS="FUNCTION" >setval(<TT CLASS="TYPE" >regclass</TT >, <TT CLASS="TYPE" >bigint</TT >)</CODE ></TT ></TD ><TD ><TT CLASS="TYPE" >bigint</TT ></TD ><TD >Set sequence's current value</TD ></TR ><TR ><TD ><TT CLASS="LITERAL" ><CODE CLASS="FUNCTION" >setval(<TT CLASS="TYPE" >regclass</TT >, <TT CLASS="TYPE" >bigint</TT >, <TT CLASS="TYPE" >boolean</TT >)</CODE ></TT ></TD ><TD ><TT CLASS="TYPE" >bigint</TT ></TD ><TD >Set sequence's current value and <TT CLASS="LITERAL" >is_called</TT > flag</TD ></TR ></TBODY ></TABLE ></DIV ><P > The sequence to be operated on by a sequence function is specified by a <TT CLASS="TYPE" >regclass</TT > argument, which is simply the OID of the sequence in the <TT CLASS="STRUCTNAME" >pg_class</TT > system catalog. You do not have to look up the OID by hand, however, since the <TT CLASS="TYPE" >regclass</TT > data type's input converter will do the work for you. Just write the sequence name enclosed in single quotes so that it looks like a literal constant. For compatibility with the handling of ordinary <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM > names, the string will be converted to lower case unless it contains double quotes around the sequence name. Thus: </P><PRE CLASS="PROGRAMLISTING" >nextval('foo') <I CLASS="LINEANNOTATION" >operates on sequence <TT CLASS="LITERAL" >foo</TT ></I > nextval('FOO') <I CLASS="LINEANNOTATION" >operates on sequence <TT CLASS="LITERAL" >foo</TT ></I > nextval('"Foo"') <I CLASS="LINEANNOTATION" >operates on sequence <TT CLASS="LITERAL" >Foo</TT ></I ></PRE ><P> The sequence name can be schema-qualified if necessary: </P><PRE CLASS="PROGRAMLISTING" >nextval('myschema.foo') <I CLASS="LINEANNOTATION" >operates on <TT CLASS="LITERAL" >myschema.foo</TT ></I > nextval('"myschema".foo') <I CLASS="LINEANNOTATION" >same as above</I > nextval('foo') <I CLASS="LINEANNOTATION" >searches search path for <TT CLASS="LITERAL" >foo</TT ></I ></PRE ><P> See <A HREF="datatype-oid.html" >Section 8.18</A > for more information about <TT CLASS="TYPE" >regclass</TT >. </P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > Before <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 8.1, the arguments of the sequence functions were of type <TT CLASS="TYPE" >text</TT >, not <TT CLASS="TYPE" >regclass</TT >, and the above-described conversion from a text string to an OID value would happen at run time during each call. For backward compatibility, this facility still exists, but internally it is now handled as an implicit coercion from <TT CLASS="TYPE" >text</TT > to <TT CLASS="TYPE" >regclass</TT > before the function is invoked. </P ><P > When you write the argument of a sequence function as an unadorned literal string, it becomes a constant of type <TT CLASS="TYPE" >regclass</TT >. Since this is really just an OID, it will track the originally identified sequence despite later renaming, schema reassignment, etc. This <SPAN CLASS="QUOTE" >"early binding"</SPAN > behavior is usually desirable for sequence references in column defaults and views. But sometimes you might want <SPAN CLASS="QUOTE" >"late binding"</SPAN > where the sequence reference is resolved at run time. To get late-binding behavior, force the constant to be stored as a <TT CLASS="TYPE" >text</TT > constant instead of <TT CLASS="TYPE" >regclass</TT >: </P><PRE CLASS="PROGRAMLISTING" >nextval('foo'::text) <I CLASS="LINEANNOTATION" ><TT CLASS="LITERAL" >foo</TT > is looked up at runtime</I ></PRE ><P> Note that late binding was the only behavior supported in <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > releases before 8.1, so you might need to do this to preserve the semantics of old applications. </P ><P > Of course, the argument of a sequence function can be an expression as well as a constant. If it is a text expression then the implicit coercion will result in a run-time lookup. </P ></BLOCKQUOTE ></DIV ><P > The available sequence functions are: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><CODE CLASS="FUNCTION" >nextval</CODE ></DT ><DD ><P > Advance the sequence object to its next value and return that value. This is done atomically: even if multiple sessions execute <CODE CLASS="FUNCTION" >nextval</CODE > concurrently, each will safely receive a distinct sequence value. </P ><P > If a sequence object has been created with default parameters, successive <CODE CLASS="FUNCTION" >nextval</CODE > calls will return successive values beginning with 1. Other behaviors can be obtained by using special parameters in the <A HREF="sql-createsequence.html" >CREATE SEQUENCE</A > command; see its command reference page for more information. </P ><DIV CLASS="IMPORTANT" ><BLOCKQUOTE CLASS="IMPORTANT" ><P ><B >Important: </B > To avoid blocking concurrent transactions that obtain numbers from the same sequence, a <CODE CLASS="FUNCTION" >nextval</CODE > operation is never rolled back; that is, once a value has been fetched it is considered used, even if the transaction that did the <CODE CLASS="FUNCTION" >nextval</CODE > later aborts. This means that aborted transactions might leave unused <SPAN CLASS="QUOTE" >"holes"</SPAN > in the sequence of assigned values. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><CODE CLASS="FUNCTION" >currval</CODE ></DT ><DD ><P > Return the value most recently obtained by <CODE CLASS="FUNCTION" >nextval</CODE > for this sequence in the current session. (An error is reported if <CODE CLASS="FUNCTION" >nextval</CODE > has never been called for this sequence in this session.) Because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed <CODE CLASS="FUNCTION" >nextval</CODE > since the current session did. </P ></DD ><DT ><CODE CLASS="FUNCTION" >lastval</CODE ></DT ><DD ><P > Return the value most recently returned by <CODE CLASS="FUNCTION" >nextval</CODE > in the current session. This function is identical to <CODE CLASS="FUNCTION" >currval</CODE >, except that instead of taking the sequence name as an argument it fetches the value of the last sequence used by <CODE CLASS="FUNCTION" >nextval</CODE > in the current session. It is an error to call <CODE CLASS="FUNCTION" >lastval</CODE > if <CODE CLASS="FUNCTION" >nextval</CODE > has not yet been called in the current session. </P ></DD ><DT ><CODE CLASS="FUNCTION" >setval</CODE ></DT ><DD ><P > Reset the sequence object's counter value. The two-parameter form sets the sequence's <TT CLASS="LITERAL" >last_value</TT > field to the specified value and sets its <TT CLASS="LITERAL" >is_called</TT > field to <TT CLASS="LITERAL" >true</TT >, meaning that the next <CODE CLASS="FUNCTION" >nextval</CODE > will advance the sequence before returning a value. The value reported by <CODE CLASS="FUNCTION" >currval</CODE > is also set to the specified value. In the three-parameter form, <TT CLASS="LITERAL" >is_called</TT > can be set to either <TT CLASS="LITERAL" >true</TT > or <TT CLASS="LITERAL" >false</TT >. <TT CLASS="LITERAL" >true</TT > has the same effect as the two-parameter form. If it is set to <TT CLASS="LITERAL" >false</TT >, the next <CODE CLASS="FUNCTION" >nextval</CODE > will return exactly the specified value, and sequence advancement commences with the following <CODE CLASS="FUNCTION" >nextval</CODE >. Furthermore, the value reported by <CODE CLASS="FUNCTION" >currval</CODE > is not changed in this case (this is a change from pre-8.3 behavior). For example, </P><PRE CLASS="SCREEN" >SELECT setval('foo', 42); <I CLASS="LINEANNOTATION" >Next <CODE CLASS="FUNCTION" >nextval</CODE > will return 43</I > SELECT setval('foo', 42, true); <I CLASS="LINEANNOTATION" >Same as above</I > SELECT setval('foo', 42, false); <I CLASS="LINEANNOTATION" >Next <CODE CLASS="FUNCTION" >nextval</CODE > will return 42</I ></PRE ><P> The result returned by <CODE CLASS="FUNCTION" >setval</CODE > is just the value of its second argument. </P ><DIV CLASS="IMPORTANT" ><BLOCKQUOTE CLASS="IMPORTANT" ><P ><B >Important: </B > Because sequences are non-transactional, changes made by <CODE CLASS="FUNCTION" >setval</CODE > are not undone if the transaction rolls back. </P ></BLOCKQUOTE ></DIV ></DD ></DL ></DIV ><P> </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="functions-json.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="functions-conditional.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >JSON Functions</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="functions.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Conditional Expressions</TD ></TR ></TABLE ></DIV ></BODY ></HTML >