pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedSERIAL Field
12+ messages / 7 participants
[nested] [flat]
* SERIAL Field
@ 2002-05-06 10:46 Gaetano Mendola <mendola@bigfoot.com>
0 siblings, 2 replies; 12+ messages in thread
From: Gaetano Mendola @ 2002-05-06 10:46 UTC (permalink / raw)
To: pgsql-admin
Hi all,
having a table with a serial field what is the corrected method
to retrieve the value after an insert ?
Ciao
Gaetano
--
#exclude <windows>
#include <CSRSS>
printf("\t\t\b\b\b\b\b\b");.
printf("\t\t\b\b\b\b\b\b");
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 12:38 Joel Burton <joel@joelburton.com>
parent: Gaetano Mendola <mendola@bigfoot.com>
1 sibling, 0 replies; 12+ messages in thread
From: Joel Burton @ 2002-05-06 12:38 UTC (permalink / raw)
To: Gaetano Mendola <mendola@bigfoot.com>; pgsql-admin
> Hi all,
>
> having a table with a serial field what is the corrected method
> to retrieve the value after an insert ?
Use currval(_name_of_sequence_):
joel=# create table test ( id serial );
NOTICE: CREATE TABLE will create implicit sequence 'test_id_seq' for SERIAL
column 'test.id'
NOTICE: CREATE TABLE / UNIQUE will create implicit index 'test_id_key' for
table 'test'
joel=# insert into test values (default);
INSERT 16617 1
joel=# select currval('test_id_seq');
currval
---------
1
(1 row)
See also nextval() and setval() for other functions for sequences.
Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 13:26 Rajesh Kumar Mallah. <mallah@trade-india.com>
parent: Gaetano Mendola <mendola@bigfoot.com>
1 sibling, 2 replies; 12+ messages in thread
From: Rajesh Kumar Mallah. @ 2002-05-06 13:26 UTC (permalink / raw)
To: Gaetano Mendola <mendola@bigfoot.com>; pgsql-admin
Hi Gaetano,
Postgresql implements serial field using sequences.
detailed description of sequences are avaliable at
http://www.postgresql.org/idocs/index.php?functions-sequence.html
a NICE flash tutorial is also available at:
http://techdocs.postgresql.org/college/001_sequences/
the specific answer to your question is the function "currval(sequence_name)"
can be used.
you can get the name of sequenced being used for the serial field by
describing the table (\d tablename).
But the very idea of inserting the value and then retriving the sequence
number sounds fishy to me. Why not get the sequence number first and than
insert using that seqnce number (more on the flash tutorial ).
regds
mallah.
On Monday 06 May 2002 04:16 pm, Gaetano Mendola wrote:
> Hi all,
>
> having a table with a serial field what is the corrected method
> to retrieve the value after an insert ?
>
>
>
>
> Ciao
> Gaetano
--
Rajesh Kumar Mallah,
Project Manager (Development)
Infocom Network Limited, New Delhi
phone: +91(11)6152172 (221) (L) ,9811255597 (M)
Visit http://www.trade-india.com ,
India's Leading B2B eMarketplace.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 14:51 Joel Burton <joel@joelburton.com>
parent: Rajesh Kumar Mallah. <mallah@trade-india.com>
1 sibling, 1 reply; 12+ messages in thread
From: Joel Burton @ 2002-05-06 14:51 UTC (permalink / raw)
To: Rajesh Kumar Mallah. <mallah@trade-india.com>; Gaetano Mendola <mendola@bigfoot.com>; pgsql-admin
> -----Original Message-----
> From: pgsql-admin-owner@postgresql.org
> [mailto:pgsql-admin-owner@postgresql.org]On Behalf Of Rajesh Kumar
> Mallah.
> Sent: Monday, May 06, 2002 9:26 AM
> To: Gaetano Mendola; pgsql-admin@postgresql.org
> Subject: Re: [ADMIN] SERIAL Field
> But the very idea of inserting the value and then retriving the sequence
> number sounds fishy to me. Why not get the sequence number first and than
> insert using that seqnce number (more on the flash tutorial ).
Hmmm... does nextval() 'hold' a sequence number for this backend? Otherwise,
the risk would be that you request a value, and someone else has beat you to
it before you insert it.
In any event, inserting then using currval() is the standard practice around
here, and it works great. Nothing fishy at all here, nothing to see, move
on.
J.
Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 14:57 Joel Burton <joel@joelburton.com>
parent: Joel Burton <joel@joelburton.com>
0 siblings, 0 replies; 12+ messages in thread
From: Joel Burton @ 2002-05-06 14:57 UTC (permalink / raw)
To: Joel Burton <joel@joelburton.com>; Rajesh Kumar Mallah. <mallah@trade-india.com>; Gaetano Mendola <mendola@bigfoot.com>; pgsql-admin
> -----Original Message-----
> From: Joel Burton [mailto:joel@joelburton.com]
> Sent: Monday, May 06, 2002 10:51 AM
> To: Rajesh Kumar Mallah.; Gaetano Mendola; pgsql-admin@postgresql.org
> Subject: RE: [ADMIN] SERIAL Field
>
>
> > -----Original Message-----
> > From: pgsql-admin-owner@postgresql.org
> > [mailto:pgsql-admin-owner@postgresql.org]On Behalf Of Rajesh Kumar
> > Mallah.
> > Sent: Monday, May 06, 2002 9:26 AM
> > To: Gaetano Mendola; pgsql-admin@postgresql.org
> > Subject: Re: [ADMIN] SERIAL Field
>
> > But the very idea of inserting the value and then retriving the
> sequence
> > number sounds fishy to me. Why not get the sequence number
> first and than
> > insert using that seqnce number (more on the flash tutorial ).
>
> Hmmm... does nextval() 'hold' a sequence number for this backend?
> Otherwise, the risk would be that you request a value, and
> someone else has beat you to it before you insert it.
Duh. Of course it does. Forgive me: it's still a bit early here in EST for
my brain to be on.
> In any event, inserting then using currval() is the standard
> practice around here, and it works great. Nothing fishy at all
> here, nothing to see, move on.
>
> J.
>
> Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
> Knowledge Management & Technology Consultant
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 16:41 Dan Langille <dan@langille.org>
parent: Rajesh Kumar Mallah. <mallah@trade-india.com>
1 sibling, 2 replies; 12+ messages in thread
From: Dan Langille @ 2002-05-06 16:41 UTC (permalink / raw)
To: Joel Burton <joel@joelburton.com>; +Cc: pgsql-admin
On 6 May 2002 at 10:51, Joel Burton wrote:
> > But the very idea of inserting the value and then retriving the sequence
> > number sounds fishy to me. Why not get the sequence number first and than
> > insert using that seqnce number (more on the flash tutorial ).
That's pretty standard in a number of database applications (i.e. not just
PostgreSQL situations).
> Hmmm... does nextval() 'hold' a sequence number for this backend?
> Otherwise, the risk would be that you request a value, and someone else has
> beat you to it before you insert it.
I use nextval all the time. In fact:
fp2migration=# \d users
Column | Type | Modifiers
------------+--------------------------+--------------------------
id | integer | not null default nextval('users_id_seq'::text)
What's wrong this this? Nobody is going to "beat me to it".
> In any event, inserting then using currval() is the standard practice
> around here, and it works great. Nothing fishy at all here, nothing to see,
> move on.
Why is that "less risk"?
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 16:43 Joel Burton <joel@joelburton.com>
parent: Dan Langille <dan@langille.org>
1 sibling, 0 replies; 12+ messages in thread
From: Joel Burton @ 2002-05-06 16:43 UTC (permalink / raw)
To: dan@langille.org; +Cc: pgsql-admin
> -----Original Message-----
> From: Dan Langille [mailto:dan@langille.org]
> Sent: Monday, May 06, 2002 12:41 PM
> To: Joel Burton
> Cc: pgsql-admin@postgresql.org
> Subject: Re: [ADMIN] SERIAL Field
>
> > In any event, inserting then using currval() is the standard practice
> > around here, and it works great. Nothing fishy at all here,
> nothing to see,
> > move on.
>
> Why is that "less risk"?
It's not; nextval() is just fine. I was half asleep and thinking, I think,
of another database when I wrote that.
"You say nextval() + write, I say write + currval()..."
J.
Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 18:25 Dan Langille <dan@langille.org>
parent: Dan Langille <dan@langille.org>
1 sibling, 1 reply; 12+ messages in thread
From: Dan Langille @ 2002-05-06 18:25 UTC (permalink / raw)
To: Joel Burton <joel@joelburton.com>; +Cc: pgsql-admin
On 6 May 2002 at 12:43, Joel Burton wrote:
> > -----Original Message-----
> > From: Dan Langille [mailto:dan@langille.org]
> > Sent: Monday, May 06, 2002 12:41 PM
> > To: Joel Burton
> > Cc: pgsql-admin@postgresql.org
> > Subject: Re: [ADMIN] SERIAL Field
> >
> > > In any event, inserting then using currval() is the standard practice
> > > around here, and it works great. Nothing fishy at all here,
> > nothing to see,
> > > move on.
> >
> > Why is that "less risk"?
>
> It's not; nextval() is just fine. I was half asleep and thinking, I think,
> of another database when I wrote that.
>
> "You say nextval() + write, I say write + currval()..."
So long as a given application does not mix the two approaches, everything
should be fine.
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 18:49 Brian McCane <bmccane@mccons.net>
0 siblings, 0 replies; 12+ messages in thread
From: Brian McCane @ 2002-05-06 18:49 UTC (permalink / raw)
To: pgsql-admin
My personal experience is that you MUST operate like:
SELECT nextval...
INSERT ...
I have several web sites that have many simultaneous updates occurring,
based on user interaction with the website and its underlying data
structures. I used to INSERT, then SELECT currval, and depending on
timing, I might get a different sequence number than the one I actually
used. Apparently I had the following occurring:
INSERT ...
INSERT ...
SELECT currval...
SELECT currval...
In this situation, both of the SELECTs return the same sequence number.
So basically, user1 would update user2's "myarea" data. This problem
became much more critical when I went to multiple Apache servers on the
front end and had a higher likelihood of concurrency. However, even on a
single server, you can't guarantee where the task switch will occur and
mess you up.
- brian
On Mon, 6 May 2002, Joel Burton wrote:
>
> > -----Original Message-----
> > From: Dan Langille [mailto:dan@langille.org]
> > Sent: Monday, May 06, 2002 12:41 PM
> > To: Joel Burton
> > Cc: pgsql-admin@postgresql.org
> > Subject: Re: [ADMIN] SERIAL Field
> >
> > > In any event, inserting then using currval() is the standard practice
> > > around here, and it works great. Nothing fishy at all here,
> > nothing to see,
> > > move on.
> >
> > Why is that "less risk"?
>
> It's not; nextval() is just fine. I was half asleep and thinking, I think,
> of another database when I wrote that.
>
> "You say nextval() + write, I say write + currval()..."
>
> J.
>
> Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
> Knowledge Management & Technology Consultant
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
>
Wm. Brian McCane | Life is full of doors that won't open
Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 19:29 Joel Stevenson <jstevenson@bearriver.com>
parent: Dan Langille <dan@langille.org>
0 siblings, 1 reply; 12+ messages in thread
From: Joel Stevenson @ 2002-05-06 19:29 UTC (permalink / raw)
To: pgsql-admin
> > "You say nextval() + write, I say write + currval()..."
>
>So long as a given application does not mix the two approaches, everything
>should be fine.
Though, if you have multiple clients inserting into the table, there
is a chance that between client A inserting the record and then
calling currval(), client B could have performed an insert, in which
case client A gets the value of client B's record. 'nextval() +
write' avoids this possible overlap.
--
Joel Stevenson
Bear River Associates, Inc.
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 20:16 Tom Lane <tgl@sss.pgh.pa.us>
parent: Joel Stevenson <jstevenson@bearriver.com>
0 siblings, 1 reply; 12+ messages in thread
From: Tom Lane @ 2002-05-06 20:16 UTC (permalink / raw)
To: Joel Stevenson <jstevenson@bearriver.com>; +Cc: pgsql-admin
Joel Stevenson <jstevenson@bearriver.com> writes:
> Though, if you have multiple clients inserting into the table, there
> is a chance that between client A inserting the record and then
> calling currval(), client B could have performed an insert, in which
> case client A gets the value of client B's record. 'nextval() +
> write' avoids this possible overlap.
People who have not studied the documentation frequently make that
claim. *There is no such risk* (unless A and B are interleaving their
commands on a single database connection, in which case they have lots
worse risks of conflicts than this one). See
http://www.ca.postgresql.org/users-lounge/docs/7.2/postgres/functions-sequence.html
regards, tom lane
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: SERIAL Field
@ 2002-05-06 20:57 Joel Stevenson <jstevenson@bearriver.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 12+ messages in thread
From: Joel Stevenson @ 2002-05-06 20:57 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-admin
>
>People who have not studied the documentation frequently make that
>claim. *There is no such risk* (unless A and B are interleaving their
>commands on a single database connection, in which case they have lots
>worse risks of conflicts than this one). See
>http://www.ca.postgresql.org/users-lounge/docs/7.2/postgres/functions-sequence.html
>
> regards, tom lane
Point taken. I've worked on web-apps which use a single JDBC
connection per user session where the overlap could occur, but as you
say there are far greater issues involved for such session-based
connections.
--
Joel Stevenson
Bear River Associates, Inc.
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2002-05-06 20:57 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-05-06 10:46 SERIAL Field Gaetano Mendola <mendola@bigfoot.com>
2002-05-06 12:38 ` Joel Burton <joel@joelburton.com>
2002-05-06 13:26 ` Rajesh Kumar Mallah. <mallah@trade-india.com>
2002-05-06 14:51 ` Joel Burton <joel@joelburton.com>
2002-05-06 14:57 ` Joel Burton <joel@joelburton.com>
2002-05-06 16:41 ` Dan Langille <dan@langille.org>
2002-05-06 16:43 ` Joel Burton <joel@joelburton.com>
2002-05-06 18:25 ` Dan Langille <dan@langille.org>
2002-05-06 19:29 ` Joel Stevenson <jstevenson@bearriver.com>
2002-05-06 20:16 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-05-06 20:57 ` Joel Stevenson <jstevenson@bearriver.com>
2002-05-06 18:49 Re: SERIAL Field Brian McCane <bmccane@mccons.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox