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 1tb1DB-007G7p-3z for pgsql-admin@arkaria.postgresql.org; Thu, 23 Jan 2025 17:41:49 +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 1tb1D8-001KMM-A4 for pgsql-admin@arkaria.postgresql.org; Thu, 23 Jan 2025 17:41:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tb1D7-001KMD-Vl for pgsql-admin@lists.postgresql.org; Thu, 23 Jan 2025 17:41:45 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tb1D5-00185Q-2n for pgsql-admin@postgresql.org; Thu, 23 Jan 2025 17:41:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 50NHfem2202381; Thu, 23 Jan 2025 12:41:40 -0500 From: Tom Lane To: "Campbell, Lance" cc: "pgsql-admin@postgresql.org" Subject: Re: Sequence Cycle question In-reply-to: References: Comments: In-reply-to "Campbell, Lance" message dated "Thu, 23 Jan 2025 17:13:05 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <202379.1737654100.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 23 Jan 2025 12:41:40 -0500 Message-ID: <202380.1737654100@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Campbell, Lance" writes: > Table X has records that have been removed over time randomly. There ar= e 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