Re: Sequence Cycle question

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "Campbell, Lance" <lance(at)illinois(dot)edu>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Sequence Cycle question
Date: 2025-01-23 18:58:53
Message-ID: CAKFQuwYugYNtWdOb4xuHa=ePY5JOb-nJYUoOVfykD8BTKVfX+w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Thu, Jan 23, 2025 at 11:47 AM Campbell, Lance <lance(at)illinois(dot)edu> wrote:

> Lets say I was to set the cycle=true. Once the IDs start back at 1, lets
> say we get to an ID of 5 where there is a duplicate.
>
> Is there a trigger or something else, that I could associate with the
> table that would "catch/detect" the insert error. If an error occurs it
> would then do a "fresh" insert with no specified ID so the sequence would
> naturally be incremented?
>
>
You are really fighting against the design of the system here. I suggest
you avoid doing inserts to this table concurrently and put logic in the
insertion code to simply find what would be the next identifier and use
it. Sequences are meant to be used for performance and simplicity - your
requirements are incompatible with both.

The better option if you can manage it is to increase your identifier
space to bigint and forget about wrap-around. Re-using identifiers is
simply not a good practice.

David J.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Mauricio Fernandez 2025-01-23 19:01:14 Re: High Availability
Previous Message Campbell, Lance 2025-01-23 18:46:52 RE: Sequence Cycle question