Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGo41-00HPFo-Hc for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 23:04:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sGo3z-00HIFO-PS for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 23:04:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGo3z-00HIEj-EV for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 23:04:32 +0000 Received: from mail-oo1-xc2d.google.com ([2607:f8b0:4864:20::c2d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGo3y-0010Y6-6F for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 23:04:31 +0000 Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-5b5254f9c32so2596100eaf.0 for ; Mon, 10 Jun 2024 16:04:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718060668; x=1718665468; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=XB9A6/qs9dc4F/991KYsFkwtmg8l5nG0Uk1lSCYy5R8=; b=L6pKJ2z+cWhDM5PMQVhO49AbvDWRwXzhPkSGQzCSi4TaVlUU8f0hnuZ2j3PADV/QFD WIeSZj5lLfk3b5TyTuw3sSZy6F25QlhjVs+lMql6PF2e0WxUAhSQAWgeb8sbBzzd6GIx WhUT/bmoXbd4g95fOS5m/2Pjn0Y2z7N45zGFiVLs1JPue5Wv9z+rjVACFFuXmYeXuZdk 6aTTj9LlBa+kFUFICVzpCUB1/d1mUusxQpmacBuJ73CmKMEvaWYxsK8eUVAf1zu1ZIaX nMu6hK3kfLKZFfUpHu+Lt616W7J65bpW6dXTQPl50cmPGCDvtRKOeI5KbeC3OQFhZSTL GKkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718060668; x=1718665468; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XB9A6/qs9dc4F/991KYsFkwtmg8l5nG0Uk1lSCYy5R8=; b=BFi40PFBM8cM430SYjWRUgQRZ9COfIAhtSclI5Mb9evx1BraBKCLYGHam4g/NBp7Tq nDIdwI4GNOsUBFbC14Tr2WDWjpU1FLLtnQqu70O67LMqzdz08qFuiVwKtvVBwq27DCRW 0GI5YrVTXR0xVaN+fVoYZPDd3tdsWuGuhSB/fggxOWhG6dvyHCqNsVzkvR74+350q1Sj xpCjDzkgik7F18/JcfOdb7U1rVEF5pcd88rgOFq4l+hokMqCYUJmsxqjdiyzVa8Afa7b Pyb1Fc1sT+acoozM7nmROCHRN9yQD9xmT22QdiQvDJR8BHR+evagdDvvxHgjfu5HNS13 VFPw== X-Gm-Message-State: AOJu0YyDISCVc9FutlUiRwI6V6R7VjXU3/AaO8ZNzOXQxegmGJbchGM6 keW1/zCLR7secIg48TgbpUPBqkFZce3ogkfPRqSxdE+3JnOy4dvMoCA6a9sgMy4F2hJ11Pa9+ws mME6ckJMElo6ZDabnFJBcABJeBm6fEg== X-Google-Smtp-Source: AGHT+IEuMR+XNe+FexzDdS4x3c/x7fl/7wwLdWvwfiB5P8XP2vClW7/xUF8CKsWK/tRP2JJhZAepIyoM4VfqlS2rVWQ= X-Received: by 2002:a05:6820:603:b0:5ba:71ce:f82c with SMTP id 006d021491bc7-5baae52e647mr11474793eaf.3.1718060668617; Mon, 10 Jun 2024 16:04:28 -0700 (PDT) MIME-Version: 1.0 References: <35fd10ab-5f7f-7a57-9cc5-c03d81476d13@appl-ecosys.com> In-Reply-To: <35fd10ab-5f7f-7a57-9cc5-c03d81476d13@appl-ecosys.com> From: "David G. Johnston" Date: Mon, 10 Jun 2024 16:03:50 -0700 Message-ID: Subject: Re: Gaps in PK sequence numbers To: Rich Shepard Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000db0dc9061a912b77" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000db0dc9061a912b77 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jun 10, 2024 at 3:57=E2=80=AFPM Rich Shepard wrote: > > I found a web page that explains how to find the gaps in a sequence, yet = I > want to understand why nextval() doesn't begin with the max(FK)+1 value. > For efficiency the only thing used to determine the next value of a sequence is the stored value of the last sequence value issued. Where that value may have been used, in a table as a PK or some other purpose, does not enter into it. Using a sequence as a default does indeed become problematic if you don't use it exclusively. If you do use it exclusively usually you just set the last value to be the maximum needed and use it going forward. The numbers from deleted rows simply remain missing in the table. David J. --000000000000db0dc9061a912b77 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Jun 10, 2024 at 3:57=E2=80=AFPM Rich Shepard <<= a href=3D"mailto:rshepard@appl-ecosys.com">rshepard@appl-ecosys.com>= wrote:

I found a web page that explains how to find the gaps in a sequence, yet I<= br> want to understand why nextval() doesn't begin with the max(FK)+1 value= .

For efficiency the only thing used to d= etermine the next value of a sequence is the stored value of the last seque= nce value issued.=C2=A0 Where that value may have been used, in a table as = a PK or some other purpose, does not enter into it.=C2=A0 Using a sequence = as a default does indeed become problematic if you don't use it exclusi= vely.=C2=A0 If you do use it exclusively usually you just set the last valu= e to be the maximum needed and use it going forward.=C2=A0 The numbers from= deleted rows simply remain missing in the table.

Davi= d J.

--000000000000db0dc9061a912b77--