Re: Gaps in PK sequence numbers

From: Christophe Pettus <xof(at)thebuild(dot)com>
To: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Gaps in PK sequence numbers
Date: 2024-06-10 23:05:30
Message-ID: DB68A3C3-FCF6-4815-9C5C-A77310B66474@thebuild.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> On Jun 10, 2024, at 15:57, Rich Shepard <rshepard(at)appl-ecosys(dot)com> wrote:
> When I tried inserting new rows in the companies table psql told me that PK
> value 2310 already existed. Selecting max(PK) returned 2341. When entering
> multiple new rows is there a way to ignore gaps?

Strictly speaking, the sequence underlying nextval() has no idea what primary keys are or are not in use. It's just a transaction-ignoring counter that increases with each nextval() call. The only reason that you'd get duplicate key errors in this case are:

1. The sequence was reset to a different, lower value.
2. Rows were inserted that didn't use the sequence to select a primary key.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2024-06-10 23:49:25 Re: libpq v17 PQsocketPoll timeout is not granular enough
Previous Message David G. Johnston 2024-06-10 23:03:50 Re: Gaps in PK sequence numbers