Sequences not affected by transactions?

后端 未结 2 827
忘掉有多难
忘掉有多难 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.

    0 讨论(0)
  • 2020-12-01 16:10

    No, there isn't. See the note at the bottom of this page. It's a bad idea to do something like that anyway. If you have two transactions running at the same time, each inserting one row, you want them to insert rows with different IDs.

    0 讨论(0)
提交回复
热议问题