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 1iDVQV-0005k0-BY for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 15:11:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iDVQU-0001Mu-9Q for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 15:11:26 +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 1iDVPD-0005uE-L9 for pgsql-general@lists.postgresql.org; Thu, 26 Sep 2019 15:10:07 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iDVPA-00031n-7j for pgsql-general@postgresql.org; Thu, 26 Sep 2019 15:10:07 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id DA23B220AF; Thu, 26 Sep 2019 11:10:01 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 26 Sep 2019 11:10:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=a 7A6CkYg8lLXqBVNSWBQyyFyClQEqTuMNSIaMwXWO3o=; b=1Sb+7l3swFl5Qgcxl /C4mMhjDpfGYtUu33y3qtdwnwP2c7V1ulIuRPpP+/0p2iCyUfIN8NzdhoTRhWjQO CmIAtF0Rw+DI1IU571qoqJYXTGlaxzN6fiV7j6sjLYD08BrIrlmMlIp1tCkZ8i9X XgM/lxqSEltjlpSKo/eL3LyELQwxOHqR787702kZixpl7jTC4v54QzNIDDp2L92+ JiHmwVqRkEYgLT1ZJKWXnd+9v8aDuGtLNE7ROuSSH5yacDiGdOlNEVq3rbZOYEBz VLla1Y6mmf7/JTlVnQECp1lZ2NCkN6xbgWHinfwXaq8zFyU5RyoeCfbgXde95963 BZXgQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=a7A6CkYg8lLXqBVNSWBQyyFyClQEqTuMNSIaMwXWO 3o=; b=IoN78sQJoAi8yddWGASzY12nWPcRqmrDgCnQn9NulffrnXb4/7gZ02sxX AIdstNaqHMzRlcaYJfOayO/MIyib/sT9bomujCmZUmjAau9DjuWGpB1Ck79azYdM YPFfi8ugP7rXhBoBktdmoIHBfFdpry59U/VQvZGvye0KQByDW2MVdBpRwxUgB0P5 dDbtynWN1KWXf8KS9xKYzPLxd4wPbVjWUgAWYTDQ9Mbh3jv0Diu1B7QkTCIYt4em f6Usi+SqZx7KMfhyAComkjPXod7rMoHtlW38Tq+788qOpCFz9khDp5Jr4MoX+UHy N19v0OT/Yw4bpMpn8bDvcSbia5ZgA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrfeeggdekgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepudejgedrvddurdduheefrddule dunecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhl rghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.3] (174-21-153-191.tukw.qwest.net [174.21.153.191]) by mail.messagingengine.com (Postfix) with ESMTPA id 5DD3580060; Thu, 26 Sep 2019 11:10:00 -0400 (EDT) Subject: Re: updating sequence value for column 'serial' To: Matthias Apitz Cc: pgsql-general@postgresql.org References: <20190924144738.GA2377@c720-r342378> <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> <20190925054011.GA2887@c720-r342378> <3d189c7f-2e45-a1a4-fc53-5dff1d364540@aklaver.com> <20190926051246.GA2939@c720-r342378> From: Adrian Klaver Message-ID: Date: Thu, 26 Sep 2019 08:09:59 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20190926051246.GA2939@c720-r342378> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/25/19 10:12 PM, Matthias Apitz wrote: > El día miércoles, septiembre 25, 2019 a las 07:42:11a. m. -0700, Adrian Klaver escribió: > >>>>> 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 >>>> >>>> I have not found a better way. I use ALTER SEQUENCE .. RESTART 330722 >>>> though: >>> >>> Yes, but I found no way to use a variable like 'maxikatkey' in the ALTER SEQUENCE ... >>> it only excepts digits like 330722. >> >> 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 '... WITH 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 I forgot about the possibility of NULL being returned by max_id in: SELECT INTO max_id max(id) + 1 FROM ig_target_ipfilter ; So: SELECT INTO max_id COALESCE(max(id), 0) + 1 FROM ig_target_ipfilter ; That will turn a NULL max(id) into 0. > > matthias > > -- Adrian Klaver adrian.klaver@aklaver.com