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 1sHo7l-00Av9f-MD for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 17:20:33 +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 1sHo7j-004JSA-FZ for pgsql-general@arkaria.postgresql.org; Thu, 13 Jun 2024 17:20: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 1sHo7j-004JRL-5I for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 17:20:32 +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 1sHo7d-001V55-FE for pgsql-general@lists.postgresql.org; Thu, 13 Jun 2024 17:20:31 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id DCB082A2B7C for ; Thu, 13 Jun 2024 10:20:22 -0700 (PDT) Date: Thu, 13 Jun 2024 10:20:22 -0700 (PDT) From: Rich Shepard Reply-To: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Reset sequence to current maximum value of rows Message-ID: <387fa6c2-5de-f24-d522-46282597703@appl-ecosys.com> MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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) 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 52 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 newly inserted name. Feh! I need to restore all the correct names for each PK. There's an alternate expression in that SE thread that I didn't try: ALTER SEQUENCE RESTART WITH ; I want to avoid this same situation when resetting the second table's PK sequence number and would like to understand why the SELECT expression changed all column values rather than adding a new row with its attributes. And how to I reset sequences to ignore all current values while adding the next higher value to the end when a new row is INSERTed. TIA, Rich