Sequences not affected by transactions?

后端 未结 2 915
忘掉有多难
忘掉有多难 2020-12-01 15:46

I have a table

create table testtable(
  testtable_rid serial not null,
  data integer not null,
  constraint pk_testtable primary key(testtable_rid)
);
         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-01 16:03

    It would not be a good idea to rollback sequences. Imagine two transactions happening at the same time, each of which uses the sequence for a unique id. If the second transaction commits and the first transaction rolls back, then the second inserted a row with "2" while the first rolls the sequence back to "1".

    If that sequence is then used again, the value of the sequence will become "2" which could lead to a unique constraint problem.

提交回复
热议问题