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 1iDM6G-0003zs-GO for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 05:13:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iDM5H-0001ux-Db for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 05:12:55 +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 1iDM5H-0001uq-1J for pgsql-general@lists.postgresql.org; Thu, 26 Sep 2019 05:12:55 +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 1iDM5D-0001d8-VP for pgsql-general@postgresql.org; Thu, 26 Sep 2019 05:12:53 +0000 Received: from [188.174.52.210] (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 1iDM5B-0007Qz-Eg; Thu, 26 Sep 2019 07:12:49 +0200 Received: from localhost.my.domain (localhost [127.0.0.1]) by localhost.unixarea.de (8.15.2/8.14.9) with ESMTPS id x8Q5Cllu003107 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Thu, 26 Sep 2019 07:12:48 +0200 (CEST) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by localhost.my.domain (8.15.2/8.14.9/Submit) id x8Q5CkmR003106; Thu, 26 Sep 2019 07:12:47 +0200 (CEST) (envelope-from guru@unixarea.de) X-Authentication-Warning: localhost.my.domain: guru set sender to guru@unixarea.de using -f Date: Thu, 26 Sep 2019 07:12:46 +0200 From: Matthias Apitz To: Adrian Klaver Cc: pgsql-general@postgresql.org Subject: Re: updating sequence value for column 'serial' Message-ID: <20190926051246.GA2939@c720-r342378> Reply-To: Matthias Apitz References: <20190924144738.GA2377@c720-r342378> <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> <20190925054011.GA2887@c720-r342378> <3d189c7f-2e45-a1a4-fc53-5dff1d364540@aklaver.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="6c2NcOVqGQ03X4Wi" Content-Disposition: inline In-Reply-To: <3d189c7f-2e45-a1a4-fc53-5dff1d364540@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.52.210 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --6c2NcOVqGQ03X4Wi Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable El d=C3=ADa mi=C3=A9rcoles, septiembre 25, 2019 a las 07:42:11a. m. -0700, = Adrian Klaver escribi=C3=B3: > >>> sisis$# DECLARE > >>> sisis$# maxikatkey integer :=3D ( select max(katkey) from titel_da= ten ); > >>> 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', maxik= atkey) ); > >>> sisis$# RAISE NOTICE '%', result ; > >>> sisis$# END $$; > >>> NOTICE: 330722 > >>> NOTICE: 330723 > >>> DO > >>> > >>> Is there any better way? Thanks > >> > >> I have not found a better way. I use ALTER SEQUENCE .. RESTART 330722 > >> though: > >=20 > > Yes, but I found no way to use a variable like 'maxikatkey' in the ALTE= R SEQUENCE ... > > it only excepts digits like 330722. >=20 > DO $$ > DECLARE > max_id int; > BEGIN > SELECT INTO max_id max(id) + 1 FROM seq_test; > RAISE NOTICE 'Max id is %', max_id; > EXECUTE 'ALTER SEQUENCE seq_test_id_seq RESTART ' || max_id::text; > END; > $$ LANGUAGE plpgsql; Hi Adrian, I adopted your code to the name of my table 'ig_target_ipfilter' and its SERIAL column 'id'; it does not work (and I don't know how it could works because in the 'ALTER SEQUENCE ...' stmt is somehow missing '... WIT= H value ...') or do I understand something wrong?): cat -n /home/apitzm/postgreSQL/test.sql 1 DO $$ 2 DECLARE 3 max_id int; 4 BEGIN 5 SELECT INTO max_id max(id) + 1 FROM ig_target_ipfilter ; 6 RAISE NOTICE 'Max id in % is %', 'ig_target_ipfilter', max_id; 7 EXECUTE 'ALTER SEQUENCE ig_target_ipfilter_id_seq RESTART ' || = max_id::text; 8 END; 9 $$ LANGUAGE plpgsql; psql -Usisis -dsisis < /home/apitzm/postgreSQL/test.sql NOTICE: Max id in ig_target_ipfilter is ERROR: query string argument of EXECUTE is null KONTEXT: PL/pgSQL function inline_code_block line 7 at EXECUTE Please clarify. Thanks 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. --6c2NcOVqGQ03X4Wi Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEXmn7rBYYViyzy/vBR8z35Hb+nREFAl2MSMsACgkQR8z35Hb+ nRF2JBAAmVaQGVKaePcnaZM8aEnUVOXZrQVpmPSHhSuzfSaPFmzWa2lLtPBNiY5g gIrfqAdAUbDtCuv8gs2eKdUIgbPZcKOJ48cysu5J/JaLlqKlDf2HEeoC4XHCYHei 6oD1Z0HQ3z2GWJZUKAPbkjC5YInXmm/bfUIqQnoPnE7uVWtHrMGXxUYn/PYsS3rN mSjSgFXuGgPDzB0rUE675gppV50b4jyg+DudcqJpjFJhl9Rb1haCuvo/UQDE+86P gCo+gosG6u4cGvhxyidPxjQM9GcJwq+uzwdWfjcm2Y8wLgStgaiaz5geXU3+CNhr oN961lKqvvG/2HGL7Lbj2+1zO5POajYdzkEn0SjCCowNoQHSsPpDXpJTUZedvRim VRi4vQSmSDx5tsDosuhAOBvCuF14AgZV+D662DWdl7fltao4V0R269vVPKEH8ZZz X8MwvUALpT8yezHHg9l99NrUe9WeFxZxXbL8sjpjLSHebIriJUR8AVKom+ttZ8IW nCVZCCSNsZHGposgx/RsQdYWqPbuoF2tbpgTB0Ok5r8vsAJSoEZEpo6WkGk3fYop bxoG1ndQ87SzMclbsqf71KAxscAV2nUVLHkJiBykC5y5cLE0kJ/5Flec6v647He5 tRRjSlYHLnY7tnS0+4JP7ugVJ6dp53ng0YOOmgAICSjIX/IR35A= =0uau -----END PGP SIGNATURE----- --6c2NcOVqGQ03X4Wi--