begin/end/abort work for sequences?
begin/end/abort work for sequences?
I assumed that wrapping a call to nextval() in a transaction would allow
me to roll back the update of the sequence. But when I do the following:
begin;
select nextval('po_seq');
abort;
The sequence po_seq is advancing to the next value even though the
transaction was aborted. If I put other things like updates, inserts, etc.
inside the transaction, they seem to be rolled back OK, but not the
update of the sequence.
Am I doing something wrong?
----------------------------------------------------
Kyle Bateman President, Action Target Inc.
"Viva Yo!" kyle@actarg.com (801)377-8033x101
----------------------------------------------------Re: [SQL] begin/end/abort work for sequences?
Kyle ha scritto:
> I assumed that wrapping a call to nextval() in a transaction would allow
> me to roll back the update of the sequence. But when I do the following:
>
> begin;
> select nextval('po_seq');
> abort;
>
> The sequence po_seq is advancing to the next value even though the
> transaction was aborted. If I put other things like updates, inserts, etc.
> inside the transaction, they seem to be rolled back OK, but not the
> update of the sequence.
>
> Am I doing something wrong?
No. I can't explain you why but AFAIK sequences don't rollback.
______________________________________________________________
PostgreSQL 6.5.0 on i586-pc-linux-gnu, compiled by gcc 2.7.2.3
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jose'
Re: [SQL] begin/end/abort work for sequences?
> > The sequence po_seq is advancing to the next value even though the > > transaction was aborted. If I put other things like updates, inserts, etc. > > inside the transaction, they seem to be rolled back OK, but not the > > update of the sequence. > > > > Am I doing something wrong? > > No. I can't explain you why but AFAIK sequences don't rollback. If we rolled back sequence numbers, we would have to lock the table until the transaction commited or was rolled back. That is too much locking so was not worth it. -- Bruce Momjian | http://www.op.net/~candle maillist@candle.pha.pa.us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Re: [SQL] begin/end/abort work for sequences?
Bruce Momjian wrote:> > The sequence po_seq is advancing to the next value even though the
> > transaction was aborted. If I put other things like updates, inserts, etc.
> > inside the transaction, they seem to be rolled back OK, but not the
> > update of the sequence.
> >
> > Am I doing something wrong?
>
> No. I can't explain you why but AFAIK sequences don't rollback.If we rolled back sequence numbers, we would have to lock the table
until the transaction commited or was rolled back. That is too much
locking so was not worth it.
That seems like a pretty big thing to sacrifice. Did sequence locking work before 6.5? It was my impression that it did.Do you know if there is a workaround? In my particular situation, it is critical that all instances of the sequence actually get used. If a transaction is aborted, I lose an instance and everything gets messed up.
-- ---------------------------------------------------- Kyle Bateman President, Action Target Inc. "Viva Yo!" kyle@actarg.com (801)377-8033x101 ----------------------------------------------------
Re: [SQL] begin/end/abort work for sequences?
> > If we rolled back sequence numbers, we would have to lock the table > > until the transaction commited or was rolled back. That is too much > > locking so was not worth it. > > > > That seems like a pretty big thing to sacrifice. Did sequence locking work before > 6.5? It was my impression that it did. No, sequence numbers of aborted transactions never were saved. > Do you know if there is a workaround? In my particular situation, it is critical > that all instances of the sequence actually get used. If a transaction is > aborted, I lose an instance and everything gets messed up. I recommend hand-rolling a sequence number, similar to how the sequence mechanism works, but explicitly lock your sequence table, retrieve the value, and the commit/abort will unlock the table. -- Bruce Momjian | http://www.op.net/~candle maillist@candle.pha.pa.us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026
Re: [SQL] begin/end/abort work for sequences?
Thus spake Kyle Bateman > Do you know if there is a workaround? In my particular situation, it is critical > that all instances of the sequence actually get used. If a transaction is > aborted, I lose an instance and everything gets messed up. I had to solve this once in another RDBMS (Progress) and this is how I did it. I had a table of available sequence numbers consisting of two fields, a name and a number. The table was seeded with 10 entries for each required sequence numbered 1 to 10. When I needed a sequence I started a transaction and grabbed and locked the lowest numbered record for that sequence that wasn't locked. I save the number then add 10 to it. If I abort the transaction, the number remains unchanged. Under this scheme 10 users can be working on the same sequence without locking others out. Numbers can be used out of sequence from time to time if someone aborts but they all get used eventually. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 424 2871 (DoD#0082) (eNTP) | what's for dinner.
Re: [SQL] begin/end/abort work for sequences?
"D'Arcy J.M. Cain" wrote:Thus spake Kyle BatemanThanks for taking the time to reply!
> Do you know if there is a workaround? In my particular situation, it is critical
> that all instances of the sequence actually get used. If a transaction is
> aborted, I lose an instance and everything gets messed up.I had to solve this once in another RDBMS (Progress) and this is how I
did it. I had a table of available sequence numbers consisting of two
fields, a name and a number. The table was seeded with 10 entries for
each required sequence numbered 1 to 10. When I needed a sequence I
started a transaction and grabbed and locked the lowest numbered record
for that sequence that wasn't locked. I save the number then add 10 to
it. If I abort the transaction, the number remains unchanged. Under
this scheme 10 users can be working on the same sequence without locking
others out. Numbers can be used out of sequence from time to time if
someone aborts but they all get used eventually.--
D'Arcy J.M. Cain <darcy@{druid|vex}.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 424 2871 (DoD#0082) (eNTP) | what's for dinner.-- ---------------------------------------------------- Kyle Bateman President, Action Target Inc. "Viva Yo!" kyle@actarg.com (801)377-8033x101 ----------------------------------------------------