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 1hj99U-0000Io-Kv for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 21:20:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hj99T-0006lO-6e for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 21:20:23 +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 1hj99S-0006l4-Q3 for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 21:20:22 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hj99Q-0005Kh-Ch for pgsql-general@postgresql.org; Thu, 04 Jul 2019 21:20:21 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6965C2177A; Thu, 4 Jul 2019 17:20:19 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 04 Jul 2019 17:20:19 -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=fm3; bh=u fU+WOL/gA8IlsJoTND3HIFy5PA17tAE+lcF+9ch9Ks=; b=srhOAJuOlhyGjv8Ur eKsMe0/spEVnMfN6xYYAFh0yTSkae46dvcjY5RiYEgaM6jMtk0HkHUGZWdB0r2Jy fjxxICE/pX3Ut/tjdF8C+jbdvyKR2mlZharzZBg4EmaK1ILeOUGBAc2udyA4e2GH QHobKoJzSEwEzKTJU2kV/DpRpxxuPyUFBpIkG/WU0evhtsEzeYTHU1b5Deyj8P60 Vxosn27lrNSFzkH4KQR9Yqb6rt+StMwgpAbJ+kZR0E5Kp4lwVARrGllz1wPOdBtO 2ZG91cGlHPfMllF38+hP7bUoo7XcVfhoRm11WMSVciFZV+O+gmLXdm80EIiPDYBQ F+XQg== 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=fm3; bh=ufU+WOL/gA8IlsJoTND3HIFy5PA17tAE+lcF+9ch9 Ks=; b=rVv/bQqUJkolvBMkPJ3PtcVhyFygTNUKgFn28udVR2KMxz6RPbbaeJqO2 cCDBbUmY8cSKuE7s60ApiQ3Z//2JFCuQ/MfI7IJieK5tuKorebid1ie/7wYsBDRB T84GkKAr5kFbZ43NiiUnjdl42TRAN3anGlwFIMTjSNJZR2tmP5icDAFC1UdYbVYF 7NRpQDgATybiQKqvCDnAnUBcNWx1+Rs43FTLUKe5T+ff7Ba9Utuo9ayuEzbQg0lz tupoNKw+Gl5i24Eb34T1M7uIIb8DYPrTCpb8X5lrmAi5gqUGpJLyV1bR/1ntJkWs QAn/LZs3g0uXDxV7KOMDUnrO9jqPA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrfedvgdduieduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvdduvddrudehtddrvd dtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 8B88D8005C; Thu, 4 Jul 2019 17:20:18 -0400 (EDT) Subject: Re: Converting to identity columns with domains on PK columns To: PegoraroF10 , pgsql-general@postgresql.org References: <1562269287792-0.post@n3.nabble.com> <2a86ab61-0bdb-9ac9-22a0-02d3402ec7bd@aklaver.com> <1562270630672-0.post@n3.nabble.com> From: Adrian Klaver Message-ID: <41d40a94-4e5c-904b-dd29-8b0130a6920f@aklaver.com> Date: Thu, 4 Jul 2019 14:20:17 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: 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 7/4/19 1:27 PM, Adrian Klaver wrote: > On 7/4/19 1:03 PM, PegoraroF10 wrote: >> ok, thanks for the explanation but ... I cannot add a field and move >> data, >> constraints, triggers, identity to it because my pk field will be >> repositioned to the last field on that table and I have lots of other >> codes >> which point to pk as the first field on every table. > > I stay away from using the index position of field for this reason. > >> >> So, there is a way to convert that field to a identity field ? > > See my second option in previous post. > > Or, old school identity column:): > > create sequence id_seq owned by mytable.id; > > alter table mytable alter column id set default nextval('id_seq'); > > \d mytable >                          Table "public.mytable" >    Column    | Type | Collation | Nullable |           Default > -------------+------+-----------+----------+----------------------------- >  id          | i32  |           | not null | nextval('id_seq'::regclass) >  description | t50  |           |          | > Indexes: >     "mytable_pkey" PRIMARY KEY, btree (id) > Further thinking pointed up the peril of the above. Your field is defined as integer and per my previous post a sequence without an AS data_type will be bigint. At some point the sequence is going to start trying to set nextval() to a number your field cannot handle. If you go that route you will need to do something like: create sequence id_seq AS integer owned by mytable.id; -- Adrian Klaver adrian.klaver@aklaver.com