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 1sHpXI-00B4me-Nq for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 18:51:00 +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 1sHpXG-005evl-80 for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 18:50:59 +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 1sHpXF-005evY-Th for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 18:50:58 +0000 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sHpXE-001A3O-6m for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 18:50:57 +0000 Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6f99555c922so736800a34.3 for ; Thu, 13 Jun 2024 11:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718304655; x=1718909455; 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=punUI5HYfJINYA/vg9GemZ9b7Ko7DchpOIrRI3HRpbc=; b=UrkYgC2rcK/rL3QgfAiywKoWUOv7O0bLmYdVBI89HIqaYwVFr54ZtWbWPkVKPSwwJo YV2/u7CFtvmDaGVMZ9LabdAoODJY8U2qe2MOOrhLVocWhJkLE4nsqKFRl0dW6XITsMx2 p50wIZN23+AfjzF0XKQKsJMz4P5qc5WS/Z8C0+/8z97ngXeIwsz073FfqqNTQ64dJvXk mdIz22Rq6+Dul5xiwA/59R7NBrL8njKV/cuJOguCddHlWkVgjxyfGg3AJv0vC7pnCzLN gAPoFjKGTGi/h28rn3sriEUrCModixjeWxCmZ0nl4yLxTepyG+fJNMoAQ4HTYHAB8YgJ m3cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718304655; x=1718909455; 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=punUI5HYfJINYA/vg9GemZ9b7Ko7DchpOIrRI3HRpbc=; b=UQhzeovsaxIDm+RMFtqHFPDZwzsm/KUHZi9TVh1clAZKGDBKxk5ZpQVKHXuE5nrlVd uULI+YKVPJ04ML23I5+gHl1aS7q93dXNJvxCFLBGJVZFkrS1Md/emWc8q90Bs64O3yDp oOpqheELgWSjCbAV8xNYuVMvHt15wufkZ/2+qTQQSA4Qu+OoMgZuM49YmDjXVUjhvm+O hHLkSal2daP63ECVoezpabDboAGg9zXkz+w9rGC4WLCWB7KFPfn+cOUnJzUHt6AhcZGH vEKodUtCwQJ9++bpUtcD52CXO9QR5XPNcXVZGvMbwoR50fTx3mGPRAJ0RSvP4cXmvAgf 6JhQ== X-Gm-Message-State: AOJu0YzEhnCVkulUKNLVSC5p73fV6qmxR0qqN0QfSi9xTJANxyKA0j7Q eF3LOjyOVUgy8V6Uy7fUo+I5dNmfrQ6CdUIat35uzC7IZsiOfR4LPEq3c3+DWLNlWPKoF3kuEzv kJAcYW5ljlqy0+1pdRv6G1fXEYiK3ux3D X-Google-Smtp-Source: AGHT+IHXduNEVPnbs55NvTQwJmOiH8i87EvzofwlhN1QnjwJh+Aq3zLwt3pYyou1u3NawucMJ2puECiSxcaj5HfdJTw= X-Received: by 2002:a05:6870:1646:b0:24f:c7cf:17fb with SMTP id 586e51a60fabf-258428de86cmr543254fac.22.1718304655072; Thu, 13 Jun 2024 11:50:55 -0700 (PDT) MIME-Version: 1.0 References: <387fa6c2-5de-f24-d522-46282597703@appl-ecosys.com> In-Reply-To: From: Ron Johnson Date: Thu, 13 Jun 2024 14:50:43 -0400 Message-ID: Subject: Re: Reset sequence to current maximum value of rows To: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="00000000000094e4f6061ac9faf9" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000094e4f6061ac9faf9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 13, 2024 at 2:38=E2=80=AFPM Rich Shepard wrote: > On Thu, 13 Jun 2024, Ron Johnson wrote: > > > 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. > > Ron, > > I had manually set the PKs (column: company_nbr) which has a sequence > defined for it when I added about 50 rows to the table yesterday. > > Now that I'm aware of the DEFAULT option when inserting new rows I tried > to reset the sequence maximum number to max(company_nbr); the highest > number > for the rows inserted yesterday. That's when I tried resetting the curren= t > sequence number with the expectation that new rows would be numbered > sequentially higher than that value. > > Today I saw that I had missed one new company and entered it using DEFAUL= T > for the company_nbr PK. No need to do that. Just write: INSERT INTO public.companies (company_name, , industry, status) VALUES ('Berkshire Hathaway', 'Conglomerate', 'Mumble'); The next value of companies_org_nbr_seq will automatically be taken and inserted into the table. When I looked at that table every company_name that > I had added yesterday was changed to the one inserted today. > You'll have to show us what you did. --00000000000094e4f6061ac9faf9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Jun 13, 2024 at 2:38=E2=80=AFPM R= ich Shepard <rshepard@appl-e= cosys.com> wrote:
On Thu, 13 Jun 2024, Ron Johnson wrote:=

> 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.

Ron,

I had manually set the PKs (column: company_nbr) which has a sequence
defined for it when I added about 50 rows to the table yesterday.

Now that I'm aware of the DEFAULT option when inserting new rows I trie= d
to reset the sequence maximum number to max(company_nbr); the highest numbe= r
for the rows inserted yesterday. That's when I tried resetting the curr= ent
sequence number with the expectation that new rows would be numbered
sequentially higher than that value.

Today I saw that I had missed one new company and entered it using DEFAULT<= br> for the company_nbr PK.

No need to do that.= =C2=A0 Just write:
INSERT INTO public.co= mpanies (company_name, , industry, status)
=C2=A0 =C2=A0 VALUES ('Berkshire Hathaway', 'Conglome= rate', 'Mumble');
=C2=A0
The next va= lue of companies_org_nbr_seq will automatically be taken and inserted=C2=A0= into the table.

When I looked at that table every company_name that
I had added yesterday was changed to the one inserted today.

You'll have to show us what you did.=C2=A0
<= div>
--00000000000094e4f6061ac9faf9--