Re: guaranteeing that a sequence never skips (fwd)

From: Mike Nolan <nolan(at)gw(dot)tssi(dot)com>
To: pgsql-general(at)postgresql(dot)org (pgsql general list)
Subject: Re: guaranteeing that a sequence never skips (fwd)
Date: 2004-10-03 17:48:48
Message-ID: 200410031748.i93HmmOX019531@gw.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On Sun, 2004-10-03 at 08:58, David Garamond wrote:
> > Am I correct to assume that SERIAL does not guarantee that a sequence
> > won't skip (e.g. one successful INSERT gets 32 and the next might be 34)?
> >
> > Sometimes a business requirement is that a serial sequence never skips,
> > e.g. when generating invoice/ticket/formal letter numbers. Would an
> > INSERT INTO t (id, ...) VALUES (SELECT MAX(col)+1 FROM t, ...) suffice,
> > or must I install a trigger too to do additional checking?
>
> You will have to lock the whole table and your parallel performance will
> be poor.

Locking the table isn't sufficient to guarantee that a sequence value
never skips. What if a transaction fails and has to be rolled back?

I've written database systems that used pre-numbered checks, what's usually
necessary is to postpone the check-numbering phase until the number of
checks is finalized, so that there's not much chance of anything else
causing a rollback.
--
Mike Nolan

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Uwe C. Schroeder 2004-10-03 18:01:46 Re: guaranteeing that a sequence never skips
Previous Message Scott Marlowe 2004-10-03 17:24:28 Re: [HACKERS] OT moving from MS SQL to PostgreSQL