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 1iD02K-0006mw-EY for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 05:40:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iD02I-0005mM-BG for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 05:40:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iD02H-0005jn-T3 for pgsql-general@lists.postgresql.org; Wed, 25 Sep 2019 05:40:22 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iD02E-000673-TM for pgsql-general@postgresql.org; Wed, 25 Sep 2019 05:40:20 +0000 Received: from [188.174.63.184] (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 1iD02A-0007xw-I1; Wed, 25 Sep 2019 07:40:14 +0200 Received: from localhost.my.domain (localhost [127.0.0.1]) by localhost.unixarea.de (8.15.2/8.14.9) with ESMTPS id x8P5eCH4003195 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 25 Sep 2019 07:40:13 +0200 (CEST) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by localhost.my.domain (8.15.2/8.14.9/Submit) id x8P5eCeq003194; Wed, 25 Sep 2019 07:40:12 +0200 (CEST) (envelope-from guru@unixarea.de) X-Authentication-Warning: localhost.my.domain: guru set sender to guru@unixarea.de using -f Date: Wed, 25 Sep 2019 07:40:11 +0200 From: Matthias Apitz To: Adrian Klaver Cc: pgsql-general@postgresql.org Subject: Re: updating sequence value for column 'serial' Message-ID: <20190925054011.GA2887@c720-r342378> Reply-To: Matthias Apitz References: <20190924144738.GA2377@c720-r342378> <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="BXVAT5kNtrzKuDFl" Content-Disposition: inline In-Reply-To: <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> 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.63.184 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --BXVAT5kNtrzKuDFl Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable El d=C3=ADa martes, septiembre 24, 2019 a las 08:01:46a. m. -0700, Adrian K= laver escribi=C3=B3: > On 9/24/19 7:47 AM, Matthias Apitz wrote: > >=20 > > Hello, > >=20 > > 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= '. > >=20 > > 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): > >=20 > > sisis=3D# DO $$ > > sisis$# DECLARE > > sisis$# maxikatkey integer :=3D ( select max(katkey) from titel_date= n ); > > sisis$# result integer :=3D 1; > > sisis$# BEGIN > > sisis$# maxikatkey :=3D maxikatkey +1; > > sisis$# RAISE NOTICE '%', maxikatkey ; > > sisis$# result :=3D (SELECT SETVAL('titel_daten_katkey_seq', maxikat= key) ); > > sisis$# RAISE NOTICE '%', result ; > > sisis$# END $$; > > NOTICE: 330722 > > NOTICE: 330723 > > DO > >=20 > > Is there any better way? Thanks >=20 > I have not found a better way. I use ALTER SEQUENCE .. RESTART 330722=20 > though: Yes, but I found no way to use a variable like 'maxikatkey' in the ALTER SE= QUENCE ... it only excepts digits like 330722. Sometimes, when the table has no rows for example, the SELECT MAX(...) FROM= ... returns . I'm surprised about that even maxikatkey :=3D maxikatkey +1= ; does not set it to 1 'maxikatkey'. Should I worry about this in SELECT SETVAL(..= =2E) or can I make it somehow to 1 or 0? matthias --=20 Matthias Apitz, =E2=9C=89 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=C3=A1 esperanza de vencer. --BXVAT5kNtrzKuDFl Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEXmn7rBYYViyzy/vBR8z35Hb+nREFAl2K/bgACgkQR8z35Hb+ nRFBghAAoWbra/df5PEkKur7XnCuJiZNKaS9h7qKEvLg5Oi9o3F6vDKLPL8/u1yD 4CgRItknC9+HwToM9DqQs89D0cYhtL0QNtuw++WzuBmKp77WQCai76PtRUVfE1fE J2S/dwHUKskZtZmo52GY54/Nd8sB7gyHB0TtDgm8kQ9Dq9jUCQVL6KX+u41y0aMo tEgwvEZIc2SiQ0O3aMIhI3oXdGhesr+1B20YnxUWqswEqod6xYL9eEugkcm9X1cv vjoLYtyxSSFv0E1iQnemaLVnaYG53znVpR6q2tPNWOD+Z+JNHgBE26TmRuaZ3MHl h80NQgGydq6aFovbjIoIyLrxYOyvXSMAGKwgcbLPpk7Zx2XqDUD8h1xRWPEas3Jz Om5HD7N2yGcQjJaMK3Z3I3ZPwMa2T1iCrmf3kAr1+MU7t/vClRzmksEhSi1VMIo1 C1EhEb3sBWJPIKJkPo8La+1pIMGk2b9w214E/ADLhZJOg++dc8lSt0VsxZc/z9Bo gZqoyklHUuq8IEtJDOMT1/qZoglAqNlCcGVvYzgDRuVkAZ46sbEDC3SwfrZ2ourg gEppt0scoQjV8SFrZ7SaiHlwMt/S1GL+39aQwlHeS7a4P7H7VKMgWoixZNeEac8E dVWYEzXiDgy3qJkJeMN9/I4x+3hLbq9nG23jzXPOxshSmpXU5OI= =zLY7 -----END PGP SIGNATURE----- --BXVAT5kNtrzKuDFl--