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 1hIem6-00006T-L6 for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 19:38:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hIem5-0007nH-Ah for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 19:38:45 +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 1hIem4-0007k8-Rf for pgsql-general@lists.postgresql.org; Mon, 22 Apr 2019 19:38:45 +0000 Received: from mout.gmx.net ([212.227.17.22]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hIem2-0004Me-Kh for pgsql-general@lists.postgresql.org; Mon, 22 Apr 2019 19:38:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1555961918; bh=olN6WkhE8jTYNN2NbaS9GFmgALhXQIfyXcb47A9itd4=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=btvGAfbRmPdw3qO9z+2xDHCHSxYQC1lMKXCGOQdwnlcMwL7M5k1q/6lBIKlZYlTtu ScSSknPcLvcAK3v750Vri84cTSl+r/3PmarxtkqhLRBSBVYeoNxJ612YcIGMagtnvO 5WcPKPe6F731NkLsOpxYjyfb/x876cTI4Cz6J4vQ= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.178.37] ([93.104.68.174]) by mail.gmx.com (mrgmx101 [212.227.17.168]) with ESMTPSA (Nemesis) id 0MeLKt-1hSKcj3Dsl-00QDcL for ; Mon, 22 Apr 2019 21:38:38 +0200 Subject: Re: Resetting identity columns To: pgsql-general@lists.postgresql.org References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> From: Thomas Kellerer Message-ID: Date: Mon, 22 Apr 2019 21:38:39 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 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: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:PWK0iRz9PXcUiBm+E7Dew2/D7NaVVrFvwIjywkxRMsZCHuRlVVg jMaSZhKNrCfgmJ9unaJ7egRJSwOYU7tgaahyltu/7t4TYNLE+p32Og+geJqNnAoa5i4ba2w sN4aGpV95XJEJrGyEbTJop+5QPHHFYW6Bb2dclAAAPrPDO1X3z4Ig564q3Vr0wm++QxWlNf 6iJx5jbQYg+E8nxlin7Fg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:YkeULDywic0=:SAa1AIp0h62wpPfyW8hNTm K/MegofIODORe1YE+QXgOfDELJrg9Z0aXuC0JSdJRgHvjxB4yuUbUTn/h5KmzVekR3TTdmITR hm/iZPjclYv63fNZPRHXCbiNMBucaJLjMzmnUBx1OORTiK/Ozz2g9oz8hkz9FzQse3KLYg9Rm d/PgB//dZipNUfrcDxjT3vjIv4nf5JDOXs/NSDVOsEh72YElxpnhjl6EeiO7UmXjakXuN+6a8 +g/kUFp2wafm1GCVlCrJehvMaD+jKkoZzzOayUVMduXqS5+RJ/pCXxsnGwCLB6y15joY/sgT0 UldtdvAcFBbAXfXoSDB/npgtn3y3WF9BG9LWpgGexGbJVQri2DxIMLTWQliiNJwZzZsGRdun7 JdgwMsmKr8MvNV3h43KDOOR7WJLFpmwjS2CW/Nw3OflphBDCh3+AAc5kezDRkb0w2BDJuOxJk wX84rckttseFzyMdGZ6Wpw+2neJ9ghE6SvEzuZmQe68wnfvisZAgo4MHjT9QXnXhbblJrPHQa 35KOltCwKmsRqsEASEG/2j55bOaxsdeQtQ4oAsEEMwyLyZPlUxaecydA1W276Gq4c2Kpp9gnW M8jMTyA46gzWyPuZB0bvmsl/m539/3t03g8XXCkNYePJdbjAoXhbmEH2B8junIzhdj/TT/CwP ngkhFTzt9S0HTC9KNGVICwrXrc0tJvgp5rAbsU0XiCR41soQ25u4JmVr6Ry0psBcMyT+IVLFH 5TdGUXkf6bjDvb1ajMrf50YbTYe+7Ahc20ZrerlbwMf5k3ERy8dJHq20fgwbWkv/0qHTj7KhX uc+yiz9WrA86jRK7UN8CFNaLLZCYgp/RrwxM7aKEn//vI50pOOsjgu0MECpj3QLkId4KP2tjw KRwb5+2pAzGmyOPKCqKLZMamSxIG8wq4bsJ6j94y3IPIO2v65xMoMJI3W0xi5f5WcnlUUltjM 2SY5ZBkFYfA== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ray O'Donnell schrieb am 22.04.2019 um 17:30: > 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: > > =3D# alter table orders alter column order_id restart with ( > select max(order_id) + 1 from orders); > > ERROR:=C2=A0 syntax error at or near "(" > LINE 1: ...r table orders alter column order_id restart with (select ma.= .. > > > 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. As you noticed, an identity column is backed by a sequence, just like a se= rial column, so you can use setval() to sync the sequence. To get the name of the sequence you can also use pg_get_serial_sequence() = (despite its name): select setval(pg_get_serial_sequence('orders', 'order_id'), (select ma= x(order_id) from x)); Thomas