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 1iCmKC-0006gZ-92 for pgsql-general@arkaria.postgresql.org; Tue, 24 Sep 2019 15:01: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 1iCmKA-0006V1-7f for pgsql-general@arkaria.postgresql.org; Tue, 24 Sep 2019 15:01:54 +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 1iCmK9-0006Uu-Rn for pgsql-general@lists.postgresql.org; Tue, 24 Sep 2019 15:01:53 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iCmK7-0006uK-Ih for pgsql-general@postgresql.org; Tue, 24 Sep 2019 15:01:52 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 1E3F24E4; Tue, 24 Sep 2019 11:01:50 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 24 Sep 2019 11:01:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=r STYPjC3LmtdG59L0W/m1mcyET9Y8b3XeNn2mv3QVqM=; b=MoqVD8YKzjV8UHy29 WfdDkg/JCr8oQNPJQgb4hWIbKoRRjoNSvOCEAe+ein6ipCguh7lEJWD5/5J0WHNS 1Zbkmn0a2DVfIBNPmYNuyTL+nxnUJoIcjTouGFkO/vMSN2Xx55XX3vCGeGr5Kz/S 7rE9WQu2OqJATBsgwGysi89zTUwXh/sRRUpuXx3eFPGbmVgvtfMrxnb7vnEc53E1 Ae5Nj7C4HuDThYumn1RvjYRjAdZYkFyL4ExPoeWeJ/iB8f9JPUFOaP+43XuWaK8G xbyg/3+QZa2UIjSBRqzj8orHL9DFiNHYMPjIG0yPgeH5Xir3LD6XoZN923hdgKB+ 5+/ZQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=rSTYPjC3LmtdG59L0W/m1mcyET9Y8b3XeNn2mv3QV qM=; b=u9F4KdYZl6bzZDquh3+eHlF+qTTixgMZP/v12iZGUHoyx5hBbq2c2NfGc Rr/LEs8VHSph7nv+lHng0OJH5QVlXeNpcTv/wKjeI2hiBpI+ID/IS9P0WjRpJp8n VhCPHOo+A2tu85/Ny2NwsOfxpEZYhl0cnl9Wh0BoCWh1MoUd3PGLvs+I3ekYJoU9 IpLyIEMAtK6DjbrIbz011uazs8TgBS/03CTd0Ah48lrBs44T5sbgMtM/sakFfZLn E8XGbgcabJiNtVDfPRcE5RWkLrIPU5p2joOFw353lS6N6SgKyadWG7q3wiWeweDa DapCELoJgvw25kpHZjcoHcvEWXjkg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrfedtgdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhoshhtghhrvghsqh hlrdhorhhgnecukfhppedujeegrddvuddrjeekrdduieeknecurfgrrhgrmhepmhgrihhl fhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluh hsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.3] (174-21-78-168.tukw.qwest.net [174.21.78.168]) by mail.messagingengine.com (Postfix) with ESMTPA id B8D69D6005F; Tue, 24 Sep 2019 11:01:47 -0400 (EDT) Subject: Re: updating sequence value for column 'serial' To: Matthias Apitz , pgsql-general@postgresql.org References: <20190924144738.GA2377@c720-r342378> From: Adrian Klaver Message-ID: <7c2a43ab-7fc7-8fd3-77ae-b572efa8df15@aklaver.com> Date: Tue, 24 Sep 2019 08:01:46 -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: <20190924144738.GA2377@c720-r342378> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/24/19 7:47 AM, Matthias Apitz wrote: > > 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 I have not found a better way. I use ALTER SEQUENCE .. RESTART 330722 though: https://www.postgresql.org/docs/11/sql-altersequence.html That is roughly equivalent to SELECT SETVAL('titel_daten_katkey_seq', maxikatkey, false) in that the next value used will be 330722 not 330723. RESTART is also transactional whereas SETVAL() is not. > > matthias > -- Adrian Klaver adrian.klaver@aklaver.com