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 1hIb8B-0006Vh-Mu for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 15:45:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hIb8A-0005rZ-1Z for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 15:45:18 +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 1hIb88-0005rS-0a for pgsql-general@lists.postgresql.org; Mon, 22 Apr 2019 15:45:17 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hIb84-0001dx-HI for pgsql-general@postgresql.org; Mon, 22 Apr 2019 15:45:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 83C6D360; Mon, 22 Apr 2019 11:45:10 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 22 Apr 2019 11:45:10 -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=fm2; bh=W 0GsyvUCDspDCwG0H/76WukCJ17avhOSVqLwwB8ffAI=; b=Mu0Lr9JyRRxD/v0SO x0LTMymO2SNOHP62T6gW47wkGTislg7WS6iy+4kAItb9izfNwV83B1KdYqfy9+lp 4StPw1xXd5WTqpMwo85093qc8PhHbQKq4CPoOXsdzCNED8oYLRUDsiwDp5HMSCf4 gjIewKGoCKg1CAVxBbUW9zNQaZeZbuyzovnHlH1LZR97g1TimgFXrseaOJ99zuBh hAQB8seH/HZVDbANmj64VhpRe5BWXoB5NN87qj3CoDgXQAYk728uCFpGjLs0wN4A alYPWLSZC9auvr2srRL31jPkMh5jPU10rJ3QYJ1dUqKIO4nKu479cbG2vbM91Pit Y3oSg== 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=W0GsyvUCDspDCwG0H/76WukCJ17avhOSVqLwwB8ff AI=; b=1vNbuKBWnr2pXk+rUHBHmqaozDp19Q5BKOF6Ai736RlCEw7k9WI91Cl+M 0hAtUX2/jK9v/qgGgLXTjOJJ8CCCTOAHzZxxbk29oNjDGkXnFIJhwY2Bf6A8Jjvz 6mbrNoRuVXty+qjjySIThRtokq2wwleas9ciMlJlNVpZuudMBR8UxEK5Kwib0f7Q wKQFVjVvf8fpdtL8/yzwK/rURbAEpOlgIx9/2Nfo1TFMasLSyGI6u26URsdH8yOb YUflLkNu9YCVawnN0y8XxYv6tSRlbRiguIvtbdIDsCIfRPYXr3CCBI/neC0a1nQm 7bS3Ywy4TVhGb7TtPijd3QFtVvcKQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrgeeigdekjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre 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 D230E103CE; Mon, 22 Apr 2019 11:45:08 -0400 (EDT) Subject: Re: Resetting identity columns To: Ray O'Donnell , 'PostgreSQL' References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> From: Adrian Klaver Message-ID: <2b2a0f4f-c422-2f5f-1355-e37e446f104e@aklaver.com> Date: Mon, 22 Apr 2019 08:45:08 -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: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> 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: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. > > 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