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 1sHoDV-00Ave1-UJ for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 17:26:29 +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 1sHoDT-004R5h-KF for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 17:26:28 +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 1sHoDT-004R5Y-95 for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 17:26:28 +0000 Received: from mail-oa1-x2d.google.com ([2001:4860:4864:20::2d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sHoDR-0019Uv-Jw for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 17:26:26 +0000 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-254c8e27c80so645977fac.2 for ; Thu, 13 Jun 2024 10:26:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718299584; x=1718904384; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=pDyOUqDmW0JCl0nG0cL9gsmXcP3aTxUIr1bOC5qOVy8=; b=ihuLZwUhf0tEEAKc9Hxeo5aFO0TX3a67Nkb4zLkJ/c3Td8CRGKiT+KMUnjEG3dhgeE 16BD0C482qnwo9EzzBn0b2u5H53hhmbv4w3ExGZefuRV74DKOi1t9ECsPR+8wKq92ASV thdksY+VbnVPxA8L/b8uAX2EJz3pSCHtd2KBK7kMHOTZ+FRjnbcpHwQc43eLC60HOqMM gfOxudiakqXqNunDFStccQnuWxARs6sd0SjlJbhySJCWytff40nA4ZtROPBqsl6JPgox MwY5O0lUzpsIsi7V6/3m9vEVbMQ9xNrs8akdzo75ysg0FqBJ92cGi1ewyphLyxgd1Tri bVIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718299584; x=1718904384; h=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=pDyOUqDmW0JCl0nG0cL9gsmXcP3aTxUIr1bOC5qOVy8=; b=OyDqaUwV2NGOGbHqBaZlvhiiw9vRIFL9354FCoy9UHkpmqVOAUNOoS/aD83MNHnqe+ tD6SXXRp1o9r/nrNIGdVCr1MrbP3Q3QnhAfpVPt7S3lbZCrcHAdo05Qk8/fRdKAIwwxh 75ufTVmnr9IO3Fes2rYPq9KHkQK7bnMBWbylyKDxYK1uSKHXNnnv2XrLw0fnSQF9i05S rUE1EHKgRHhY5MWThpkMJzKsEdnKTschVshZEQSgl0daeJVhnfYJMNRLXrJVPIClDCVX nGNuoe16b0n6LND2nPdpWGu9KX2S7ePZ7tcIWOe9PAzJgsTJznQXD4DLiL6GrfYUh/UK VADg== X-Gm-Message-State: AOJu0YzvP8YLjS7jpCu5OiH4cB8W0sQSd08MMXkO/FfI4F9I2dtBKKoG m0ZsIyFQRKcuycYqzT1T79z6Bljfs5tQ3vegtsCPXWbxRAB5RVLS0J+Vf3Dt5SSIpGjzpPs9V9z 6dbWFLDSS0xcx0qmZZnu0uKq5BK04qw== X-Google-Smtp-Source: AGHT+IFhBPbFU2MhFIyHqQZV/ztSpR+NRHeY4SUHyOIRySxjDCrrxXzeaxf0vR18sTLhNg4v/fDocFAfcVwIPzgZYQ4= X-Received: by 2002:a05:6870:e8c3:b0:254:aaeb:d29b with SMTP id 586e51a60fabf-258428f6587mr317911fac.13.1718299584338; Thu, 13 Jun 2024 10:26:24 -0700 (PDT) MIME-Version: 1.0 References: <387fa6c2-5de-f24-d522-46282597703@appl-ecosys.com> In-Reply-To: <387fa6c2-5de-f24-d522-46282597703@appl-ecosys.com> From: Ron Johnson Date: Thu, 13 Jun 2024 13:26:12 -0400 Message-ID: Subject: Re: Reset sequence to current maximum value of rows To: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="00000000000057a06d061ac8cc29" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000057a06d061ac8cc29 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 13, 2024 at 1:20=E2=80=AFPM Rich Shepard wrote: > Two tables have a sequence for the PK. Over time I manually entered the P= K > numbers not being aware of applying DEFAULT to generate the next number. > > I just tried to set one table's PK sequence to the current max(PK) value > using this expression from a stackexchange thread: > SELECT setval('', , true); -- next > value will be max(PK) + 1 > > Needing to add a new row to a table for a specific industry table (with 5= 2 > rows) I set the PK as DEFAULT in the INSERT INTO expression. To my surpri= se > and disappointment all 52 rows now have the company_name column as the > newly > inserted name. Feh! I need to restore all the correct names for each PK. > If the table has a primary key, then the command *should* have failed with a duplicate key error as soon as the first dupe was discovered. What does your table definition look like? --00000000000057a06d061ac8cc29 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Jun 13, 2024 at 1:20=E2=80=AFPM R= ich Shepard <rshepard@appl-e= cosys.com> wrote:
Two tables have a sequence for the PK. = Over time I manually entered the PK
numbers not being aware of applying DEFAULT to generate the next number.
I just tried to set one table's PK sequence to the current max(PK) valu= e
using this expression from a stackexchange thread:
SELECT setval('<sequence_name>', <current_max_number>, = true);=C2=A0 -- next value will be max(PK) + 1

Needing to add a new row to a table for a specific industry table (with 52<= br> rows) I set the PK as DEFAULT in the INSERT INTO expression. To my surprise=
and disappointment all 52 rows now have the company_name column as the newl= y
inserted name. Feh! I need to restore all the correct names for each PK.

If the table has a primary key, then the c= ommand should=C2=A0have failed with a duplicate key error as = soon as the first dupe was discovered.

What does y= our table definition look like?

--00000000000057a06d061ac8cc29--