高并发(压力测试)下,

发现postgresql数据库中出现自增长的ID断层的情况,如下所示:

ID

1
2
3
4
7
8
9

  

少了ID为5和6的数据,经过确认,是postgresql对高并发处理机制导致的,

在pg中,每次新增数据,都会将一个ID锁定到这条数据上,如果这条数据因为某些原因插入失败(主键重复等),那么这个ID也不会再赋予新的数据,因此出现ID跳过的现象,官方解释:

If transactions rollback, the serial value assigned during the rolled
back transaction is skipped. This has been discussed many times, it's
a tradeoff between losing some #s now and again and taking a huge
performance and code complexity hit to avoid it.

If you absolutely need consecutive #s, then serial is not for you and
you should implement your own method of acquiring sequential numbers.

 

 

posted on 2022-04-02 15:43  Boom__Clap  阅读(129)  评论(0编辑  收藏  举报