Fixing holes/gaps in numbers generated by Postgres sequence

前端 未结 3 1374
误落风尘
误落风尘 2020-12-21 12:35

I have a postgres database that uses sequences extensively to generate primary keys of tables. After a lot of usage of this database i.e. Adding/Update/Delete operation the

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-21 13:17

    Postgres allows you to update PKs, although a lot of people think it's bad practice. So you could lock the table, and UPDATE. (You can make an oldkey, newkey table all sorts of ways, e.g., window function.) All the FK relationships have to be marked to cascade. Then you can reset the currval of the id sequence.

    Personally, I would just use a BIGSERIAL. If you have so many updates and deletes that you may run out even so, maybe there is some composite PK based on (say) a timestamp and id that would help you.

提交回复
热议问题