Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCm6V-00067a-2I for pgsql-general@arkaria.postgresql.org; Tue, 24 Sep 2019 14:47:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCm6S-0001qV-Ih for pgsql-general@arkaria.postgresql.org; Tue, 24 Sep 2019 14:47:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCm6S-0001qI-9Q for pgsql-general@lists.postgresql.org; Tue, 24 Sep 2019 14:47:44 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCm6P-0000OH-9M for pgsql-general@postgresql.org; Tue, 24 Sep 2019 14:47:43 +0000 Received: from [188.174.92.119] (helo=localhost.unixarea.de) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.86_2) (envelope-from ) id 1iCm6N-00037c-RQ for pgsql-general@postgresql.org; Tue, 24 Sep 2019 16:47:39 +0200 Received: from localhost.my.domain (localhost [127.0.0.1]) by localhost.unixarea.de (8.15.2/8.14.9) with ESMTPS id x8OElcog002499 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO) for ; Tue, 24 Sep 2019 16:47:38 +0200 (CEST) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by localhost.my.domain (8.15.2/8.14.9/Submit) id x8OElcKu002498 for pgsql-general@postgresql.org; Tue, 24 Sep 2019 16:47:38 +0200 (CEST) (envelope-from guru@unixarea.de) X-Authentication-Warning: localhost.my.domain: guru set sender to guru@unixarea.de using -f Date: Tue, 24 Sep 2019 16:47:38 +0200 From: Matthias Apitz To: pgsql-general@postgresql.org Subject: updating sequence value for column 'serial' Message-ID: <20190924144738.GA2377@c720-r342378> Reply-To: Matthias Apitz MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Operating-System: FreeBSD 13.0-CURRENT r342378 (amd64) X-message-flag: Mails containing HTML will not be read! Please send only plain text. User-Agent: Mutt/1.11.1 (2018-12-01) X-Con-Id: 51246 X-Con-U: 0-guru X-Originating-IP: 188.174.92.119 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, We have in a database some 400 tables, 75 of them have a 'serial' column, like the one in the example table 'titel_daten', column 'katkey'. I want to create a SQL script to adjust alls these sequences to the max+1 value in its column after loading the database from CSV file. I found no other way as the code below (the RAISE NOTICE is only for test at the moment and the output is correct for this table, i.e current max in 'katkey' is 330721): sisis=# DO $$ sisis$# DECLARE sisis$# maxikatkey integer := ( select max(katkey) from titel_daten ); sisis$# result integer := 1; sisis$# BEGIN sisis$# maxikatkey := maxikatkey +1; sisis$# RAISE NOTICE '%', maxikatkey ; sisis$# result := (SELECT SETVAL('titel_daten_katkey_seq', maxikatkey) ); sisis$# RAISE NOTICE '%', result ; sisis$# END $$; NOTICE: 330722 NOTICE: 330723 DO Is there any better way? Thanks matthias -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub Mientras haya voluntad de lucha habrá esperanza de vencer.