Re: guaranteeing that a sequence never skips - Mailing list pgsql-general

From Andre Maasikas
Subject Re: guaranteeing that a sequence never skips
Date
Msg-id DBD47D010A455B46A6446A1A89D1393F6134B6@ogalik.corp.abs.ee
Whole thread Raw
In response to guaranteeing that a sequence never skips  (David Garamond <lists@zara.6.isreserved.com>)
List 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

pgsql-general by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: current transaction is aborted, commands ignored until end of transaction block
Next
From: "ruben20@superguai.com"
Date:
Subject: Re: current transaction is aborted, commands ignored until