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 1sHpLW-00B3eg-Oo for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 18:38:50 +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 1sHpLT-005RoO-RV for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 18:38:48 +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 1sHpLT-005Ro4-Fs for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 18:38:48 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sHpLS-001Vb2-0z for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 18:38:47 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 9C2AF2A2B7C for ; Thu, 13 Jun 2024 11:38:43 -0700 (PDT) Date: Thu, 13 Jun 2024 11:38:43 -0700 (PDT) From: Rich Shepard To: "pgsql-generallists.postgresql.org" Subject: Re: Reset sequence to current maximum value of rows In-Reply-To: Message-ID: References: <387fa6c2-5de-f24-d522-46282597703@appl-ecosys.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 current 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 for the company_nbr PK. When I looked at that table every company_name that I had added yesterday was changed to the one inserted today. > What does your table definition look like? Table "public.companies" Column | Type | Collation | Nullable | Default --------------+-----------------------+-----------+----------+-------------------------------------- company_nbr | integer | | not null | nextval('companies_org_nbr_seq'::regclass) company_name | character varying(64) | | not null | '??'::character varying url | character varying(64) | | | email | character varying(64) | | | industry | character varying(24) | | not null | 'Other'::character varying status | character varying(20) | | not null | 'Opportunity'::character varying comment | text | | | ea_nbr | integer | | | 0 ea_amt | numeric(10,2) | | | 0.00 Indexes: "organizations_pkey" PRIMARY KEY, btree (company_nbr) Foreign-key constraints: "organizations_industry_fkey" FOREIGN KEY (industry) REFERENCES industrytypes(ind_name) ON UPDAT E CASCADE ON DELETE RESTRICT "organizations_status_fkey" FOREIGN KEY (status) REFERENCES statustypes(stat_name) ON UPDATE CAS CADE ON DELETE RESTRICT Referenced by: TABLE "locations" CONSTRAINT "locations_org_nbr_fkey" FOREIGN KEY (company_nbr) REFERENCES companies(company_nbr) ON UPDATE CASCADE ON DELETE RESTRICT TABLE "people" CONSTRAINT "people_org_nbr_fkey" FOREIGN KEY (company_nbr) REFERENCES companies(c ompany_nbr) ON UPDATE CASCADE ON DELETE RESTRICT Rich