Can I prevent a sequence from being updated when a transaction fail? - Mailing list pgsql-general

From Thomas Holmgren
Subject Can I prevent a sequence from being updated when a transaction fail?
Date
Msg-id Pine.GSO.4.21.0006050001190.1138-100000@luke.cs.auc.dk
Whole thread Raw
List pgsql-general
Hello All! :)

I have a sequence that I use to get an unique default value for an
attribute in one of my tables. The sequence is incremented by one each
time I insert a new tuple in the table. Excellent! :)

An example:
Given the sequence 'serial' and the relation 'test':

Table test
----------
number : integer default nextval('serial')
whatever : text

Table 'test' is constrained by having the attribute 'whatever' being a
member of the set {'foo', 'bar'}.

Now I execute the following transaction:

BEGIN;
   INSERT INTO test (whatever) values('Mars bar');
ROLLBACK;

This INSERT is obviously illegal given the constraint on attribute
'whatever'.  BUT, even after the rollback, the value of the sequence
'serial' remains incremented by the failing INSERT statement??!

How come sequences are not rolled back when a transaction is aborted, and
can I do anything to prevent a failing statement from updating the
involved sequence?


--

Thomas Holmgren
Institute for Computer Science
University of Aalborg
Denmark


pgsql-general by date:

Previous
From: Charles Tassell
Date:
Subject: Re: Compiling Error
Next
From: Haroldo Stenger
Date:
Subject: Re: Can I prevent a sequence from being updated when a transaction fail?