From: | Andrew Perrin <aperrin(at)socrates(dot)berkeley(dot)edu> |
---|---|
To: | postgresql <pgsql(at)symcom(dot)com> |
Cc: | PgSQL-SQL <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: creating "job numbers" |
Date: | 2001-03-22 13:36:56 |
Message-ID: | Pine.LNX.4.21.0103220835100.24994-100000@nujoma.perrins |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Check out nextval() and currval(). They do exactly what you need. They're
also specific to the current backend, so you can guarantee that the same
value won't be passed to two different frontend sessions.
nextval('sequencename') -> the number that will be assigned next in
the current backend; and
currval('sequencename') -> the number that was last assigned in the
current backend (undefined if there's been
no INSERT in this session)
Hope this helps.
----------------------------------------------------------------------
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology
(Soon: Asst Professor of Sociology, U of North Carolina, Chapel Hill)
andrew_perrin(at)unc(dot)edu - http://www.unc.edu/~aperrin
On Thu, 22 Mar 2001, postgresql wrote:
> I have been working with PG for about 2 months now. I am creating a
> job tracking system for my company. I have written a front end on the
> workstations (all macintoshes) that seems to be working quite well.
> However, I have a problem with a concept.
>
> In my current setup I have only one workstation that is actually
> inputting new jobs. So, I took the expedient way to create the job
> number. Ask PG to count the rows, add a magic number and insert
> this data. This all happens in one connection. What are the odds of
> two people hitting the db at the same time? In the current set up nil.
> There is only one entry computer. I want to change the system to use
> a job number generated by PG. I created a test table and I am
> playing with inserting and the sequence function works great.
> However, I am at a loss of how to pick up this next (last) job. I have
> read the docs and I still am confused. I can not first ask with the
> number will be, and asking for the previous oid after the fact can
> also lead to the same problem. so that leaves me with, 1 ask for
> that last oid from this workstation ip, or 2 since a job is inserted with
> data, I could do a select of this data after the insert (not very elegant).
>
> How are you professionals handling this problem? I like the ability to
> insert and have the system give me the number. As I grow into more
> workstations inputting the jobs I won't have to worry about chasing
> the next highest number.
>
> Thanks,
> Ted P.
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
From | Date | Subject | |
---|---|---|---|
Next Message | J.H.M. Dassen Ray | 2001-03-22 13:41:02 | Re: creating "job numbers" |
Previous Message | postgresql | 2001-03-22 13:19:03 | creating "job numbers" |