Re: guaranteeing that a sequence never skips

From: "Andre Maasikas" <Andre(dot)Maasikas(at)abs(dot)ee>
To: "David Garamond" <lists(at)zara(dot)6(dot)isreserved(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: guaranteeing that a sequence never skips
Date: 2004-10-05 07:59:32
Message-ID: DBD47D010A455B46A6446A1A89D1393F6134B6@ogalik.corp.abs.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>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?
>

If id is defined unique it should be ok but if two of those statments
happen
to run concurrently you could get duplicate key violations and be
prepared
to retry the transaction. Savepoints in 8.0 will probably come on handy.

Of course concurrency is determined by the length of time you
insert a new value and commit. So it would help if you had all
other values/statements ready/done by the time you
insert into this table, and can do commit straight after
this statement.

Andre

Browse pgsql-general by date

  From Date Subject
Next Message ruben20@superguai.com 2004-10-05 08:54:27 Re: current transaction is aborted, commands ignored until
Previous Message Michael Fuhr 2004-10-05 07:29:37 Re: current transaction is aborted, commands ignored until end of transaction block