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 1hIbP8-0007HR-AW for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 16:02:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hIbP7-0004hu-5b for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 16:02:49 +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 1hIbP6-0004hm-SE for pgsql-general@lists.postgresql.org; Mon, 22 Apr 2019 16:02:48 +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 1hIbP4-0007Ag-Mf for pgsql-general@postgresql.org; Mon, 22 Apr 2019 16:02:48 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id D28302EA; Mon, 22 Apr 2019 12:02:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 22 Apr 2019 12:02:45 -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=E cpnHaf58DWo4tOPebPTiTP28+JlZcCUzSSQ4ss9nE0=; b=YvruDpuCSjKH7IUQz il87IKZX+FIQYTm1P0ooRtYyqfGz4zHE2VDBh9e2SEVBsJbpCI4XHpZYS1yIDiub yxGVhFBJS7NcYYT0ap6O8yiWOibT+yoXdLg1LwuPt5+b2betocyDUkIOrqnLtYX4 bjwY+BWgVeXzly7+ppQKq68On9tLjDHGZshJkb/ahYiK/3eujdppFWquaJ5ym7uh ClAqcmKXzSdd57vvfZMcw/mliDnVJcaWyCwAmYixydJAGy8ubgh9n0OrG2qpm0En ULQfO3so617wQI6UCXsR2tArJXD+wXd8klb2nRkT3gBiWOLg+1iZvsltj17Qg7Cl TJLIw== 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=EcpnHaf58DWo4tOPebPTiTP28+JlZcCUzSSQ4ss9n E0=; b=PEcH9N6zHCZh/oUQkGiDh2pckDTOZXUuhPQyI01nStrdxjuB7ISdR2AD2 i3Zg39pw1IjGo6EUm0eUaRghx+Ef24SJ3+ZFc70Zag8kJq2U+3SY0/jGGenZtlJg viQpj9rp+VrCG/A9jomWmhRGx2KAC1ZHgPJrAu0+a/DMRvsE9tPWgPbYkucnKTQo jtrHObVYY1y+wal6gVbuNeNOq++kJCccUKEvb6mwbib9A+TE/8Rh4BZ6pDaDmm/B RthK0FKguAnMXLXDzTTuK7uBLS2mccdKyVZ5GHT0urhNgDdj42Q/UIpFxo+InMMA 6EDhtvqcQNc9RuNCvSYqQfsB+Rvtw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrgeeigdelvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjeegrdelgedrjeefrddvvddvne curfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghv vghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from ranger.suse (unknown [74.94.73.222]) by mail.messagingengine.com (Postfix) with ESMTPA id 02D67103C9; Mon, 22 Apr 2019 12:02:42 -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: Date: Mon, 22 Apr 2019 09:02:42 -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); > > 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? Attempt #2: test_(postgres)# \d identity_test Table "public.identity_test" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+---------------------------------- id | integer | | not null | generated by default as identity select * from identity_test; id ---- 2 3 do language plpgsql $$ declare m_max_id bigint; begin select max(id) + 1 from identity_test into m_max_id; EXECUTE 'alter table identity_test alter column id restart with ' || m_max_id; end; $$; select pg_get_serial_sequence('identity_test', 'id'); pg_get_serial_sequence ----------------------------- public.identity_test_id_seq select * from identity_test_id_seq test-# ; last_value | log_cnt | is_called ------------+---------+----------- 4 | 0 | f (1 row) > > 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