Re: Sequence Cycle question

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Campbell, Lance" <lance(at)illinois(dot)edu>
Cc: "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Sequence Cycle question
Date: 2025-01-23 17:41:40
Message-ID: 202380.1737654100@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

"Campbell, Lance" <lance(at)illinois(dot)edu> writes:
> Table X has records that have been removed over time randomly. There are IDs that cover a wide range of values between 1 and 1,000,000.
> When the primary key ID, which is a sequence, reaches 1,000,000 then the next sequence value will start back at 1.
> What would happen if I had a primary key for ID of 5 still in use? When I reach 5 will the sequence skip that number and go to 6 instead?

No, the sequence has no idea about what is in the table. It will
generate "5" when it's time to, and then your insert will get a
duplicate-key violation.

You could work around that by retrying the insert, but it might
be better to reconsider whether you want a cycling sequence
for this application.

regards, tom lane

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message msalais 2025-01-23 18:35:44 RE: Commit with wait event on advisory lock!
Previous Message Campbell, Lance 2025-01-23 17:13:05 Sequence Cycle question