Update sequence FAQ items, per suggestion from Pavel Stehule.
authorBruce Momjian
Tue, 9 Oct 2007 19:55:36 +0000 (19:55 +0000)
committerBruce Momjian
Tue, 9 Oct 2007 19:55:36 +0000 (19:55 +0000)
doc/FAQ
doc/src/FAQ/FAQ.html

diff --git a/doc/FAQ b/doc/FAQ
index 9b06e5ac5c91ea1229a8a944e4d1d09884f8875c..f35ada2c58bbc0129e21db46226d686976fa7a35 100644 (file)
--- a/doc/FAQ
+++ b/doc/FAQ
@@ -1,7 +1,7 @@
 
                 Frequently Asked Questions (FAQ) for PostgreSQL
                                        
-   Last updated: Mon Oct 8 23:19:46 EDT 2007
+   Last updated: Tue Oct 9 15:52:10 EDT 2007
    
    Current maintainer: Bruce Momjian ([email protected])
    
         name TEXT
     );
 
-   See the create_sequence manual page for more information about
-   sequences.
+   Automatically created sequence are named __seq,
+   where table and serialcolumn are the names of the table and SERIAL
+   column, respectively. See the create_sequence manual page for more
+   information about sequences.
    
   4.11.2) How do I get the value of a SERIAL insert?
   
-   One approach is to retrieve the next SERIAL value from the sequence
-   object with the nextval() function before inserting and then insert it
-   explicitly. Using the example table in 4.11.1, an example in a
-   pseudo-language would look like this:
-    new_id = execute("SELECT nextval('person_id_seq')");
-    execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
-
-   You would then also have the new value stored in new_id for use in
-   other queries (e.g., as a foreign key to the person table). Note that
-   the name of the automatically created SEQUENCE object will be named
-   
_< serialcolumn>_seq, where table and serialcolumn are the
-   names of your table and your SERIAL column, respectively.
-   
-   Alternatively, you could retrieve the assigned SERIAL value with the
-   currval() function after it was inserted by default, e.g.,
-    execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
-    new_id = execute("SELECT currval('person_id_seq')");
+   The simplest way is to retrieve the assigned SERIAL value with
+   RETURNING. Using the example table in 4.11.1, it would look like this:
+    INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id;
 
+   You can also call nextval() and use that value in the INSERT, or call
+   currval() after the INSERT.
+   
   4.11.3) Doesn't currval() lead to a race condition with other users?
   
    No. currval() returns the current value assigned by your session, not
index 410c2c6f5514b790f0b39ee27bf48f9c5569520f..3a3235eaa6b3c5cada08ca871f89090bb8fabd90 100644 (file)
@@ -10,7 +10,7 @@
   alink="#0000ff">
     

Frequently Asked Questions (FAQ) for PostgreSQL

 
-    

Last updated: Mon Oct  8 23:19:46 EDT 2007

+    

Last updated: Tue Oct  9 15:52:10 EDT 2007

 
     

Current maintainer: Bruce Momjian (

@@ -916,38 +916,28 @@ length
     );
 
 
-    See the create_sequence manual page for more information
-    about sequences.
+    

Automatically created sequence are named

+    <table>_<serialcolumn>_seq, where
+    table and serialcolumn are the names of the table and
+    SERIAL column, respectively.  See the
+    create_sequence manual page for more information about
+    sequences.

 
     4.11.2) How do I get the value of a
     SERIAL insert?
 
-    

One approach is to retrieve the next SERIAL value

-    from the sequence object with the nextval() function
-    before inserting and then insert it explicitly. Using the
-    example table in 4.11.1, an example in a
-    pseudo-language would look like this:

-
-    new_id = execute("SELECT nextval('person_id_seq')");
-    execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
-
+    

The simplest way is to retrieve the assigned SERIAL

+    value with RETURNING.  Using the example table in 
+    href="#item4.11.1">4.11.1, it would look like this:

 
-    You would then also have the new value stored in new_id
-    for use in other queries (e.g., as a foreign key to the person
-     table). Note that the name of the automatically created
-    SEQUENCE object will be named <table>_<
-    serialcolumn>_seq, where table and serialcolumn
-    are the names of your table and your SERIAL column,
-    respectively.
-
-    

Alternatively, you could retrieve the assigned SERIAL

-    value with the currval() function after it was inserted by
-    default, e.g.,

 
-    execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
-    new_id = execute("SELECT currval('person_id_seq')");
+    INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id;
 
 
+    You can also call nextval() and use that value in the
+    INSERT, or call currval() after the
+    INSERT.
+    
     4.11.3) Doesn't currval()
     lead to a race condition with other users?