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 1hIbHA-0006sn-4W for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 15:54:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hIbH7-0005vP-Dg for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 15:54:33 +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 1hIbH7-0005s8-2K for pgsql-general@lists.postgresql.org; Mon, 22 Apr 2019 15:54:33 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hIbH2-0006ty-CZ for pgsql-general@postgresql.org; Mon, 22 Apr 2019 15:54:32 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 088AE33A; Mon, 22 Apr 2019 11:54:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 22 Apr 2019 11:54:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:from:to:references:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=J rUqQBQ0N2kaU+9BK0gTI1//TCPoKbo46QKPKy9NEyc=; b=b8SDUI1Mm5EF078AC To41W5nGjKgX0A1E53nh45F4EKMMtvZrpwytkwgsw4QRtjTjaA3d5/rA8Rb40v1c hVA2c2hqmrRAHha3KNvv4z+V+hgl7r6dIo7aHAAuRRfEyrFpqyJ7apMkQxCd495O q0WWwwkLZ9QWaV9e0Y6bDSM/eqYhO96aGu7Fyxk5ADirCk+gtx3nv1/GujX2S2O0 pqa8JaiVFZkbkRzP/wjDCgJyNs8eTRJR/K+ji3ge3n4tyMf0tdgyM5xrmen3dIPv b585HG/6y0V8QvSy7RUAKEPSwVmv3ozGXu2iugDyjl1wHougFzqHsip813FI8S8L yQ6rw== 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=fm2; bh=JrUqQBQ0N2kaU+9BK0gTI1//TCPoKbo46QKPKy9NE yc=; b=2TNcHcmX8n+YR6FJdzno5GpuUtHDUq2sVF27DeUmOnlLrgTcoi/12FLq5 EczMngEKDO2TcpEr3YHvIIb4VK+fWgTP5ITXr6K/d2cI/twowBrrB6VaqmNjnHcB 7I3XVaipLW1VCst4axa2qMYCQAzwsOA83uamtprfU9fzCBk7YEIuEmMWMZFV5/SU u9TOfv6q8fB+vci3qb0pJxzxFb29KdNhKE24HWVyVKw/sVUH+qqluho+U4w/WMl4 pZ48Kdb2A9LM9ph1V85iuS5BA/Bw4x7QZIQVInHKIARfqY8oVO5t5IkUiMsc/ozb ewMUtZ0foe7muBBAV342H2g0LwoTA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrgeeigdeltdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffhvfhfkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhoshhtghhrvghsqh hlrdhorhhgnecukfhppeejgedrleegrdejfedrvddvvdenucfrrghrrghmpehmrghilhhf rhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhush htvghrufhiiigvpedt X-ME-Proxy: Received: from ranger.suse (unknown [74.94.73.222]) by mail.messagingengine.com (Postfix) with ESMTPA id 8BA3A10316; Mon, 22 Apr 2019 11:54:23 -0400 (EDT) Subject: Re: Resetting identity columns From: Adrian Klaver To: Ray O'Donnell , 'PostgreSQL' References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> <2b2a0f4f-c422-2f5f-1355-e37e446f104e@aklaver.com> Message-ID: <617a279e-17c9-c4cd-ec17-1ad8a11498ba@aklaver.com> Date: Mon, 22 Apr 2019 08:54:22 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <2b2a0f4f-c422-2f5f-1355-e37e446f104e@aklaver.com> 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 4/22/19 8:45 AM, Adrian Klaver wrote: > On 4/22/19 8:30 AM, Ray O'Donnell wrote: >> Hi all, >> >> I'm probably doing something silly.... I'm migrating data from one >> database table to another, where the old table used a SERIAL primary >> key and the new one uses GENERATED BY DEFAULT AS IDENTITY. Having >> loaded the data into the new table, I need to reset the underlying >> sequence so that it picks up from the highest existing value. >> >> I'm using PostgreSQL 11.2 on Debian 9. >> >> I've tried: >> >> =# alter table orders alter column order_id restart with ( >> select max(order_id) + 1 from orders); > > https://www.postgresql.org/docs/11/sql-altertable.html > > "ALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } | > SET sequence_option | RESTART [ [ WITH ] restart ] } [...]" > > See if the above form will work in your Do block below. Aargh, time to clean the glasses. You where using the above. Sorry for the noise. > >> >> ERROR:  syntax error at or near "(" >> LINE 1: ...r table orders alter column order_id restart with (select >> ma... >> >> >> I also tried it with a DO block: >> >> =# do language plpgsql $$ >> $# declare m_max_id bigint; >> $# begin >> $# select max(order_id) + 1 from orders into m_max_id; >> $# alter table orders alter column order_id restart with m_max_id; >> $# end; >> $# $$; >> >> ERROR:  syntax error at or near "m_max_id" >> LINE 5: ...er table orders alter column order_id restart with m_max_id; >> >> >> What am I missing? >> >> I should add that this is part of a larger migration script; otherwise >> I could just do it by hand the command line. >> >> Thanks in advance, >> >> Ray. >> > > -- Adrian Klaver adrian.klaver@aklaver.com