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 1hIebm-00081D-Md for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 19:28:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hIebl-0005Ka-Et for pgsql-general@arkaria.postgresql.org; Mon, 22 Apr 2019 19:28:05 +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 1hIebj-0005J6-DX for pgsql-general@lists.postgresql.org; Mon, 22 Apr 2019 19:28:05 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hIebf-00077b-J3 for pgsql-general@postgresql.org; Mon, 22 Apr 2019 19:28:01 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 0925C2EB; Mon, 22 Apr 2019 15:27:43 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 22 Apr 2019 15:27:44 -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=I Gfra2jXuN6nc1qvaiadcay6A5xYIXCWxEDChOiqEIY=; b=E6M64O52w61ANVfVQ 6KzpNuOQJj+N25WDKEZ9CvNtuKCVrf/7iRe0hssno5afLmrz95t0o9b097GLYIfb j0FPamK65QCHsfb2U37zxoCWCc6cnIh2BUE4j8+60hgWRACmnirHaWQ9dQYljVJP lsTS6tfFzMB/JVtRpAG5bMCVoi48alxJlB1kLbp8VWZkJ06JEzpQ0X96F3/nf4Ge wCv3GpRvZ0O7wcvs/mupMF3AWsuJgBVB03zTuM+DzOVxvyH+mcXYx2/sEtxksF4U EOLDyM7EYqJH6mpLv2uIAsMINLLcdaEbQGX/EvfeX2gc045Pts7DmmRoVWxTK15h oFG/A== 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=IGfra2jXuN6nc1qvaiadcay6A5xYIXCWxEDChOiqE IY=; b=sQxi79ISvO9q9L/W+vqjHDpq1odNxQ7OgS5MWZ3u97N2Ud34syKTfccJO 91sgCqieTbEaIZxZHqhBYBMlPd+wIZZ8ORiKqNW7q+UoGX0eMZz9+3G41IEbgu0d lRCvar2iTxwim/B/snunaxvQDPl2B636NVhokl27U1MCcqrzs+cydGjfUMi/tVo8 aGL84V3e1yTJxk4Jt4zAiKB+/znylcCP2qfXh/vuSHqqiyIh5ClgE9Yx6X2/Ihiy od72ghFXcgn6Pw2XuUdnSjf/AmGpMLiZfZ3lPzG0InHJjAjxO18rKotKyJgQVa0k rzTrWlO4iAv1aorVABVyNniPdHoyQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrgeeigddufedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuffhomhgrihhnpehpohhsthhgrhgvsh hqlhdrohhrghenucfkphepjedurddvuddvrdduhedtrddvtddvnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuve hluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 47599E407B; Mon, 22 Apr 2019 15:27:42 -0400 (EDT) Subject: Re: Resetting identity columns To: Ray O'Donnell , 'PostgreSQL' References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> <2864b568-76cf-c84b-52e6-8adee24962c3@rodonnell.ie> From: Adrian Klaver Message-ID: Date: Mon, 22 Apr 2019 12:27:41 -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: <2864b568-76cf-c84b-52e6-8adee24962c3@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 10:08 AM, Ray O'Donnell wrote: > On 22/04/2019 17:02, Adrian Klaver wrote: > >> 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; >> $$; > > Thanks a million Adrian - EXECUTE did the job, and I finished up > wrapping it in a function as I used it in a number of places in the > larger migration script: > > create function reset_identity( >     p_table text, >     p_column text > ) > returns text > as > $$ > declare >     m_max_id bigint; > begin >     execute 'select max(' || quote_ident(p_column) || ') + 1 from ' >        || quote_ident(p_table) into m_max_id; >     execute 'alter table ' || quote_ident(p_table) >        || ' alter column ' || quote_ident(p_column) >        || ' restart with ' || m_max_id; > >     return 'New identity value for ' || p_table || '.' || p_column >        || ': ' || m_max_id; > end; > $$ > language plpgsql; > > > In general, then, is it not possible to use an expression thus? - > >     [...] ALTER COLUMN [...] RESTART WITH No. It took some digging when I first ran into this to find out why. To follow up on Tom's post and show where it is called out: https://www.postgresql.org/docs/11/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN "Another restriction on parameter symbols is that they only work in SELECT, INSERT, UPDATE, and DELETE commands. In other statement types (generically called utility statements), you must insert values textually even if they are just data values." > > Thanks, > > Ray. > > -- Adrian Klaver adrian.klaver@aklaver.com