Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cd2cf74

Browse files
committed
Update sequence FAQ items, per suggestion from Pavel Stehule.
1 parent 81f285d commit cd2cf74

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed

doc/FAQ

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Frequently Asked Questions (FAQ) for PostgreSQL
33

4-
Last updated: Mon Oct 8 23:19:46 EDT 2007
4+
Last updated: Tue Oct 9 15:52:10 EDT 2007
55

66
Current maintainer: Bruce Momjian (bruce@momjian.us)
77

@@ -697,29 +697,20 @@
697697
name TEXT
698698
);
699699

700-
See the create_sequence manual page for more information about
701-
sequences.
700+
Automatically created sequence are named <table>_<serialcolumn>_seq,
701+
where table and serialcolumn are the names of the table and SERIAL
702+
column, respectively. See the create_sequence manual page for more
703+
information about sequences.
702704

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

705-
One approach is to retrieve the next SERIAL value from the sequence
706-
object with the nextval() function before inserting and then insert it
707-
explicitly. Using the example table in 4.11.1, an example in a
708-
pseudo-language would look like this:
709-
new_id = execute("SELECT nextval('person_id_seq')");
710-
execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
711-
712-
You would then also have the new value stored in new_id for use in
713-
other queries (e.g., as a foreign key to the person table). Note that
714-
the name of the automatically created SEQUENCE object will be named
715-
<table>_< serialcolumn>_seq, where table and serialcolumn are the
716-
names of your table and your SERIAL column, respectively.
717-
718-
Alternatively, you could retrieve the assigned SERIAL value with the
719-
currval() function after it was inserted by default, e.g.,
720-
execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
721-
new_id = execute("SELECT currval('person_id_seq')");
707+
The simplest way is to retrieve the assigned SERIAL value with
708+
RETURNING. Using the example table in 4.11.1, it would look like this:
709+
INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id;
722710

711+
You can also call nextval() and use that value in the INSERT, or call
712+
currval() after the INSERT.
713+
723714
4.11.3) Doesn't currval() lead to a race condition with other users?
724715

725716
No. currval() returns the current value assigned by your session, not

doc/src/FAQ/FAQ.html

+15-25
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
alink="#0000ff">
1111
<H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
1212

13-
<P>Last updated: Mon Oct 8 23:19:46 EDT 2007</P>
13+
<P>Last updated: Tue Oct 9 15:52:10 EDT 2007</P>
1414

1515
<P>Current maintainer: Bruce Momjian (<A href=
1616
"mailto:bruce@momjian.us">bruce@momjian.us</A>)
@@ -916,38 +916,28 @@ <H3 id="item4.11.1">4.11.1) How do I create a
916916
);
917917
</PRE>
918918

919-
See the <I>create_sequence</I> manual page for more information
920-
about sequences.
919+
<P>Automatically created sequence are named
920+
&lt;<I>table</I>&gt;_&lt;<I>serialcolumn</I>&gt;_<I>seq</I>, where
921+
<I>table</I> and <I>serialcolumn</I> are the names of the table and
922+
<SMALL>SERIAL</SMALL> column, respectively. See the
923+
<I>create_sequence</I> manual page for more information about
924+
sequences.</P>
921925

922926
<H3 id="item4.11.2">4.11.2) How do I get the value of a
923927
<SMALL>SERIAL</SMALL> insert?</H3>
924928

925-
<P>One approach is to retrieve the next <SMALL>SERIAL</SMALL> value
926-
from the sequence object with the <I>nextval()</I> function
927-
<I>before</I> inserting and then insert it explicitly. Using the
928-
example table in <A href="#item4.11.1">4.11.1</A>, an example in a
929-
pseudo-language would look like this:</P>
930-
<PRE>
931-
new_id = execute("SELECT nextval('person_id_seq')");
932-
execute("INSERT INTO person (id, name) VALUES (new_id, 'Blaise Pascal')");
933-
</PRE>
929+
<P>The simplest way is to retrieve the assigned <SMALL>SERIAL</SMALL>
930+
value with <SMALL>RETURNING</SMALL>. Using the example table in <A
931+
href="#item4.11.1">4.11.1</A>, it would look like this:</P>
934932

935-
You would then also have the new value stored in <CODE>new_id</CODE>
936-
for use in other queries (e.g., as a foreign key to the <CODE>person
937-
</CODE> table). Note that the name of the automatically created
938-
<SMALL>SEQUENCE</SMALL> object will be named &lt;<I>table</I>&gt;_&lt;<I>
939-
serialcolumn</I>&gt;_<I>seq</I>, where <I>table</I> and <I>serialcolumn</I>
940-
are the names of your table and your <SMALL>SERIAL</SMALL> column,
941-
respectively.
942-
943-
<P>Alternatively, you could retrieve the assigned <SMALL>SERIAL</SMALL>
944-
value with the <I>currval()</I> function <I>after</I> it was inserted by
945-
default, e.g.,</P>
946933
<PRE>
947-
execute("INSERT INTO person (name) VALUES ('Blaise Pascal')");
948-
new_id = execute("SELECT currval('person_id_seq')");
934+
INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id;
949935
</PRE>
950936

937+
You can also call <I>nextval()</I> and use that value in the
938+
<SMALL>INSERT</SMALL>, or call <I>currval()</I> <I>after</I> the
939+
<SMALL>INSERT</SMALL>.
940+
951941
<H3 id="item4.11.3">4.11.3) Doesn't <I>currval()</I>
952942
lead to a race condition with other users?</H3>
953943

0 commit comments

Comments
 (0)