Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > a866202fe868538f89a755dbcabc378b > files > 181

postgresql8.2-docs-8.2.14-1mdv2010.0.i586.rpm

<!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 8.2.14 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Functions and Operators"
HREF="functions.html"><LINK
REL="PREVIOUS"
TITLE="Network Address Functions and Operators"
HREF="functions-net.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="2009-09-04T05:25:47"></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"
>PostgreSQL 8.2.14 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions-net.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="functions.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 9. Functions and Operators</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="functions.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
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.12. Sequence Manipulation Functions</A
></H1
><A
NAME="AEN12892"
></A
><A
NAME="AEN12894"
></A
><A
NAME="AEN12896"
></A
><A
NAME="AEN12898"
></A
><A
NAME="AEN12900"
></A
><P
>   This section describes <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>'s functions
   for operating on <I
CLASS="FIRSTTERM"
>sequence objects</I
>.
   Sequence objects (also called sequence generators or
   just sequences) are special single-row tables created with
   <TT
CLASS="COMMAND"
>CREATE SEQUENCE</TT
>.  A sequence object is usually 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-34</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-34. 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</CODE
>(<TT
CLASS="TYPE"
>regclass</TT
>)</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"
>nextval</CODE
>(<TT
CLASS="TYPE"
>regclass</TT
>)</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</CODE
>(<TT
CLASS="TYPE"
>regclass</TT
>, <TT
CLASS="TYPE"
>bigint</TT
>)</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</CODE
>(<TT
CLASS="TYPE"
>regclass</TT
>, <TT
CLASS="TYPE"
>bigint</TT
>, <TT
CLASS="TYPE"
>boolean</TT
>)</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 call is specified by
   a <TT
CLASS="TYPE"
>regclass</TT
> argument, which is just 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.  To
   achieve some compatibility with the handling of ordinary
   <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> names, the string will be converted to lowercase
   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.12</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 backwards 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 will
    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
    may 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
></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.)  Notice that 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 that <CODE
CLASS="FUNCTION"
>nextval</CODE
>
        was used on 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.  In the three-parameter form,
        <TT
CLASS="LITERAL"
>is_called</TT
> may be set either <TT
CLASS="LITERAL"
>true</TT
> or
        <TT
CLASS="LITERAL"
>false</TT
>.  If it's 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
>.  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
></DD
></DL
></DIV
><P>
  </P
><P
>   If a sequence object has been created with default parameters,
   <CODE
CLASS="FUNCTION"
>nextval</CODE
> calls on it will return successive values
   beginning with 1.  Other behaviors can be obtained by using
   special parameters in the <A
HREF="sql-createsequence.html"
><I
>CREATE SEQUENCE</I
></A
> command;
   see its command reference page for more information.
  </P
><DIV
CLASS="IMPORTANT"
><BLOCKQUOTE
CLASS="IMPORTANT"
><P
><B
>Important: </B
>    To avoid blocking of 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 may leave unused <SPAN
CLASS="QUOTE"
>"holes"</SPAN
> in the
    sequence of assigned values.  <CODE
CLASS="FUNCTION"
>setval</CODE
> operations are never
    rolled back, either.
   </P
></BLOCKQUOTE
></DIV
></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-net.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"
>Network Address Functions and Operators</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
>