pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: postgresql <pgsql@symcom.com>
To: PgSQL-SQL <pgsql-sql@postgresql.org>
Subject: Re: creating "job numbers"
Date: Thu, 22 Mar 2001 13:59:48 -0000
Message-ID: <004201c0b2d8$5d3c8020$1001a8c0@archonet.com> (raw)
References: <200103221311.f2MDB0t56270@mail.postgresql.org>

From: "postgresql" <pgsql@symcom.com>

> 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).

I wouldn't use oid's for this - create a jobnum field and use a sequence.

Sequences are smarter than you think, use:
select currval('mysequence') to get the current value and
select nextval('mysequence') to get the next value *for this backend*

So - each client will be guaranteed a unique number. Note that if you "use
up" a number and e.g. an insert fails there will be gaps in your numbering.

Also check out the SERIAL data-type which can provide automatic numbering
for the fields.

I'm sure there are examples in Bruce's book (there's a link on
www.postgresql.org)

- Richard Huxton




view thread (9+ messages)  latest in thread

Message-ID: <004201c0b2d8$5d3c8020$1001a8c0@archonet.com>
Permalink:  ../004201c0b2d8$5d3c8020$1001a8c0@archonet.com/
Also on:    postgresql.org/message-id/004201c0b2d8$5d3c8020$1001a8c0@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, pgsql@symcom.com
  Subject: Re: creating "job numbers"
  In-Reply-To: <004201c0b2d8$5d3c8020$1001a8c0@archonet.com>
  References: <200103221311.f2MDB0t56270@mail.postgresql.org> <004201c0b2d8$5d3c8020$1001a8c0@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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